npm install
npm run dev # http://localhost:5173The profile picker from the TanStack Query videos, with the edit-city form from the useMutation video. Type a new city
for Alice (Mumbai), press Save: with onMutate the card says Mumbai at once, before the PATCH has even left, because
the mutation writes the expected result straight into the query cache (setQueryData) and keeps a snapshot of the old
data. Type Atlantis instead: the server refuses it (500), onError puts the snapshot back, and the card snaps back to
Pune. Either way onSettled invalidates the query, so the server's copy is what you end up with. Throttle the network in
DevTools (Slow 3G) to feel the difference between waiting for the server and not.
vite.config.jsserves a tiny API fromapi/users.json:GET /api/users/:id(Alice is delayed 2 s on purpose, everyone else 0.4 s) andPATCH /api/users/:id(0.6 s), which merges the JSON body into that user and answers with the updated user. A city ofAtlantisanswers500 "No such city". The changes live in memory: restarting the dev server puts everyone back (the file is never written).src/main.jsxcreates oneQueryClientand wraps the app inQueryClientProvider(plusReactQueryDevtools).src/versions/holds each step of the story (see its README).src/Profile.jsxis whichever version was copied in last (the finished app:onMutate+onError+onSettled).
Sources: Optimistic updates · Mutations · Query cancellation