Skip to content
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

Block Editor: Clear toolbar timeout by ref on unmount #20546

Merged
merged 2 commits into from
Mar 2, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/block-toolbar/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export function useDebouncedShowMovers( {
[ isFocused ]
);

useEffect( () => () => clearTimeout( timeoutRef.current ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to uun this on unmount, you need to add [] as a second argument of useEffect otherwise it runs on each render.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch Riad, I see it explained in React docs:

The default behavior for effects is to fire the effect after every completed render. That way an effect is always recreated if one of its dependencies changes.

If you want to run an effect and clean it up only once (on mount and unmount), you can pass an empty array ([]) as a second argument. This tells React that your effect doesn’t depend on any values from props or state, so it never needs to re-run. This isn’t handled as a special case — it follows directly from how the dependencies array always works.

https://reactjs.org/docs/hooks-reference.html#conditionally-firing-an-effect

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 02eb218.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks for spotting @youknowriad, and for the fix @gziolo .


return {
showMovers,
debouncedShowMovers,
Expand Down