irychen / keepalive-for-react

React KeepAlive is a component that can cache the state of the component and reuse it when needed.
MIT License
162 stars 32 forks source link

The timer cannot be destroyed when refreshing the cache. #19

Closed skyliwq closed 4 days ago

skyliwq commented 4 days ago

我设置了定时器,在刷新 refresh 缓存后依旧执行,无法在 useEffect(() => { return clearInterval(intervalRef.current) }, []) 销毁

I set a timer, and it continues to execute after refreshing the cache. It cannot be destroyed in useEffect(() => { return clearInterval(intervalRef.current) }, []).

irychen commented 4 days ago

刷新之后useEffect重新执行设置新的定时器
After refreshing, useEffect will re-execute to set a new timer.

image
  useEffect(()=>{
      const interval = setInterval(()=>{
          console.log('interval');
      },1000);

      return ()=>{
          console.log('clear interval ---------------');
          clearInterval(interval);
      }
  },[])

考虑一下你的设置定时器逻辑,在什么情况下不用设置 Consider your logic for setting timers: when would you not need to set one.

skyliwq commented 4 days ago

谢谢,是我的疏忽