generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Cache the query string in useQueryStates (#631)
The cache key for testing if the query changed wasn't being updated in useQueryStates. This also revealed it being broken in next@14.0.3, where the history patching mechanism wasn't working. A fix has been added and will be removed in v2 (where support for those version ranges is dropped).
- Loading branch information
Showing
3 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/// <reference types="cypress" /> | ||
|
||
describe('Reproduction for issue #630', () => { | ||
it('works with useQueryState', () => { | ||
runTest('1') | ||
}) | ||
it('works with useQueryStates', () => { | ||
runTest('3') | ||
}) | ||
}) | ||
|
||
function runTest(sectionToTry) { | ||
cy.visit('/app/repro-630') | ||
cy.contains('#hydration-marker', 'hydrated').should('be.hidden') | ||
cy.get('#1-pre').should('have.text', '{"a":null,"b":null}') | ||
cy.get('#2-pre').should('have.text', '{"a":null,"b":null}') | ||
cy.get('#3-pre').should('have.text', '{"a":null,"b":null}') | ||
cy.get('#4-pre').should('have.text', '{"a":null,"b":null}') | ||
cy.get(`#${sectionToTry}-set`).click() | ||
cy.get('#1-pre').should('have.text', '{"a":"1","b":"2"}') | ||
cy.get('#2-pre').should('have.text', '{"a":"1","b":"2"}') | ||
cy.get('#3-pre').should('have.text', '{"a":"1","b":"2"}') | ||
cy.get('#4-pre').should('have.text', '{"a":"1","b":"2"}') | ||
cy.get(`#${sectionToTry}-clear`).click() | ||
cy.get('#1-pre').should('have.text', '{"a":null,"b":null}') | ||
cy.get('#2-pre').should('have.text', '{"a":null,"b":null}') | ||
cy.get('#3-pre').should('have.text', '{"a":null,"b":null}') | ||
cy.get('#4-pre').should('have.text', '{"a":null,"b":null}') | ||
cy.go('back') | ||
cy.get('#1-pre').should('have.text', '{"a":"1","b":"2"}') | ||
cy.get('#2-pre').should('have.text', '{"a":"1","b":"2"}') | ||
cy.get('#3-pre').should('have.text', '{"a":"1","b":"2"}') | ||
cy.get('#4-pre').should('have.text', '{"a":"1","b":"2"}') | ||
cy.go('back') | ||
cy.get('#1-pre').should('have.text', '{"a":null,"b":null}') | ||
cy.get('#2-pre').should('have.text', '{"a":null,"b":null}') | ||
cy.get('#3-pre').should('have.text', '{"a":null,"b":null}') | ||
cy.get('#4-pre').should('have.text', '{"a":null,"b":null}') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use client' | ||
|
||
import { parseAsString, useQueryState, useQueryStates } from 'nuqs' | ||
import { Suspense } from 'react' | ||
|
||
export default function Page() { | ||
return ( | ||
<Suspense> | ||
<Client id="1" /> | ||
<Client id="2" /> | ||
<Clients id="3" /> | ||
<Clients id="4" /> | ||
</Suspense> | ||
) | ||
} | ||
|
||
type ClientProps = { | ||
id: string | ||
} | ||
|
||
function Client({ id }: ClientProps) { | ||
const [a, setA] = useQueryState( | ||
'a', | ||
parseAsString.withOptions({ history: 'push' }) | ||
) | ||
const [b, setB] = useQueryState( | ||
'b', | ||
parseAsString.withOptions({ history: 'push' }) | ||
) | ||
|
||
return ( | ||
<> | ||
<p>useQueryState {id}</p> | ||
<pre id={`${id}-pre`}>{JSON.stringify({ a, b })}</pre> | ||
<button | ||
id={`${id}-set`} | ||
onClick={() => { | ||
setA('1') | ||
setB('2') | ||
}} | ||
> | ||
Set | ||
</button> | ||
<button | ||
id={`${id}-clear`} | ||
onClick={() => { | ||
setA(null) | ||
setB(null) | ||
}} | ||
> | ||
Clear | ||
</button> | ||
<hr /> | ||
</> | ||
) | ||
} | ||
|
||
function Clients({ id }: ClientProps) { | ||
const [params, setParams] = useQueryStates( | ||
{ | ||
a: parseAsString, | ||
b: parseAsString | ||
}, | ||
{ history: 'push' } | ||
) | ||
return ( | ||
<> | ||
<p>useQueryStates {id}</p> | ||
<pre id={`${id}-pre`}>{JSON.stringify(params)}</pre> | ||
<button id={`${id}-set`} onClick={() => setParams({ a: '1', b: '2' })}> | ||
Set | ||
</button> | ||
<button id={`${id}-clear`} onClick={() => setParams(null)}> | ||
Clear | ||
</button> | ||
<hr /> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters