可以在减速器状态上设置计时器吗? React + Redux

时间:2019-02-20 10:04:48

标签: javascript reactjs redux

是否可以设置计时器以清除某些减速器状态?我有一个状态来显示“成功”,“错误”,“警告”消息

示例:

减速器:

const statusState = {
   status: {action: '', result: ''}
}

...

CASE fetchContent:
    return {
      ...state,
      contents: [...state.contents, action.data],
      status: {
        ...state.status,
        action: action.type,
        result: action.result
      }
    }

组件:

render(){
   cost { status } = this.props

   if(status.action == "something" && status.result == "success"){
      alert("success");
   }
}

const mapStateToProps = store => ({
       status: store.initialState.status
   });

   export default connect(mapStateToProps)(Component);

如果我不清除status.action和status.result警报将一直显示,是否可以设置计时器清除它?

1 个答案:

答案 0 :(得分:2)

创建一个清除动作,以清除status.action和status.result。创建一个javscript超时功能,该功能在超时后调用清除动作。如下所示:

 clearStatus = ()=>{
       setTimeout(
        function() {
            //create a action that clears status and result
        },3000
       );
    }   

     render(){
           cost { status } = this.props

           if(status.action == "something" && status.result == "success"){
             //showMessage 
             clearStatus();
           }
        }