Nico
Nico

Reputation: 843

Catch in redux thunk

can I use redux thunk and manage catch error in component?, is recommended ?

const handleSubmit = (values) => {
  try {
     await dispatch(postLogin(values));
  } catch (err) {
    setError(true);
  }
}

or I should run dispatch in catch of the actionCreator ?

Thanks.

Upvotes: 0

Views: 910

Answers (1)

akhilD
akhilD

Reputation: 681

If the error is caught, you could dispatch an action object instead of logging it or calling another handler. It helps, in case you want to display the error message anywhere in your component.

const handleSubmit = (values) => {
  try {
     await dispatch(postLogin(values));
  } catch (err) {
     dispatch({
       type: LOGIN_ERROR,
       payload: { err }
     })
  }
}

Upvotes: 1

Related Questions