自定义hooks实现在useState改变值之后立刻获取到最新的值

// 自定义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;

你可能感兴趣的:(前端,react,javascript,前端,react.js)