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

Wrap useDebounce call inside useCallback #37422

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from

Conversation

amustaque97
Copy link
Member

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:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

@Mamaduka Mamaduka added [Package] Server Side Render /packages/server-side-render [Type] Bug An existing feature does not function as intended labels Dec 28, 2021
@amustaque97 amustaque97 marked this pull request as ready for review January 28, 2022 16:57
@@ -140,7 +146,7 @@ export default function ServerSideRender( props ) {
} ) );

return fetchRequest;
}
}, [ isMountedRef, prevProps ] );
Copy link
Member

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.

Copy link
Member

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-equal package.
  • deep comparison: the fast-deep-equal package.

@Mamaduka
Copy link
Member

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.

@amustaque97
Copy link
Member Author

Hi @Mamaduka, by this weekend. I should be able to work on the review comments 🙂

@Mamaduka
Copy link
Member

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
@amustaque97 amustaque97 force-pushed the fix/useDebounce-does-not-call-function branch from 8480425 to 92d1c06 Compare August 7, 2022 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Server Side Render /packages/server-side-render [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useDebounce does not call function if the component refreshes
4 participants