Skip to content

Commit

Permalink
chore: Split throttling page
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 committed Oct 25, 2023
1 parent 06ece2c commit d1b5abf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 40 deletions.
23 changes: 23 additions & 0 deletions packages/playground/src/app/demos/throttling/client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use client'

import { parseAsString, useQueryState } from 'src/nuqs'

export function Client() {
const [q, setQ] = useQueryState(
'q',
parseAsString
.withOptions({ throttleMs: 350, shallow: false })
.withDefault('')
)

return (
<>
<input
value={q}
onChange={e => setQ(e.target.value)}
placeholder="Search"
/>
<p>Query: {q || <em>empty</em>}</p>
</>
)
}
45 changes: 5 additions & 40 deletions packages/playground/src/app/demos/throttling/page.tsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,14 @@
'use client'

import { parseAsInteger, useQueryState } from 'src/nuqs'
import { Suspense } from 'react'
import { Client } from './client'

export default function ThottlingDemoPage() {
const [a, setA] = useQueryState('a', parseAsInteger.withDefault(0))
const [b, setB] = useQueryState('b', parseAsInteger.withDefault(0))

return (
<>
<h1>Throttled counters</h1>
<p>Note: URL state updated are intentionally slowed down</p>
<nav style={{ display: 'flex', gap: '4px' }}>
<button
style={{ padding: '2px 12px' }}
onClick={() => {
console.debug('decrement')
setA(x => x - 1, { throttleMs: 1000 })
setB(x => x - 1, { throttleMs: 2000 })
}}
>
-
</button>
<button
style={{ padding: '2px 12px' }}
onClick={() => {
console.debug('increment')
setA(x => x + 1, { throttleMs: 2000 })
setB(x => x + 1, { throttleMs: 1000 })
}}
>
+
</button>
<button
style={{ padding: '2px 6px' }}
onClick={() => {
console.debug('clear')
setB(null)
setA(null, { throttleMs: 1000 })
}}
>
Reset
</button>
</nav>
<p>A: {a}</p>
<p>B: {b}</p>
<Suspense>
<Client />
</Suspense>
<p>
<a href="https://github.com/47ng/next-usequerystate/blob/next/src/app/demos/thottling/page.tsx">
Source on GitHub
Expand Down

0 comments on commit d1b5abf

Please sign in to comment.