Skip to content

Commit

Permalink
Avoid unnecessary router subscribes (#10409)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Nanberg <alex.nanberg@hotmail.com>
  • Loading branch information
brophdawg11 and alexandernanberg committed Apr 27, 2023
1 parent 82ca7c0 commit f4a452b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-stingrays-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Avoid uneccesary unsubscribe/resubscribes on router state changes
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- akamfoad
- alany411
- alberto
- alexandernanberg
- alexlbr
- AmRo045
- amsal
Expand Down
11 changes: 2 additions & 9 deletions packages/react-router/lib/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,10 @@ export function RouterProvider({
fallbackElement,
router,
}: RouterProviderProps): React.ReactElement {
let [state, setState] = React.useState(router.state);

// Need to use a layout effect here so we are subscribed early enough to
// pick up on any render-driven redirects/navigations (useEffect/<Navigate>)
React.useLayoutEffect(() => {
return router.subscribe((newState) => {
if (newState !== state) {
setState(newState);
}
});
}, [router, state]);
let [state, setState] = React.useState(router.state);
React.useLayoutEffect(() => router.subscribe(setState), [router, setState]);

let navigator = React.useMemo((): Navigator => {
return {
Expand Down

0 comments on commit f4a452b

Please sign in to comment.