MCPcopy Create free account
hub / github.com/cosdensolutions/code / Demo

Function Demo

videos/long/react-query-tutorial/src/Demo.tsx:7–57  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5import TodoCard from "./components/TodoCard";
6
7export default function Demo() {
8 const queryClient = useQueryClient();
9
10 const [search, setSearch] = useState("");
11 const [title, setTitle] = useState("");
12
13 const { data: todos, isLoading } = useQuery({
14 queryKey: ["todos", { search }],
15 queryFn: () => fetchTodos(search),
16 staleTime: Infinity,
17 cacheTime: 0,
18 });
19
20 const { mutateAsync: addTodoMutation } = useMutation({
21 mutationFn: addTodo,
22 onSuccess: () => {
23 queryClient.invalidateQueries({ queryKey: ["todos"] });
24 },
25 });
26
27 if (isLoading) {
28 return <div>Loading...</div>;
29 }
30
31 return (
32 <div>
33 <div>
34 <input
35 type="text"
36 value={title}
37 onChange={(e) => setTitle(e.target.value)}
38 />
39 <button
40 onClick={async () => {
41 try {
42 await addTodoMutation({ title });
43 setTitle("");
44 } catch (e) {
45 console.log(e);
46 }
47 }}
48 >
49 Add Todo
50 </button>
51 </div>
52 {todos?.map((todo) => (
53 <TodoCard key={todo.id} todo={todo} />
54 ))}
55 </div>
56 );
57}

Callers

nothing calls this directly

Calls 1

fetchTodosFunction · 0.90

Tested by

no test coverage detected