📜  Usestate 回调 - 任何代码示例

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

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