📜  usestate 挂钩回调 - 任何代码示例

📅  最后修改于: 2022-03-11 14:59:17.185000             🧑  作者: Mango

代码示例4
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)