Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not call setState if combo box is no longer mounted #807

Merged
merged 3 commits into from
May 9, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/components/combo_box/combo_box.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export class EuiComboBox extends Component {
this.searchInput = undefined;
this.optionsList = undefined;
this.options = [];

this._hasUnmounted = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since a component will also be in an unmounted state when initialized, I would rather recommend, having a variable _isMounted, that is false by default, will be set to true, when the component is mounted, and set to false again when the component will unmount, e.g. see also this comment.

But this is more theoretical, since we can be pretty sure, that updateListPosition will never be called before we mounted, so I leave this decision up to you :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought componentWillMount was getting deprecated in react 16.3. How would _isMounted get set to true without componentWillMount?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think componentDidMount should do the trick here, since there is also nothing you can do in componentWillMount anymore that should triggers the promise load.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this component tracks a ref to its top-level div you could instead check if this. comboBox is null or not, as React sets the ref on mount and re-executes the ref function again with null on unmount. They're all equally as hacky IMO.

}

getMatchingOptions = (options, selectedOptions, searchValue) => {
Expand All @@ -96,6 +98,10 @@ export class EuiComboBox extends Component {
};

updateListPosition = (listBounds = this.listBounds) => {
if (this._hasUnmounted) {
return;
}

if (!this.state.isListOpen) {
return;
}
Expand Down Expand Up @@ -483,6 +489,8 @@ export class EuiComboBox extends Component {
}

componentWillUnmount() {
this.incrementActiveOptionIndex.cancel;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want to call cancel actually :-) .cancel()

this._hasUnmounted = true;
document.removeEventListener('click', this.onDocumentFocusChange);
document.removeEventListener('focusin', this.onDocumentFocusChange);
}
Expand Down