Open History to see the last commit
1import React from 'react'
2import Button from '../components/button'
3import fetch from '../libs/fetch'
4β
5import { useQuery, useMutation } from 'react-query'
6β
7export default () => {
8 const [value, setValue] = React.useState('')
9β
10 const { data, isLoading } = useQuery('todos', () => fetch('/api/data'), {
11 // Refetch the data every second
12 refetchInterval: 1000,
13 })
14β
15 const [mutateAddTodo] = useMutation(
16 value => fetch(`/api/data?add=${value}`),
17 {
18 refetchQueries: ['todos'],
19 }
20 )
21β
22 const [mutateClear] = useMutation(value => fetch(`/api/data?clear=1`), {
23 refetchQueries: ['todos'],
24 })
25β
26 if (isLoading) return <h1>Loading...</h1>
27β
28 return (
29 <div>
30 <h1>Auto Refetch with stale-time set to 1s)</h1>
31 <h2>Todo List</h2>
32 <form
33 onSubmit={async ev => {
34 ev.preventDefault()
35 try {
36 await mutateAddTodo(value)
37 setValue('')
38 } catch {}
39 }}
40 >
41 <input
42 placeholder="enter something"
43 value={value}
44 onChange={ev => setValue(ev.target.value)}
45 />
46 </form>
47 <ul>
48 {data.map(item => (
49 <li key={item}>{item}</li>
50 ))}
51 </ul>
52 <Button onClick={mutateClear}>Clear All</Button>
53 </div>
54 )
55}