简体   繁体   English

使用Redux的错误消息提示(Redux-Saga)

[英]Error message prompt using Redux(Redux-Saga)

In the current project, I am using React and Redux-Saga. 在当前项目中,我正在使用React和Redux-Saga。

  • APIs will called in actions and effect. API将调用操作和效果。
  • Data, errors and loading status will be stored for each page as below. 每个页面的数据,错误和加载状态将如下存储。

{ page1: {data: {}, error: null, loading: false}, page2: {data: {}, error: null, loading: false} }

  • When API is to be called, update store with {data: {}, error: null, loading: true} . 调用API时,请使用{data: {}, error: null, loading: true}更新存储。
  • After successful call, update store with {data: {// data here}, error: null, loading: false} . 成功调用后,使用{data: {// data here}, error: null, loading: false}更新存储{data: {// data here}, error: null, loading: false}
  • After bad request, update store with {data: {}, error: 'error', loading: false} . 提出错误请求后,请使用{data: {}, error: 'error', loading: false}更新存储。

In the page, in the function of componentWillReceiveProps , if error is not null, the page will prompt error message. 在页面中,在componentWillReceiveProps函数中,如果error不为null,则页面将提示错误消息。

However, it causes a problem that -- if I go to another page and then go back, as the error in store will not change or be reset, the page will prompt error before the API is called. 但是,这会导致一个问题-如果我转到另一个页面然后返回,因为存储中的错误不会更改或重置,该页面将在调用API之前提示错误。

Apparently, it is not the right way. 显然,这不是正确的方法。

I get over this by 2 ways: 我通过两种方法克服了这个问题:

  1. Clear the store as {data: {}, error: null, loading: false} . 将存储清除为{data: {}, error: null, loading: false}
  2. In componentWillReceiveProps , because error will only be stored after bad request, check if prev error == [] and next error to prompt or not. componentWillReceiveProps ,由于错误只会在错误请求之后存储,因此请检查prev error == []和是否提示下一个错误。

I am new developer and I have no idea that if they are right ways. 我是新开发人员,我不知道它们是否正确。

Could you guys give me some helps!! 你们能给我一些帮助吗! Thanks a lot! 非常感谢!

I think the good way is to clear the error , when component will unmount. 我认为,当组件卸下时,清除error的好方法。

componentWillUnmount() {
    // call action to clear error
}

For more information about componentWillUnmount: https://reactjs.org/docs/react-component.html#componentwillunmount 有关componentWillUnmount的更多信息: https : //reactjs.org/docs/react-component.html#componentwillunmount

I would suggest moving errors to a different branch of redux state. 我建议将错误移至redux状态的另一个分支。 I would organise the state as 我会组织国家为

state = {
  data: {
    page1: {}, page2: {}, page3: {}
  },
  error: null,
  loading: false,
}

Now, Whenever you change to a new page, reset the error to null and set it when error occurs. 现在,无论何时切换到新页面,都将错误重置为null并在发生错误时进行设置。 This will simplify the process. 这将简化过程。

If you do not want to change the way redux state is organised, then 如果您不想更改redux状态的组织方式,则

componentWillUnmount() {
   // Dispatch action to reset the error
}

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

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