我想知道是否可以在 Redux 存储中存储 setTimeout 并让 setTimeout 触发偶数。从我所学到的知识来看,动作是javascript对象,但我不确定它将如何与商店交互。
最初我是这样想的
const timer = setTimeout(() => {someFunc}, 1000);
行动:
const t = (timer) => {
return {
type:TIMER_START,
timer
}
};
还原剂:
tReducer = (state={timer:null}, action) => {
switch(action.type) {
case TIMER_START:
return {...state, timer: action.timer}
default:
return state;
}
};
这是在存储中实现超时的有效方法吗?如果是这样,是否可以取消超时?由于我们想要 Redux 中的纯函数,我不确定我们是否可以取消超时
操作必须是普通对象,没有副作用。要调度异步操作,您应该使用自定义中间件,如 redux-saga
或 redux-thunk
。使用redux-thunk
异步操作的示例:
const storeUsers = () =>{
return dispatch =>{
setTimeOut(() => dispatch({type: 'SENDED_AFTER_3_SECONDS'}), 3000)
}
}