two `setParam()` calls made together overwrite each other
hallucinogenizer opened this issue · comments
Description
I have reproduced the issue in a codesandbox. Here it is.
The sandbox uses NextJS App Router.
I'm not sure if this issue belongs here or in the next-query-params
repository.
What the issue is and how to reproduce it in the sandbox
- In the sandbox, I have two buttons, each incrementing the value of one query param in the URL. We have 2 query params therefore.
- When you click
Button 1
, it does 2 things:- It increments the value of
param1
in the URL - It increments the value of
param1
in localStorage
- It increments the value of
- Now click on
Button 2
and increment the value ofparam2
in both URL as well as localStorage. Make sure it has a different value thanparam1
. - Now erase the params from the URL and re-visit the page (without any URL params).
- Since the params still exist in
localStorage
, they need to be restored fromlocalStorage
. For this, we have auseEffect
that gets the values of both params fromlocalStorage
and sets them in URL. - The problem is that even though both params exist in
localStorage
, only one is set in the URL (usually the second one). The second setParam call overwrites the first one.
Expected Behavior
- Even if two calls to setParam are made one after another, if they are modifying separate queryParams, one call should not overwrite the other.
Found the fix
If you add options: { enableBatching: true }
to the <QueryParamProvider />
, the issue gets fixed.