Repository navigation
fix(react-router): stop hidden views from re-rendering on every navigation #31541
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/react-router/src/ReactRouter/utils/areParamsEqual.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| export type RouteParams = Record<string, string | string[] | undefined>; | ||
|
|
||
| /** | ||
| * Compares two route param objects by value, treating a missing object as empty. | ||
| */ | ||
| export const areParamsEqual = (a?: RouteParams, b?: RouteParams): boolean => { | ||
| const paramsA = a || {}; | ||
| const paramsB = b || {}; | ||
| const keysA = Object.keys(paramsA); | ||
| const keysB = Object.keys(paramsB); | ||
|
|
||
| if (keysA.length !== keysB.length) { | ||
| return false; | ||
| } | ||
|
|
||
| return keysA.every((key) => { | ||
| const valueA = paramsA[key]; | ||
| const valueB = paramsB[key]; | ||
| if (Array.isArray(valueA) && Array.isArray(valueB)) { | ||
| if (valueA.length !== valueB.length) { | ||
| return false; | ||
| } | ||
| return valueA.every((entry, idx) => entry === valueB[idx]); | ||
| } | ||
| return valueA === valueB; | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
packages/react-router/test/base/src/pages/hidden-view-renders/HiddenViewRenders.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { | ||
| IonTabs, | ||
| IonRouterOutlet, | ||
| IonTabBar, | ||
| IonTabButton, | ||
| IonLabel, | ||
| IonPage, | ||
| IonHeader, | ||
| IonToolbar, | ||
| IonTitle, | ||
| IonContent, | ||
| IonButton, | ||
| useIonRouter, | ||
| } from '@ionic/react'; | ||
| import React, { useEffect } from 'react'; | ||
| import { Route, Navigate, useParams } from 'react-router-dom'; | ||
|
|
||
| import TestDescription from '../../components/TestDescription'; | ||
| import { countRender } from '../../utils'; | ||
|
|
||
| /** | ||
| * Counts committed renders. An effect with no dependency list runs once per | ||
| * commit, so a render that bails out isn't counted. | ||
| */ | ||
| const useCommitCounter = (id: string) => { | ||
| useEffect(() => countRender(id)); | ||
| }; | ||
|
|
||
| const ParamsReader: React.FC<{ tab: string }> = ({ tab }) => { | ||
| useParams(); | ||
| useCommitCounter(`${tab}:params`); | ||
| return null; | ||
| }; | ||
|
|
||
| const IonRouterReader: React.FC<{ tab: string }> = ({ tab }) => { | ||
| useIonRouter(); | ||
| useCommitCounter(`${tab}:ionRouter`); | ||
| return null; | ||
| }; | ||
|
|
||
| const TabPage: React.FC<{ tab: string }> = ({ tab }) => { | ||
| return ( | ||
| <IonPage data-pageid={`hidden-view-renders-${tab}`}> | ||
| <IonHeader> | ||
| <IonToolbar> | ||
| <IonTitle>Tab {tab.toUpperCase()}</IonTitle> | ||
| </IonToolbar> | ||
| </IonHeader> | ||
| <IonContent> | ||
| <ParamsReader tab={tab} /> | ||
| <IonRouterReader tab={tab} /> | ||
| <TestDescription> | ||
| Switch between Tabs A, B and C a few times. Hidden pages that read useParams should not re-render, and ones | ||
| that read useIonRouter should re-render once per switch. Counts are in window.renderCounts. | ||
| </TestDescription> | ||
| </IonContent> | ||
| </IonPage> | ||
| ); | ||
| }; | ||
|
|
||
| const SplatPage: React.FC = () => { | ||
| const params = useParams(); | ||
| useCommitCounter('d:params'); | ||
|
|
||
| return ( | ||
| <IonPage data-pageid="hidden-view-renders-d"> | ||
| <IonHeader> | ||
| <IonToolbar> | ||
| <IonTitle>Tab D</IonTitle> | ||
| </IonToolbar> | ||
| </IonHeader> | ||
| <IonContent> | ||
| <div id="splat-param">{params['*']}</div> | ||
| <IonButton routerLink="/hidden-view-renders/d/two" id="go-to-d-two"> | ||
| Go to D Two | ||
| </IonButton> | ||
| <TestDescription> | ||
| Tap Go to D Two. The splat param above should change to "two", and window.renderCounts['d:params'] should go | ||
| up by 1. | ||
| </TestDescription> | ||
| </IonContent> | ||
| </IonPage> | ||
| ); | ||
| }; | ||
|
|
||
| const HiddenViewRenders: React.FC = () => { | ||
| return ( | ||
| <IonTabs data-pageid="hidden-view-renders"> | ||
| <IonRouterOutlet id="hidden-view-renders"> | ||
| <Route index element={<Navigate to="/hidden-view-renders/a" replace />} /> | ||
| <Route path="a" element={<TabPage tab="a" />} /> | ||
| <Route path="b" element={<TabPage tab="b" />} /> | ||
| <Route path="c" element={<TabPage tab="c" />} /> | ||
| <Route path="d/*" element={<SplatPage />} /> | ||
| </IonRouterOutlet> | ||
| <IonTabBar slot="bottom"> | ||
| <IonTabButton tab="a" href="/hidden-view-renders/a"> | ||
| <IonLabel>Tab A</IonLabel> | ||
| </IonTabButton> | ||
| <IonTabButton tab="b" href="/hidden-view-renders/b"> | ||
| <IonLabel>Tab B</IonLabel> | ||
| </IonTabButton> | ||
| <IonTabButton tab="c" href="/hidden-view-renders/c"> | ||
| <IonLabel>Tab C</IonLabel> | ||
| </IonTabButton> | ||
| <IonTabButton tab="d" href="/hidden-view-renders/d/one"> | ||
| <IonLabel>Tab D</IonLabel> | ||
| </IonTabButton> | ||
| </IonTabBar> | ||
| </IonTabs> | ||
| ); | ||
| }; | ||
|
|
||
| export default HiddenViewRenders; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from './generateId'; | ||
| export * from './lifecycleEvents'; | ||
| export * from './dev'; | ||
| export * from './renderCounts'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /** Records a committed render under `id` on `window.renderCounts` for a spec to assert on. */ | ||
| export const countRender = (id: string) => { | ||
| (window as any).renderCounts = (window as any).renderCounts || {}; | ||
| (window as any).renderCounts[id] = ((window as any).renderCounts[id] ?? 0) + 1; | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for adding why this page exists! Same for the rest.