import { useEffect, useState, useCallback } from 'react'; // 用于使用最新的state或者redux的值
// 使用方法:

/*
handleChange = () => {
  setCount(1)
  readCount()
}
const readCount = useSyncCallback(() => {
  console.log(count) // => 1
})
*/
// 全局参数

var params = null;

var useSyncCallback = function useSyncCallback(callback) {
  var _useState = useState({
    current: false
  }),
      proxyState = _useState[0],
      setProxyState = _useState[1];

  var memoizedFunc = useCallback(function () {
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    if (args) {
      params = args;
    } else {
      params = null;
    }

    setProxyState({
      current: true
    });
  }, [proxyState] // eslint-disable-line
  );
  useEffect(function () {
    if (proxyState.current === true) setProxyState({
      current: false
    });
  }, [proxyState]);
  useEffect(function () {
    proxyState.current && callback.apply(void 0, params || {});
  });
  return memoizedFunc;
};

export default useSyncCallback;