Optimistic Update
function SaveProduct() {
const { mutate, isWaiting } = useEditProductMutation();
const { getValues } = useFormContext();
return (
<button
disabled={isWaiting}
onClick={() => {
const payload = getValues();
const { revert, invalidate } = useProductQuery.optimisticUpdate({
key: { id: payload.id },
response: payload,
});
mutate(payload).then(({ response, error }) => {
if (error) {
revert();
}
invalidate();
});
}}
>
Save
</button>
);
}