Components: refactor NavigatorScreen to pass exhaustive-deps
#43876
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What?
Updates the
NavigatorScreencomponent pass theexhaustive-depseslint rule.Why?
Part of the effort in #41166 to apply
exhuastive-depsto the Components packageHow?
Adding
location.isBackandpreviousLocation?.focusTargetSelectorto theuseEffectdeps 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 thegoToandgoBackvalues that are being drawn from context. This, in turn, causes an update to the context value as a whole (goToandgoBackboth appear in theuseMemodep array fornavigatorContextValue). Updating thenavigatorContextValuemeans that even if it didn't change on this particular render, alllocationvalues are being redeclared, which will trigger a re-render thanks to our new dependencies.All of that said, I know
Navigatoris your brainchild @ciampo so please let me know if that logic sounds flawed! 🙂Testing Instructions
npx eslint --rule 'react-hooks/exhaustive-deps: warn' packages/components/src/navigatior/navigator-screennavigatorunit tests still passNavigatorcomponent stories and/or docs still work as expected