import { useState } from 'react'
import ReactDOM from 'react-dom/client'
import { useAsyncDebouncer } from '@tanstack/react-pacer/async-debouncer'
import { PacerProvider } from '@tanstack/react-pacer/provider'
interface SearchResult {
id: number
title: string
}
const fakeApi = async (term: string): Promise<Array<SearchResult>> => {
await new Promise((resolve) => setTimeout(resolve, 1500))
return [
{ id: 1, title: `${term} result ${Math.floor(Math.random() * 100)}` },
{ id: 2, title: `${term} result ${Math.floor(Math.random() * 100)}` },
{ id: 3, title: `${term} result ${Math.floor(Math.random() * 100)}` },
]
}
function App() {
const [searchTerm, setSearchTerm] = useState('')
const [results, setResults] = useState<Array<SearchResult>>([])
const handleSearch = async (term: string) => {
if (!term) {
setResults([])
return
}
const data = await fakeApi(term)
setResults(data)
return data
}
const asyncDebouncer = useAsyncDebouncer(
handleSearch,
{
wait: 500,
onError: (error) => {
console.error('Search failed:', error)
setResults([])
},
asyncRetryerOptions: {
maxAttempts: 3,
maxExecutionTime: 1000,
},
},
)
const handleSearchDebounced = asyncDebouncer.maybeExecute
async function onSearchChange(e: React.ChangeEvent<HTMLInputElement>) {
const newTerm = e.target.value
setSearchTerm(newTerm)
const result = await handleSearchDebounced(newTerm)
console.log('result', result)
}
return (
<div>
<h1>TanStack Pacer useAsyncDebouncer Example</h1>
<div>
<input
autoFocus
type="search"
value={searchTerm}
onChange={onSearchChange}
placeholder="Type to search..."
style={{ width: '100%' }}
autoComplete="new-password"
/>
</div>
<div style={{ marginTop: '10px' }}>
<button onClick={() => asyncDebouncer.flush()}>Flush</button>
</div>
<asyncDebouncer.Subscribe
selector={(state) => ({
isExecuting: state.isExecuting,
isPending: state.isPending,
successCount: state.successCount,
})}
>
{({ isExecuting, isPending, successCount }) => (
<div>
<p>API calls made: {successCount}</p>
{results.length > 0 && (
<ul>
{results.map((item) => (
<li key={item.id}>{item.title}</li>
))}
</ul>
)}
{isPending && <p>Pending...</p>}
{isExecuting && <p>Executing...</p>}
</div>
)}
</asyncDebouncer.Subscribe>
<asyncDebouncer.Subscribe selector={(state) => state}>
{(state) => (
<pre style={{ marginTop: '20px' }}>
{JSON.stringify(state, null, 2)}
</pre>
)}
</asyncDebouncer.Subscribe>
</div>
)
}
const root = ReactDOM.createRoot(document.getElementById('root')!)
function renderApp(mounted: boolean) {
root.render(
mounted ? (
<PacerProvider
// defaultOptions={{
>
<App />
</PacerProvider>
) : null,
)
}
let mounted = true
renderApp(mounted)
document.addEventListener('keydown', (e) => {
if (e.shiftKey && e.key === 'Enter') {
mounted = !mounted
renderApp(mounted)
}
})