-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Refactor around missing dependency in Link Control internal value sync effect #49509
Conversation
Size Change: +6 B (0%) Total Size: 1.37 MB
ℹ️ View Unchanged
|
One other suggestion to explore (hat tip @kevin940726) is |
Could we try this solution here and see if the test passes? I think we can even simplify it to just: setInternalInputValue( value ); Since that the setter of |
029caff
to
0c5f357
Compare
packages/block-editor/src/components/link-control/test/index.js
Outdated
Show resolved
Hide resolved
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 tests as expected and the code looks good.
What?
In #49506 we found that the effect at
gutenberg/packages/block-editor/src/components/link-control/use-internal-input-value.js
Lines 11 to 19 in a1f2679
internalInputValue
as a dependency.This fixes that by using the lazy update of setState to avoid having to pass the dependency.
Fixes #49506
Why?
Omitting dependencies from an effect is a code smell. However, adding the dependency in this case makes it impossible to update the values of in the component as they will constantly be synced back up with the
value
. That's not what we want.Instead we want to make sure that if the LinkControl's
value
(only!) changes (or rather the particular sub property that this hook is controlling) then we sync the internal state back up with it.Why do we even need this syncing anyway
The LinkControl is a controlled component in that it's
value
changes based ononChange
handler. However, the UX is such that you should be able to update the.value
of the<input>
's in a sort of "draft" state and only have them "committed" as the newvalue
when hitting the "Apply" button (this will triggeronChange
).Unfortunately however this means that if:
<input>
value
to the component...the internal state needs to reflect the new value passed to the component and not the internal "draft" value.
Basically the
value
should be the source of truth. If it changes then the component must reset.How?
By using the lazy form of
setState
updater, we can access the existing internal value and perform the necessary comparison without having to add it as a dependency of the effect. This restores the existing functionality without breaking the rules of hooks.Testing Instructions
key
prop heregutenberg/packages/format-library/src/link/inline.js
Line 262 in 78c4a2a
Testing Instructions for Keyboard
Screenshots or screencast