();
const { data, refetch } = useGetOne('posts', { id: 1 });
const [update, { isLoading }] = useUpdate();
const handleClick = () => {
update(
'posts',
{
id: 1,
data: { title: 'Hello World' },
},
{
mutationMode: 'pessimistic',
onSuccess: () => setSuccess('success'),
}
);
};
return (
<>
- title
- {data?.title}
- author
- {data?.author}
{success && {success}
}
{isMutating !== 0 && mutating
}
>
);
};
export const ErrorCase = () => {
const posts = [{ id: 1, title: 'Hello', author: 'John Doe' }];
const dataProvider = {
getOne: (resource, params) => {
console.log('getOne', resource, params);
return Promise.resolve({
data: posts.find(p => p.id === params.id),
});
},
update: (resource, params) => {
console.log('update', resource, params);
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('something went wrong'));
}, 1000);
});
},
} as any;
return (
);
};
const ErrorCore = () => {
const isMutating = useIsMutating();
const [success, setSuccess] = useState();
const [error, setError] = useState();
const { data, refetch } = useGetOne('posts', { id: 1 });
const [update, { isLoading }] = useUpdate();
const handleClick = () => {
setError(undefined);
update(
'posts',
{
id: 1,
data: { title: 'Hello World' },
},
{
mutationMode: 'pessimistic',
onSuccess: () => setSuccess('success'),
onError: e => setError(e),
}
);
};
return (
<>
- title
- {data?.title}
- author
- {data?.author}
{error && {error.message}
}
{success && {success}
}
{isMutating !== 0 && mutating
}
>
);
};