Navigation resets query state #893
-
Hi, I use latest nuqs version with react router adapter (nuqs/adapters/react-router/v7). I have some queries for my table (tanstack table). When I navigate, the url changes but the navigation does not finish immediately, therefore for a short time the table rerenders without any query values. Example: In contacts view there is a table with search query state (/contacts?search=something) This happens even when the javascript of contact view is loaded. I don't remember this being happened in react router v6. Any ideas how to solve this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Are you navigating with a I'd say the nuqs part behaves as expected: the URL changes, so the state follows. The root of the issue is that the URL changes before the target page is ready/rendered. IIRC, Next.js solved this by rendering the target page in the background (and prefetching). I realise that's not much help in React Router, but there may be some pointers there. |
Beta Was this translation helpful? Give feedback.
Yeah that's what I thought. React Router core isn't really reactive on (shallow) URL updates, and so neither is useSearchParams, which only updates once the navigation has ended. Nuqs needs to have this shallow update as it's the default behaviour (and we expose
useOptimisticSearchParams
to get the same reactive values nuqs uses internally). But that's the issue here: nuqs reacts to URL changes too quickly (as it should if the URL is supposed to be the source of truth, React Router is lagging behind here).One trick I can think of is to cache the current search state (in a useRef), update it along the nuqs …