How can I handle errors for useMutation
so the page doesn't turn into an unpleasant Unhandled Rejection (Error)
?
While this post here describes my problem, I still couldn't solve it. The post especially mentions. Instead of having to write the above boilerplate yourself, you can just use the provided result object.
I did that with:
const [signUp, { loading: mutationLoading, error: mutationError } [...]
However, my React App always switches to a not nice "Unhandled Rejection (Error): GraphQL error" screen when an error occurs. A similar solution seems to work in the official doc – but not for me.
function SignUp() {
// Declare a new state variable, which we'll call "count"
const [login, setLogin] = useState(true);
const [username, setUsername] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [name, setName] = useState("");
const [
signUp,
{ loading: mutationLoading, error: mutationError }
] = useMutation(SIGNUP_MUTATION, {
onCompleted(data) {
const { token } = login ? data.tokenAuth : data.createUser;
localStorage.setItem(AUTH_TOKEN, token);
console.log(token);
// this.props.history.push(`/`);
}
});
return (
<div>
{mutationLoading && <p>Loading...</p>}
{mutationError && (
<p>
Some error occured :(
{console.log(mutationError)}
{mutationError.graphQLErrors.map(({ message }, i) => (
<span key={i}>{message}</span>
))}
</p>
)}
[...]