简体   繁体   English

React Redux:捕获组件中的错误

[英]React Redux: Catching errors in the components

Quick question on react, is anything wrong with the code below? 快速反应问题,下面的代码有什么问题吗? I have done something like this before but did not feel comfortable about it, catching http errors inside the component rather than dispatching an FAILURE_ACTION, the reason I do this is to prevent me from having unnecessary states in my store, often times these states are only used once and won't make sense to live for the entire life time of the application. 我之前已经做过类似的事情,但是对此并不满意,因为它捕获了组件内部的HTTP错误,而不是分派FAILURE_ACTION,这样做的原因是为了防止我在商店中出现不必要的状态,通常这些状态只是一次使用,在整个应用程序生命周期中都没有意义。

Action 行动

const createAccount = data => dispatch => axios.get().then(() => {
  dispatch({ type: 'SIGNUP' });
})

Component 零件

export default class SignupForm extends Component {
   handleSubmit = () => {
      createAccount.catch(function (err) {
         this.setState({ error: err.message });
      });
   }

   render(){
      return (
          <form onSubmit={handleSubmit}>
            <span>{this.state.error}</span>
            //...fields
          </form>
      );
   }
}

Neither React nor Redux dictates that you should do anything in a certain way. React和Redux都没有规定您应该以某种方式进行任何操作。 They have best practises and recommendations ofcourse, but if the code works for you, then roll with it. 他们有最佳实践和课程建议,但是如果代码对您有用,那么请继续学习。

That said, generally it's a good idea to keep your components as simple as possible and to defer this kind of logic elsewhere. 也就是说,通常最好使组件尽可能简单,并在其他地方推迟这种逻辑。 It keeps your component clean and free from side-effects, which makes them more maintainable and easier to reason about. 它使您的组件保持清洁且没有副作用,这使它们更易于维护且更易于推理。 Thats one of the reasons why external state managers exist in the first place. 这就是外部状态管理器首先存在的原因之一。

In your example, you now have two paths that can be followed, one is in Redux, and the other carries on in your component. 在您的示例中,您现在可以遵循两条路径,一条在Redux中,另一条在您的组件中进行。 Which part of your code is responsible for handling errors? 您的代码的哪一部分负责处理错误? What error is caught in your component exactly? 您的组件中到底捕获了什么错误? Could it also be an error caused by a coding mistake in your reducer (as oposed to a response status from the axios call)? 也可能是减速器中的编码错误引起的错误(与axios调用的响应状态相对)? Do all components need to implement their own error handling? 所有组件都需要实现自己的错误处理吗?

Also, I would expect that createAccount is a function and that createAccount().catch can only work if that function return the axios promise. 另外,我希望createAccount是一个函数,并且仅当该函数返回axios promise时,createAccount()。catch才能工作。

If there is any other component that is interested in knowing if Sign up operation failed and that information needs to be persisted, shared by other components, or inspected by a developer in order to understand the program state, it should go in the Redux store. 如果还有其他组件希望了解注册操作是否失败,并且该信息需要保留,由其他组件共享或由开发人员检查以了解程序状态,则应将其放入Redux存储中。

But here in your case i think there is no need to externalize state to Redux and it perfectly make sense to use component's local state to deal with errors. 但是在您的情况下,我认为不需要将状态外部化为Redux,并且使用组件的本地状态来处理错误是很有意义的。

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

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