Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 从redux中间件清除间隔_Reactjs_Redux_React Redux - Fatal编程技术网

Reactjs 从redux中间件清除间隔

Reactjs 从redux中间件清除间隔,reactjs,redux,react-redux,Reactjs,Redux,React Redux,我的中间件看起来像 export const timermidleware=store=>next=>action=>{ 如果(action.type==“启动计时器”){ //控制台日志(“计时器”); action.interval=setInterval( ()=>store.dispatch({type:“TICK”,currentTime:Date.now()}), 1000 ); 调试器; }else if(action.type==“停止计时器”){ //控制台日志(存储); 调

我的中间件看起来像

export const timermidleware=store=>next=>action=>{
如果(action.type==“启动计时器”){
//控制台日志(“计时器”);
action.interval=setInterval(
()=>store.dispatch({type:“TICK”,currentTime:Date.now()}),
1000
);
调试器;
}else if(action.type==“停止计时器”){
//控制台日志(存储);
调试器;
console.log(action.interval);
clearInterval(action.interval);
}
下一步(行动);

};您可能不想改变操作。每次进入中间件时,您都会有一个新的操作,因此您的间隔将丢失。action.interval也不符合flux标准操作模式。你可以在你的中间件外面放一个

let interval;
const timerMiddleware ...
然后去

interval = setInterval // interval will now be a number which is a reference to your interval 
然后到了清理的时候

clearInterval(interval)
但是,如果您希望更不可变,那么您确实可以访问中间件中的存储

const interval = setInterval(...)
store.dispatch({
    type: "SET_YOUR_INTERVAL"
    payload: interval
});
然后在减速机中,您可以将其存储在
状态下

case "SET_YOUR_INTERVAL":
    return {
        someInterval: action.payload
    }
然后,当您回到中间件中,操作为“STOP_TIMER”时,您可以使用store.getState()获取对当前状态的引用。然后您可以转到clearInterval(currentState.someInterval)

然后store.dispatch您可以调度一个操作来清除状态中的间隔


有关setTimeout和setInterval返回值的更多信息:

您可能不想改变操作。每次进入中间件时,您都会有一个新的操作,因此您的间隔将丢失。action.interval也不符合flux标准操作模式。你可以在你的中间件外面放一个

let interval;
const timerMiddleware ...
然后去

interval = setInterval // interval will now be a number which is a reference to your interval 
然后到了清理的时候

clearInterval(interval)
但是,如果您希望更不可变,那么您确实可以访问中间件中的存储

const interval = setInterval(...)
store.dispatch({
    type: "SET_YOUR_INTERVAL"
    payload: interval
});
然后在减速机中,您可以将其存储在
状态下

case "SET_YOUR_INTERVAL":
    return {
        someInterval: action.payload
    }
然后,当您回到中间件中,操作为“STOP_TIMER”时,您可以使用store.getState()获取对当前状态的引用。然后您可以转到clearInterval(currentState.someInterval)

然后store.dispatch您可以调度一个操作来清除状态中的间隔

有关setTimeout和setInterval返回值的更多信息:

我只是在遵循这个。我只是在遵循这个。