reactjs - 是否可以在 Redux Store 中实现 setTimeout?
问题描述
我想知道是否可以在 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
or redux-thunk
。异步操作的示例redux-thunk
:
const storeUsers = () =>{
return dispatch =>{
setTimeOut(() => dispatch({type: 'SENDED_AFTER_3_SECONDS'}), 3000)
}
}
推荐阅读
- javascript - 检测浏览器是否本机支持将音频传递给输入标签
- python-3.x - 列表越界
- c++ - 在函数模板中,如何根据另一个参数确定一个参数的类型
- r - 循环以计算R中每个用户在一段时间内的事件
- c - 如何修复完美匹配和不完美匹配功能?
- flutter - 无法使用 ffmpeg 将在线 gif 转换为 mp4 视频
- python - 在 pyautogui 中同时按下键以使它们具有功能?
- php - Laminas\View\Renderer\PhpRenderer::render: 无法渲染模板
- r - 计算 R 中效果大小的所有可能配对比较
- java - 在 actionlistener 中更改布尔值