// src/App.jsx import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { useState } from "react"; function TodoList() { const queryClient = useQueryClient(); const [text, setText] = useState(""); const { data: todos, isLoading } = useQuery({ queryKey: ["todos"], queryFn: () => fetch("/api/todos").then((r) => r.json()), }); const addTodoMutation = useMutation({ mutationFn: (newTodo) => fetch("/api/todos", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(newTodo), }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["todos"] }); }, }); function handleAdd() { addTodoMutation.mutate({ text }); setText(""); } if (isLoading) return
Loading...
; return (