-
Notifications
You must be signed in to change notification settings - Fork 4.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
Remove effect dep in Link Control #53421
Conversation
if ( prevIsEditing === forceIsEditingLink ) { | ||
return prevIsEditing; | ||
} |
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.
Can someone confidence check me here on this logic 😓
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.
At this point, the forceIsEditingLink
should be boolean
, correct? Then you could just call the setIsEditingLink( forceIsEditingLink )
; React won't re-render when prevState === nextState
.
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.
Aha! Well then...maybe an opportunity more optimisation.
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.
Updated. I wonder about the undefined
check above now as well 🤔
Although...there seem to be multiple checks for undefined
.
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 understand that forceIsEditingLink
should be undefined
when the components want to derive the initial isEditingLink
state based on value.
Size Change: -117 B (0%) Total Size: 1.44 MB
ℹ️ View Unchanged
|
// Todo: bug if the missing dep is introduced. Will need a fix. | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
|
||
setIsEditingLink( forceIsEditingLink ); |
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.
Don't we also need to define setIsEditingLink
as a dependency?
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.
My understanding is that React provides a contract that state setters will be stable. Certainly dispatch
from useReducer
is guaranteed to be stable so I have always assumed the setters for useState
are also.
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.
There's no need to specify callbacks returned by useState
or useReducer
. React ensures they'll always have a stable reference and are exempt from exhaustive-deps
.
We do the same with useDispatch(storeName)
and useSelect(storeName
, but I think adding custom rules to React Hooks linter was too much work.
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 tested this with Button block and Link format and couldn't spot any regressions.
What?
Fixes "Todo" in code by removing unneeded effect dependency for
isEditingLink
update.Why?
Eslint was disabling the lint rule to add the dependency and there was a comment saying the dependency couldn't be added without causing bugs.
How?
Uses functional form of "setState" to consume current value of
isEditingLink
state beforesetIsEditingLink
is called based on value offorceIsEditingLink
.Testing Instructions
Testing Instructions for Keyboard
Screenshots or screencast