State Management · Middle · Question 55 of 75

How does a component get the thunk's result or catch its error?

try {
  const user = await dispatch(saveUser(data)).unwrap();
  navigate(`/users/${user.id}`);
} catch (err) {
  showError(err.message);
}

Explanation

Without unwrap, the promise always resolves with the final action (fulfilled or rejected), which is easy to mishandle.

Further reading

Problem with a question

State Management · Middle · question 55

Test yourself

Take the State Management · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz