📅  最后修改于: 2022-03-11 14:55:44.793000             🧑  作者: Mango
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)