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

Components: refactor NavigatorScreen to pass exhaustive-deps #43876

Merged
merged 2 commits into from
Sep 6, 2022

Conversation

chad1008
Copy link
Contributor

@chad1008 chad1008 commented Sep 5, 2022

What?

Updates the NavigatorScreen component pass the exhaustive-deps eslint rule.

Why?

Part of the effort in #41166 to apply exhuastive-deps to the Components package

How?

Adding location.isBack and previousLocation?.focusTargetSelector to the useEffect deps array.

This does cause some additional renders on navigation between screens, but they don't appear problematic in my tests. I think they're happening because each time you navigate the locationHistory changes. This triggers an update to the goTo and goBack values that are being drawn from context. This, in turn, causes an update to the context value as a whole (goTo and goBack both appear in the useMemo dep array for navigatorContextValue). Updating the navigatorContextValue means that even if it didn't change on this particular render, all location values are being redeclared, which will trigger a re-render thanks to our new dependencies.

All of that said, I know Navigator is your brainchild @ciampo so please let me know if that logic sounds flawed! 🙂

Testing Instructions

  1. From your local Gutenberg directory, run npx eslint --rule 'react-hooks/exhaustive-deps: warn' packages/components/src/navigatior/navigator-screen
  2. Confirm that the linter returns no errors or warnings
  3. Confirm navigator unit tests still pass
  4. Run Storybook locally, confirm the Navigator component stories and/or docs still work as expected

@chad1008 chad1008 added [Type] Code Quality Issues or PRs that relate to code quality [Package] Components /packages/components [Feature] Component System WordPress component system labels Sep 5, 2022
@chad1008 chad1008 self-assigned this Sep 5, 2022
@chad1008 chad1008 requested a review from ajitbohra as a code owner September 5, 2022 19:23
@chad1008 chad1008 requested review from mirka and ciampo September 5, 2022 19:24
Copy link
Contributor

@ciampo ciampo left a comment

Choose a reason for hiding this comment

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

  • ✅ Code changes make sense
  • ✅ Unit tests are passing
  • ✅ Storybook example behaves as expected
  • ✅ Global Styles sidebar in the Site editor works as expected

This does cause some additional renders on navigation between screens, but they don't appear problematic in my tests.

Does it cause more component re-renders, or just that useEffect to run more often?

Also, I wonder if we should add an if statement around elementToFocus.focus(); and make sure that we only focus if document.activeElement !== elementToFocus ?

@chad1008
Copy link
Contributor Author

chad1008 commented Sep 6, 2022

Does it cause more component re-renders, or just that useEffect to run more often?

Good point. I was conflating the two. Upon closer inspection, before this change there were four re-renders and two useEffect runs per navigation. With this change there are still four re-renders, but now there are four useEffect runs. So it isn't re-rendering more, it's just running the effect every time because it's catching the changes in location.

Also, I wonder if we should add an if statement around elementToFocus.focus(); and make sure that we only focus if document.activeElement !== elementToFocus ?

Makes sense! No need to focus if the desired element is already active.

I'll put a followup PR together 👍

@chad1008 chad1008 merged commit c57b0cf into trunk Sep 6, 2022
@chad1008 chad1008 deleted the refactor/NavigatorScreen-exhuastive-deps branch September 6, 2022 12:52
@github-actions github-actions bot added this to the Gutenberg 14.1 milestone Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Component System WordPress component system [Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants