Skip to content

[BUG] [v7] Location update is out of sync with the rendering cycle #12552

Closed
@KurtGokhan

Description

@KurtGokhan

I'm using React Router as a...

library

Reproduction

Visit https://stackblitz.com/edit/vitejs-vite-fxh9ikze?file=src%2FApp.tsx

Click Increment button and observe the console logs. The code detects if there is a state discrepancy, and logs to the console.

System Info

It is happening in v7 regardless of the OS or browser. Here is the envinfo for the Stackblitz example.

  System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    react-router: ^7.0.2 => 7.0.2 
    vite: ^6.0.3 => 6.0.3

Used Package Manager

npm

Expected Behavior

To be able to properly use the router as a state manager, the state must be in sync with the React rendering process. So when I call the setter of useSearchParams, it should update the search params in the next render cycle, similar to how useState works.

As a long time user of v5 and v6, I think this was working in all versions before v7. (Test it here)

Actual Behavior

It seems like LocationContext is updating one frame late. It may not be an issue specific to useSearchParams but maybe useLocation in general. I have tried flushSync and even awaiting the promise returned by the navigation callback, but the issue still happens.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions