📜  带有回调的 useState 钩子 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:36.484000             🧑  作者: Mango

代码示例2
const [state, setState] = useState(null);
const myCallbacksList = useRef([]);
const setStateWithCallback= (newState, callback) => {
  setState(state);
  if(callback) myCallbackList.current.push(callback)
}
useEffect(() => {
  myCallbacksList.current.forEach((callback) => callback())
  myCallbacksList.current = [];
}, [state]);
…
setStateWithCallback(newState, myCallback)