Navigating between two React Router paths that use one component does not reset it
The URL changes but the form, scroll or counter keeps the old valuesShort answer
React keeps a component's state while it is rendered at the same position in the tree. Two routes with the same element, or one route with a changing parameter, therefore reuse the same instance. Give the element a key tied to the URL parameter to reset it, and make Effects depend on the parameter.
Fix it fast
1Find out which cause you have
Log from the component on mount and on unmount. If navigation produces no unmount, React is reusing the instance.
useEffect(() => { console.log('mounted'); return () => console.log('unmounted'); }, []);2Match what you see, then jump to its fix
- 1
No unmount log when the path or parameter changesReact reuses the component at the same positionFix - 2
The fetch runs on the first visit only, and the dependency list is []An Effect loads data once and ignores the parameterFix - 3
The component uses a value captured at first render instead of useParamsThe parameter is not read from the route at allFix
- 1
What does the error look like?
The URL changes but the form, scroll or counter keeps the old valuesData from the first page stays when moving to /items/2An Effect with an empty dependency list does not run again on navigation
Cause 1 of 3
React reuses the component at the same position
1Check
When a different component is not rendered at that position, React sees the same component with new props and keeps its state.
grep -rn "<Route" src/2Fix
Add a key that changes with the route parameter. A new key makes React create the component again from scratch.
function ItemRoute() {
const { itemId } = useParams();
return <ItemPage key={itemId} />;
}3Verify
Moving from /items/1 to /items/2 now logs unmounted and mounted.
# navigate in the browser and watch the consoleConfirmed by react.dev
Cause 2 of 3
An Effect loads data once and ignores the parameter
1Check
An Effect has to list every reactive value it reads. A route parameter from useParams is one.
grep -rn "useParams\|useEffect" src/pages2Fix
Read the parameter and put it in the dependency list so the Effect runs again when it changes.
const { teamId } = useParams();
useEffect(() => {
loadTeam(teamId);
}, [teamId]);3Verify
Changing the URL triggers a new load.
# navigate and watch the network tabConfirmed by react.dev
Cause 3 of 3
The parameter is not read from the route at all
1Check
A dynamic segment starting with a colon is read with useParams. Without it the component cannot notice the change.
grep -rn "path=.*:" src/2Fix
Declare the segment in the route and read it with useParams.
<Route path="teams/:teamId" element={<Team />} />
const { teamId } = useParams();3Verify
teamId changes when the URL changes.
console.log(teamId);Confirmed by reactrouter.com
How do I stop it coming back?
- Key a page component by the id it shows when it holds local form or scroll state.
- List every route parameter an Effect uses in its dependencies.
- Keep one route per resource and let the parameter carry the id.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Clicking a React Router Link changes the URL but the new page stays blank
The parent layout renders, the child is missing
The URL change proves the Link works and the router matched a route.
3 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix