简体   繁体   English

捕获 redux thunk

[英]Catch in redux thunk

can I use redux thunk and manage catch error in component?, is recommended?我可以使用 redux thunk 和管理组件中的捕获错误吗?推荐吗?

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

or I should run dispatch in catch of the actionCreator?或者我应该在 actionCreator 的 catch 中运行调度?

Thanks.谢谢。

If the error is caught, you could dispatch an action object instead of logging it or calling another handler.如果错误被捕获,您可以调度一个动作 object 而不是记录它或调用另一个处理程序。 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 }
     })
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM