zarm
Version:
基于 React 的移动端UI库
14 lines • 496 B
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import { useCallback, useEffect, useRef } from 'react';
export default function useEventCallback(fn, dependencies) {
var ref = useRef(function () {
throw new Error('Cannot call an event handler while rendering.');
});
useEffect(function () {
ref.current = fn;
}, [fn].concat(_toConsumableArray(dependencies)));
return useCallback(function () {
var _fn = ref.current;
return _fn();
}, [ref]);
}