generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Update useQueryStates state cache when syncing useSearchParams (#…
…776) This fixes an issue where `<Link>` navigation (or external updates of the URL) kept an internal stale state ref and reused it when updating other search params.
- Loading branch information
Showing
3 changed files
with
64 additions
and
5 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,17 @@ | ||
/// <reference types="cypress" /> | ||
|
||
describe('repro-774', () => { | ||
it('updates internal state on navigation', () => { | ||
cy.visit('/app/repro-774') | ||
cy.contains('#hydration-marker', 'hydrated').should('be.hidden') | ||
cy.get('#trigger-a').click() | ||
cy.get('#value-a').should('have.text', 'a') | ||
cy.get('#value-b').should('be.empty') | ||
cy.get('#link').click() | ||
cy.get('#value-a').should('be.empty') | ||
cy.get('#value-b').should('be.empty') | ||
cy.get('#trigger-b').click() | ||
cy.get('#value-a').should('be.empty') | ||
cy.get('#value-b').should('have.text', 'b') | ||
}) | ||
}) |
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,41 @@ | ||
'use client' | ||
|
||
import Link from 'next/link' | ||
import { parseAsString, useQueryStates } from 'nuqs' | ||
import { Suspense } from 'react' | ||
|
||
export default function Home() { | ||
return ( | ||
<> | ||
<nav> | ||
<Link id="link" href="/app/repro-774"> | ||
Reset | ||
</Link> | ||
</nav> | ||
<Suspense> | ||
<Client /> | ||
</Suspense> | ||
</> | ||
) | ||
} | ||
|
||
const searchParams = { | ||
a: parseAsString.withDefault(''), | ||
b: parseAsString.withDefault('') | ||
} | ||
|
||
function Client() { | ||
const [{ a, b }, setSearchParams] = useQueryStates(searchParams) | ||
return ( | ||
<> | ||
<button onClick={() => setSearchParams({ a: 'a' })} id="trigger-a"> | ||
Set A | ||
</button> | ||
<button onClick={() => setSearchParams({ b: 'b' })} id="trigger-b"> | ||
Set B | ||
</button> | ||
<span id="value-a">{a}</span> | ||
<span id="value-b">{b}</span> | ||
</> | ||
) | ||
} |
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