// 自定义hooks实现在useState改变值之后立刻获取到最新的值
import { useEffect, useState, useCallback } from "react";
const useSyncCallback = (callback) => {
const [proxyState, setProxyState] = useState({ current: false });
const Func = useCallback(() => {
setProxyState({ current: true });
}, [proxyState]);
useEffect(() => {
if (proxyState.current === true) setProxyState({ current: false });
}, [proxyState]);
useEffect(() => {
proxyState.current && callback();
});
return Func;
};
export default useSyncCallback;
自定义hooks实现在useState改变值之后立刻获取到最新的值
最新推荐文章于 2024-07-17 16:12:58 发布