[Fix] Prevent outside click events for unmounted component#26
Open
BaZzz01010101 wants to merge 2 commits intoairbnb:masterfrom
Open
[Fix] Prevent outside click events for unmounted component#26BaZzz01010101 wants to merge 2 commits intoairbnb:masterfrom
BaZzz01010101 wants to merge 2 commits intoairbnb:masterfrom
Conversation
Author
|
P.S. I used _isMounted because components still contains deprecated read-only isMounted property which always |
ljharb
requested changes
Jun 12, 2019
Collaborator
ljharb
left a comment
There was a problem hiding this comment.
Can you add a regression test?
Author
|
I can try, but I have no experience with test frameworks. Update: |
ljharb
reviewed
Jun 12, 2019
| describe('when `this.childNode` does not contain `e.target`', () => { | ||
| it('is a noop if `this._isMounted` is `false`', () => { | ||
| const spy = sinon.spy(); | ||
| const wrapper = shallow(<OutsideClickHandler onOutsideClick={spy} />); |
Collaborator
There was a problem hiding this comment.
use mount here, and then wrapper.unmount() to test unmounting a component.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.