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

withNotices: Ensure that the callback props are stable. #29491

Merged

Conversation

pablinos
Copy link
Member

@pablinos pablinos commented Mar 2, 2021

Description

In #28676 withNotices was refactored to as a functional component. In the Jetpack podcast player block, the functions supplied as props were being used within a function calling the API. This essentially meant they became a dependency of the useEffect hook and caused the API call to happen in a loop.

If these are used in hooks elsewhere it would also cause a loop as the function references change on each render.

This change constructs each of the noticeOperations using useCallback to memoize them and prevent any unnecessary rerenders.

How has this been tested?

This has been tested in conjunction with the latest release of Jetpack. The bug can be seen by inserting a podcast block into a post and setting the podcast feed URL. With this PR the loop is removed.

Screenshots

podcastplayerloop

Types of changes

Bug fix - It makes withNotices behave more as it did before the refactor.

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.

@retrofox
Copy link
Contributor

retrofox commented Mar 3, 2021

I tested and it looks good to me, but I'd like to get other opinions. @stokesman would you be able to take a look too? Thanks in advance :-)

Copy link
Contributor

@stokesman stokesman left a comment

Choose a reason for hiding this comment

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

I've reproduced the issue with the Jetpack podcast player block and verified that these changes resolve that issue. So I don't see any reason not to 🚢

Copy link
Contributor

@retrofox retrofox left a comment

Choose a reason for hiding this comment

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

Tested and it works as expected. ✅ too. 👍

@retrofox
Copy link
Contributor

retrofox commented Mar 4, 2021

Tested and it works as expected. Merging since Paul doesn't have access to merge yet.

@retrofox retrofox merged commit 3483fbe into WordPress:trunk Mar 4, 2021
@github-actions github-actions bot added this to the Gutenberg 10.2 milestone Mar 4, 2021
@youknowriad youknowriad added the [Type] Code Quality Issues or PRs that relate to code quality label Mar 4, 2021
pablinos added a commit to pablinos/gutenberg that referenced this pull request Mar 5, 2021
The refactor of withNotices in WordPress#28676 introduced a bug where the
functions within `noticeOperations` were changing on every render. That
meant that if they were present in the dependency array of a useEffect,
then the effect would trigger causing an infinite loop.

This was partially fixed with WordPress#29491, but there are other instances
where the noticeOperations object itself is being listed as a
dependency, and so the problem persists.

As suggested by @stokesman
WordPress#29491 (comment)
this change memoizes the whole object.
ntsekouras pushed a commit that referenced this pull request Mar 5, 2021
The refactor of withNotices in #28676 introduced a bug where the
functions within `noticeOperations` were changing on every render. That
meant that if they were present in the dependency array of a useEffect,
then the effect would trigger causing an infinite loop.

This was partially fixed with #29491, but there are other instances
where the noticeOperations object itself is being listed as a
dependency, and so the problem persists.

As suggested by @stokesman
#29491 (comment)
this change memoizes the whole object.
ntsekouras pushed a commit that referenced this pull request Mar 5, 2021
The refactor of withNotices in #28676 introduced a bug where the
functions within `noticeOperations` were changing on every render. That
meant that if they were present in the dependency array of a useEffect,
then the effect would trigger causing an infinite loop.

This was partially fixed with #29491, but there are other instances
where the noticeOperations object itself is being listed as a
dependency, and so the problem persists.

As suggested by @stokesman
#29491 (comment)
this change memoizes the whole object.
@youknowriad youknowriad added [Type] Bug An existing feature does not function as intended and removed [Type] Code Quality Issues or PRs that relate to code quality labels Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants