[Fix] Prevent outside click events for unmounted component #26
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I have two nested components, each wrapped into own OutsideClickHandler. Both components is unmouning on outside clicks by calling a handler function. This handler functions are located in their parents and passed to the components in props. Handler functions are setting this.state which in turn unmount the child (our component, wrapped into OutsideClickHandler).
The problem appears when user make click out of the outer component of this pair. In this case the click event is emitting for both OutsideClickHandler components but the order of called handlers is:
And therefore the inner/child component receive the outside click event after outer/parent component unmounted. In turn it's also trying to set state to unmount itself. At this moment the ReactJs show his warning:
I see that current implementation removes event listeners in the componentWillUnmount but this is not enough in the case of nested components, because event is already in queue and will call the handler anyway even after component is unmouned.