Promise callback of Mutate function not being called
See original GitHub issueDescribe the bug
When using a Mutate
function with the PUT verb, the callback of the mutation promise is not called
To Reproduce Minimal Code example to reproduce
<Mutate
verb="PUT"
base={`https://jsonplaceholder.typicode.com/put/posts/1`}
>
{(update, { loading, error }) => {
console.log(loading);
console.log(error);
return (
<div
style={{ width: "100px", height: "100px" }}
onClick={() => {
console.log("Clicked");
update({
id: 1,
title: "foo",
body: "bar",
userId: 1
}).then(() => console.log("Then called"));
}}
>
Click
</div>
);
}}
</Mutate>
There are no throw errors and loading turns from false -> true -> false indicating an operation took place.
Not sure if the operation is being aborted early for some reason, or is throwing an error that is not being caught by the Mutate function.
NOTE: You can use the .finally
method on the promise but it is unclear if this is intended behavior in the docs
Expected behavior
According to the docs, “Each mutation returns a promise…”, which to me indicates that I should be allowed to use the .then
function on the returned promise.
Screenshots N/A
Desktop (please complete the following information):
- OS: Windows 10 1803
- Browser: Firefox
- Version: 66.0.5 (64-bit)
Additional Note
Even the docs are using the .then
function on the mutate function so I believe this is definitely a bug
See here
onClick={() => delete(movie.id).then(() => dispatch('DELETED'))}
Also a side note, delete
is a reserved word in JS and that line will result in a compile error
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Indeed the error is not correctly propagated in
Mutate
(https://github.com/contiamo/restful-react/blob/master/src/Mutate.tsx#L181), the trick is to add a.catch
statement. In your snippet you just have a 404 error for example. I’ve spotted this bug yesterday during myuseMutate
implementation but you was quicker than me for reporting 😉https://codesandbox.io/embed/competent-lalande-z3jsk?fontsize=14
Good catch for the example in the documentation 😅 using
delete
is quite stupid ^^Time to add a new unit test! Thanks for the feedback 💯