-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[es_ui_shared] Fix eslint exhaustive deps rule #76392
Merged
sebelga
merged 11 commits into
elastic:master
from
sebelga:fix-es-lint-exhaustive-deps-rule
Sep 3, 2020
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e0d7039
Fix GlobalFlyout rule
sebelga 7e7611a
Fix useJson and <JsonEditor /> rule
sebelga 6d152dd
Fix <RangeField /> rule
sebelga c09a5be
Merge branch 'master' into fix-es-lint-exhaustive-deps-rule
elasticmachine 8727c9d
Merge branch 'master' into fix-es-lint-exhaustive-deps-rule
elasticmachine bba9d40
Fix JSON editor when it is "controlled"
sebelga 1f9ae41
Update embeddable InputEditor example to use non controlled JsonEditor
sebelga ba97afd
Merge branch 'fix-es-lint-exhaustive-deps-rule' of github.com:sebelga…
sebelga 438d9f2
Revert "Update embeddable InputEditor example to use non controlled J…
sebelga 16a8514
Fix JsonEditor: don't use hook state when component is controlled
sebelga 8f2c42f
Merge remote-tracking branch 'upstream/master' into fix-es-lint-exhau…
sebelga 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
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
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
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
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
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.
I'm having trouble figuring out how we'd get into this state. Did you run into a situation where the component wasn't mounted when this function was called?
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.
Lol that's a great catch! 😊 I refactored a bit too quickly, I will put back how it was before. The intention was to not call the
onUpdate
on mount as... nothing has been updated yet.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.
EDIT: Looking at the code it is correct. So like I said, the idea is not to call the
onUpdate
on component mount as there isn't any update.In the next
useEffect
the componentisMounted.current
will be set to true so on the next update it will call the handler.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.
Ah, I see. I think the use of the name
isMounted
is confusing to me, particularly because it defaults tofalse
. The first time a hook is called is when the owner mounts, so by definition when I'm walking through a hook's "lifecycle" I begin with the mindset of it being mounted somewhere. To fix this, how about renaming the variableisInitialCall
orisFirstUpdate
, defaulting it totrue
and then setting it tofalse
? I think this area of the code also deserves a comment explaining your intentions to the reader.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 must default to false. when the ref is created the component has not mounted yet. It only has mounted when a
useEffect
is called (everyuseEffect
is called in order of appearance in the code. I learned that the hard way).So the convention I have put in place everywhere is the same:
isMounted
(because that is the React term for this and I prefer not to add another concept) defaults tofalse
and is set totrue
in the lastuseEffect
in the code. So any previous useEffect will be called when mounting but the ref will still be false for them. This is exactly what we want: have a ref in our useEffects telling us if the component has mounted or not, and have some conditional logic to prevent doing certain things "on component mount". As you commented somewhere,useEffect
should have a single purpose and not one giantuseEffect
(which would probably not work anyway).This is what I shared in my "Lesson learned" here #75796
You will see around things like this
My convention is exactly this pattern but, as I have learned, it is much better to have a dedicated useEffect to handle this ref, and set it as last useEffect without any deps.
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.
👍 Thanks for explaining. I think some comments in the code would help explain this to others (or even our future selves).