-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Wrap useDebounce call inside useCallback
#37422
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
Wrap useDebounce call inside useCallback
#37422
Conversation
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.
You shouldn't put refs inside dependency callbacks. It doesn't work the way you might think it works:
https://epicreact.dev/why-you-shouldnt-put-refs-in-a-dependency-array/
Also, becuase prevProps is an object, it will fail the identity equality check on every render due to props being a new object on every render. And prevProps is not even used inside the callback, so it shouldn't be in the dependency array anyway.
attributes, httpMethod, and urlQueryArgs are the actual dependencies of the callback. httpMethod can easily be added to the dependency array, but because attributes and urlQueryArgs are objects, you can't just put them in the dependency array directly, so you'd have to store their versions from the prior render using usePrevious, perform deep equality checks using Lodash's equals, and put the boolean result of those comparisons in the dependency array.
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.
Just a side note that since we no longer use Lodash in the entire repository (see #52571), we're using a couple of methods for comparison:
- shallow comparison: see the
@wordpress/is-shallow-equalpackage. - deep comparison: the
fast-deep-equalpackage.
|
Hi, @amustaque97 This PR came up during the weekly editor bug scrub, and I wanted to check if you plan to continue working on it. |
|
Hi @Mamaduka, by this weekend. I should be able to work on the review comments 🙂 |
|
Thanks for the reply, @amustaque97! Zero pressure, I just wanted to check on the status 👍 |
This is done in order to avoid function call getting cancelled on multiple refresh
fix `useCallback` dependency array
8480425 to
92d1c06
Compare
|
Thanks for working on this, @amustaque97! I believe this issue was resolved as part of different bug report - #69237. |
Fixes: #33839
Description
This is done in order to avoid function call getting cancelled on multiple refresh
How has this been tested?
Screenshots
Types of changes
Checklist:
*.native.jsfiles for terms that need renaming or removal).