From 672ba5d7c401aeef7f0d7f66fc2e91d8b385c71b Mon Sep 17 00:00:00 2001 From: danikaze Date: Mon, 15 Apr 2019 12:23:50 +0900 Subject: [PATCH] Fix trying to setState when the component is umounted (#28) --- src/index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 220439b..7da81c5 100644 --- a/src/index.js +++ b/src/index.js @@ -40,6 +40,8 @@ class TextInput extends PureComponent { cursorWidth: 0 } + isMounted = false; + render() { const {value, placeholder, showCursor, focus, mask, highlightPastedText} = this.props; const {cursorOffset, cursorWidth} = this.state; @@ -81,6 +83,7 @@ class TextInput extends PureComponent { componentDidMount() { const {stdin, setRawMode} = this.props; + this.isMounted = true; setRawMode(true); stdin.on('data', this.handleInput); } @@ -88,6 +91,7 @@ class TextInput extends PureComponent { componentWillUnmount() { const {stdin, setRawMode} = this.props; + this.isMounted = false; stdin.removeListener('data', this.handleInput); setRawMode(false); } @@ -96,7 +100,7 @@ class TextInput extends PureComponent { const {value: originalValue, focus, showCursor, mask, onChange, onSubmit} = this.props; const {cursorOffset: originalCursorOffset} = this.state; - if (focus === false) { + if (focus === false || this.isMounted === false) { return; }