import React from 'react' import axios from 'axios' // import { useQuery, useQueryCache, useMutation, QueryCache, ReactQueryCacheProvider, } from 'react-query' import { ReactQueryDevtools } from 'react-query-devtools' const queryCache = new QueryCache() export default function App() { return ( ) } function Example() { const cache = useQueryCache() const [intervalMs, setIntervalMs] = React.useState(1000) const [value, setValue] = React.useState('') const { status, data, error, isFetching } = useQuery( 'todos', async () => { const { data } = await axios.get('/api/data') return data }, { // Refetch the data every second refetchInterval: intervalMs, } ) const [mutateAddTodo] = useMutation( value => fetch(`/api/data?add=${value}`), { onSuccess: () => cache.invalidateQueries('todos'), } ) const [mutateClear] = useMutation(value => fetch(`/api/data?clear=1`), { onSuccess: () => cache.invalidateQueries('todos'), }) if (status === 'loading') return

Loading...

if (status === 'error') return Error: {error.message} return (

Auto Refetch with stale-time set to 1s)

This example is best experienced on your own machine, where you can open multiple tabs to the same localhost server and see your changes propagate between the two.

Todo List

{ ev.preventDefault() try { await mutateAddTodo(value) setValue('') } catch {} }} > setValue(ev.target.value)} />
) }