forked from liferay/liferay-portal
-
Notifications
You must be signed in to change notification settings - Fork 1
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
LPS-116140 Custom event listeners in openModal util are initialized multiple times #59
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be interesting to know which item(s) in the deps array was/were triggering unwanted updates. One that stands out is this one,
customEvents
, which has a default value, so if not passed, you'll get a different array every time the function is called. A safer default for that would benull
, and you'd just need anif (customEvents)
guard before using it. This in general is true with React components, that non-primitive defaults (common examples being functions, objects, arrays) can be a source of trouble for exactly this reason.If you wanted to find out which elements in the array are the cause, you could do a comparison with
Object.is()
, which is what React is using internally to perform these checks.In my experience I believe refs tend to be pretty stable, but I don't think there are any guarantees documented anywhere. I'd be interested to hear from anybody who has "caught"
useRef
returning an unstable value; at least playing around in CodeSandbox now I always get back the same thing in a trivial example. I remember looking in the React source some months back to figure that out, but I've forgotten so would need to look again...The thing we actually care about is
eventHandlersRef.current
anyway; as you know, if you stick that in the array, the lint rule will tell you:FWIW, if there is any evidence that the ref is changing, I think I would just omit it from the list and if the linter complains, suppress it.
But looking at the React source, I'd say this shows the ref to be memoized and stable, so it is very unlikely to be the problem, and it seems safe for it to stay in the array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it was
visible
, based on my analysis at #50 (comment)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ones causing the unwanted updates are
customEvents
andprocessClose
. You are right, the ref is safe.Removing default for
customEvents
and wrapping usage inif
did work! That's such a weird quirk, I would never have guessed it works that way 😮That leaves
processClose
. It's a hard nut to crack, we are changing state in it. Is there some magic to workaround that one?If not, does it make sense to keep partial list of arguments? We would still have to keep the
// eslint-disable-next-line react-hooks/exhaustive-deps
line.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As it turns out, reverting
useCallback
does the job.With this, we don't need the rule exception. Let me push the changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
visible
as an argument indeed would cause updates. That's why I removed it, by reverting the commit where you added it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😂
Didn't realize I added it!!! This lint rule is waaaay toooo smart!! 😱
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But
processClose
only because you got rid of theuseCallback
, right? (I haven't looked at your new changes yet, so I don't know if you added back theuseCallback
.)... and reading further down the thread I see you discovered that.
I feel like we discussed
useCallback
on that one in a prior iteration, but I can't find the reference to it. 🤷There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was it: https://github.com/wincent/liferay-portal/pull/295#discussion_r430402239