yxcoder

[React] 求助: react hooks zustand setTimeout

  •  
  •   yxcoder · Jun 8, 2023 · 1044 views
    This topic created in 1082 days ago, the information mentioned may be changed or developed.

    zustand store 中存储了一个 timer 保存计时器

    dataMap[id] = {
        ...
        timer
        ...
    }
    
    
    useBus(
        type, 
        ()=>{
        	...
        	updateTime()
            ...
        }
    )
    
    updateTime = (id) => {
        console.log(dataMap[id].timer)      --------- ( 1 )
        clearTimeout(dataMap[id].timer)
        const timer = setTimeout(()=>{
        	...
        }, 1000)
        store.updateDataMap(id, timer)
    }
    
    store.updateDataMap: (id, timer) => set((data)=>{
    	data[id].timer = timer
        return data
    })
    

    发现每次( 1 )处打印的都是第一次的 timer, 后面的 updateDataMap 似乎没有起作用

    求助

    问题出在哪里?

    Supplement 1  ·  Jun 8, 2023
    发现在 setTimeout 中更新 store 会有问题
    1 replies    2023-06-08 16:41:23 +08:00
    FrankFang128
        1
    FrankFang128  
       Jun 8, 2023
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5335 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 08:19 · PVG 16:19 · LAX 01:19 · JFK 04:19
    ♥ Do have faith in what you're doing.