Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Navigating between two React Router paths that use one component does not reset it

ErrorThe URL changes but the form, scroll or counter keeps the old values

Short 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.

  • Applies to React 19, React Router 7
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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');
    }, []);
  2. 2Match what you see, then jump to its fix

    1. 1No unmount log when the path or parameter changesReact reuses the component at the same positionFix
    2. 2The fetch runs on the first visit only, and the dependency list is []An Effect loads data once and ignores the parameterFix
    3. 3The component uses a value captured at first render instead of useParamsThe parameter is not read from the route at allFix

What does the error look like?

  • The URL changes but the form, scroll or counter keeps the old values
  • Data from the first page stays when moving to /items/2
  • An 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 console

Confirmed by react.dev

Did this fix it?

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/pages

2Fix

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 tab

Confirmed by react.dev

Did this fix it?

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.