{"id":1196,"date":"2025-05-21T11:27:20","date_gmt":"2025-05-21T11:27:20","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=1196"},"modified":"2026-02-05T12:06:13","modified_gmt":"2026-02-05T12:06:13","slug":"handle-optimistic-updates-in-react","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/handle-optimistic-updates-in-react\/","title":{"rendered":"How do you Handle Optimistic Updates in React When Updating a Remote DB?"},"content":{"rendered":"\n<p>Optimistic updates improve perceived performance by updating the UI before the server confirms the change. Libraries like React Query (TanStack Query) simplify this. Use the onMutate option in useMutation to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Cancel relevant ongoing queries.<\/li>\n\n\n\n<li>Snapshot the current state.<\/li>\n\n\n\n<li>Update the local cache optimistically.<\/li>\n\n\n\n<li>Return the snapshot.<\/li>\n\n\n\n<li>Use onError to roll back the cache using the snapshot if the mutation fails. Use onSettled to refetch\/invalidate queries ensuring eventual consistency.<\/li>\n<\/ul>\n\n\n\n<p>Using React Query\u2019s useMutation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const mutation = useMutation(updateItem, {\n  onMutate: async (newItem) => {\n    await queryClient.cancelQueries('items');\n    const previousData = queryClient.getQueryData('items');\n    queryClient.setQueryData('items', (old) => &#91;...old, newItem]);\n    return { previousData };\n  },\n  onError: (err, newItem, context) => {\n    queryClient.setQueryData('items', context.previousData);\n  },\n  onSettled: () => {\n    queryClient.invalidateQueries('items');\n  },\n});\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Optimistic updates improve perceived performance by updating the UI before the server confirms the change. Libraries like React Query (TanStack Query) simplify this. Use the onMutate option in useMutation to: Using React Query\u2019s useMutation:<\/p>\n","protected":false},"author":2,"featured_media":1200,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[6,3],"tags":[],"class_list":["post-1196","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react","category-web"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/comments?post=1196"}],"version-history":[{"count":6,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1196\/revisions"}],"predecessor-version":[{"id":1204,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1196\/revisions\/1204"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/1200"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=1196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=1196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=1196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}