examples/react/auto-refetching-nextjs-pages-router
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack React Query Auto Refetching Example (Next.js Pages Router)#
A todo list that polls the server with refetchInterval. Adding a todo or clearing the list invalidates the query.
Built with Next.js (Pages Router).
How it works#
src/services/todos/queries.tsdefinestodosQueries.list()withqueryOptions, andtodosMutations.create()andtodosMutations.deleteAll()withmutationOptions.src/pages/index.tsxpassestodosQueries.list()touseQuerywithrefetchInterval: intervalMs. The interval can be changed in a number input, and a dot lights up whileisFetchingistrue.- In
src/pages/index.tsx, both mutations run throughuseMutationand callqueryClient.invalidateQueries({ queryKey: todosQueries.all() })inonSuccess, so the list refetches after adding a todo or clearing the list. src/pages/api/data.tsis a Next.js API route that keeps the list in memory, adds an item with?add=, empties it with?clear=1, and responds after a 100 ms delay.
To run this example:
npm installnpm run dev