【发布时间】:2020-02-16 23:12:21
【问题描述】:
useConext 值在我附加到wheel 事件的回调中没有更新。我试图安慰但仍在打印静态值。但在回调之外,它正在打印更新的值
const Home = () => {
//accessing my context
var [appState, dispatch] = useContext(CTX);
//printing updated value here (working perfect here)
console.log(appState);
//my callback on wheel event (also using debouce to queue burst of events)
var fn = debounce(e => {
//incrementing value ++1
dispatch({ type: 'INCREMENT_COMPONENT_COUNTER' });
//printing static value here (problem here)
console.log(appState);
}, 500);
//setting and removing listener on component mount and unmount
useEffect(() => {
window.addEventListener('wheel', fn);
return () => {
window.removeEventListener('wheel', fn);
};
}, []);
};
【问题讨论】:
标签: javascript reactjs closures react-hooks