Skip to content

Commit

Permalink
Fix trying to setState when the component is umounted (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
danikaze authored and Vadim Demedes committed Apr 15, 2019
1 parent e3f4264 commit 672ba5d
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,13 +83,15 @@ class TextInput extends PureComponent {
componentDidMount() {
const {stdin, setRawMode} = this.props;

this.isMounted = true;
setRawMode(true);
stdin.on('data', this.handleInput);
}

componentWillUnmount() {
const {stdin, setRawMode} = this.props;

this.isMounted = false;
stdin.removeListener('data', this.handleInput);
setRawMode(false);
}
Expand All @@ -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;
}

Expand Down

0 comments on commit 672ba5d

Please sign in to comment.