Ask Claude to Implement Optimistic UI Updates in React
Optimistic updates make your UI feel instant — the component updates before the server confirms, then rolls back on error. Getting this right without a library is tricky. Claude handles it cleanly.
I have a like button that calls POST /api/posts/:id/like. Implement optimistic updates so the count increments immediately, then syncs with the server response. Roll back the count and show a toast on error.
Claude generates a hook using useState and a rollback variable, wrapping the fetch in a try/catch with a clean UI revert:
const [likeCount, setLikeCount] = useState(initialCount);
const handleLike = async () => {
const previous = likeCount;
setLikeCount(c => c + 1); // optimistic update
try {
const { count } = await likePost(postId);
setLikeCount(count); // sync with server truth
} catch {
setLikeCount(previous); // roll back
toast.error("Couldn't save your like");
}
};
You can also ask Claude to implement the same pattern using React Query's onMutate / onError for mutation-level optimism — useful when multiple components share the same data:
Rewrite this with React Query's useMutation, using the cache update and rollback pattern with onMutate and onError.
Claude handles the queryClient.setQueryData / queryClient.cancelQueries dance correctly, which is easy to get wrong.
Don't wait for the server — update the UI first and reconcile later.
Log in to leave a comment.
Set up Claude Code as an automated reviewer in your CI pipeline — on every pull request, it reads the diff, checks for bugs, security issues, missing tests, and convention violations, then posts its findings as a PR comment. Your human reviewers get a head start because the obvious issues are already flagged before they look.
Before deploying, tell Claude to read your project — migrations, environment variables, queue workers, scheduled tasks, caching, third-party integrations — and generate a deployment checklist that's specific to your app. Not a generic "did you run migrations?" list, but one that knows YOUR infrastructure and catches the things YOUR deploy can break.
Instead of writing a README from memory or copying a template, tell Claude to read your project and generate one that's actually accurate — real setup instructions from your config, real architecture from your directory structure, real API examples from your routes, and real prerequisites from your dependency files.