Skip to content

Commit 41fbe7e

Browse files
author
Chris Bobbe
committed
compose box: On componentWillUnmount, clear 200ms timeout on input blurs.
To avoid calling setState on an unmounted component (yellow box warning observed at zulip#2937 (comment)), clear the timeouts on componentWillUnmount that get set on handleMessageBlur and handleTopicBlur to delay the call to this.updateIsFocused (which calls this.setState) by 200ms. A better strategy than using setTimeout was opened as zulip#3843.
1 parent 9319af0 commit 41fbe7e

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/compose/ComposeBox.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ class ComposeBox extends PureComponent<Props, State> {
107107
messageInput: ?TextInput = null;
108108
topicInput: ?TextInput = null;
109109

110+
inputBlurTimeoutId: ?TimeoutID = null;
111+
110112
static contextTypes = {
111113
styles: () => null,
112114
};
@@ -122,6 +124,11 @@ class ComposeBox extends PureComponent<Props, State> {
122124
selection: { start: 0, end: 0 },
123125
};
124126

127+
componentWillUnmount() {
128+
clearTimeout(this.inputBlurTimeoutId);
129+
this.inputBlurTimeoutId = null;
130+
}
131+
125132
updateIsFocused = () => {
126133
this.setState(state => ({
127134
...state,
@@ -201,7 +208,9 @@ class ComposeBox extends PureComponent<Props, State> {
201208
isMessageFocused: false,
202209
isMenuExpanded: false,
203210
});
204-
setTimeout(this.updateIsFocused, 200); // give a chance to the topic input to get the focus
211+
// give a chance to the topic input to get the focus
212+
clearTimeout(this.inputBlurTimeoutId);
213+
this.inputBlurTimeoutId = setTimeout(this.updateIsFocused, 200);
205214
};
206215

207216
handleTopicFocus = () => {
@@ -219,7 +228,9 @@ class ComposeBox extends PureComponent<Props, State> {
219228
isTopicFocused: false,
220229
isMenuExpanded: false,
221230
});
222-
setTimeout(this.updateIsFocused, 200); // give a chance to the message input to get the focus
231+
// give a chance to the message input to get the focus
232+
clearTimeout(this.inputBlurTimeoutId);
233+
this.inputBlurTimeoutId = setTimeout(this.updateIsFocused, 200);
223234
};
224235

225236
handleInputTouchStart = () => {

0 commit comments

Comments
 (0)