是否可以在 Redux Store 中实现 setTimeout?
Would it be possible to implement a setTimeout in the Redux Store?
我想知道是否可以在 Redux Store 中存储一个 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)
}
}
我想知道是否可以在 Redux Store 中存储一个 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)
}
}