-
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
ToolsPanel: Trigger onDeselect
/onSelect
callbacks directly
#61694
base: trunk
Are you sure you want to change the base?
Conversation
ba2ce5c
to
b77c611
Compare
onDeselect
/onSelect
callbacks directlyonDeselect
/onSelect
callbacks directly
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Instead of via an effect hook.
b77c611
to
df3451b
Compare
? menuItems?.[ menuGroup ]?.[ label ] !== undefined | ||
: isMenuItemChecked; | ||
const isShown = isShownByDefault ? isRegistered : isMenuItemChecked; |
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.
Not related to the purpose of the PR, this was just to reuse isRegistered
instead of reevaluating the same condition.
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.
// hasValue is a new function on every render, so do not add it as a | |
// dependency to the useCallback hook! If needed, we should use a ref. | |
// eslint-disable-next-line react-hooks/exhaustive-deps | |
const hasValueCallback = useCallback( hasValue, [ panelId ] ); | |
// resetAllFilter is a new function on every render, so do not add it as a | |
// dependency to the useCallback hook! If needed, we should use a ref. | |
// eslint-disable-next-line react-hooks/exhaustive-deps | |
const resetAllFilterCallback = useCallback( resetAllFilter, [ panelId ] ); | |
const onDeselect = useEvent( onDeselectProp ); | |
const onSelect = useEvent( onSelectProp ); |
The useEvent
calls are added in 5db7309, and I just want to highlight the discrepancy with the callbacks above which are memoized by panelId
, the same could be done for onDeselect
and onSelect
if that seems better. If so, I’d rather return all of them from a single useMemo
.
What?
Instead of using an effect hook to run some callbacks runs them from an event.
Why?
Simplification for maintainability. I noticed this while working on #60621.
How?
onDeselect
andonSelect
callbacks into state accessible bytoggleItem
.toggleItem
which is the event handler for menu item clicks.Automated tests
Currently, with the effect being responsible for triggering the callbacks, I wasn’t sure if
onSelect
oronDeselect
might be triggered from controlled updates to an item’s value or interactions with the item’s actual control. I made some unit tests for such and according to these, it was never a thing (they pass on trunk or this branch). I'm not sure if these tests are actually worth adding.Testing Instructions
Testing Instructions for Keyboard
Screenshots or screencast