UNPKG

@crossed/primitive

Version:

A universal & performant styling library for React Native, Next.js & React

28 lines (27 loc) 836 B
import { useCallback, useEffect, useLayoutEffect, useRef } from "react"; function useEvent(callback) { return useGet(callback, defaultValue, true); } const defaultValue = () => { throw new Error("Cannot call an event handler while rendering."); }; const isClient = typeof window !== "undefined"; const useIsomorphicLayoutEffect = isClient ? useLayoutEffect : useEffect; function useGet(currentValue, initialValue, forwardToFunction) { const curRef = useRef(initialValue ?? currentValue); useIsomorphicLayoutEffect(() => { curRef.current = currentValue; }); return useCallback( forwardToFunction ? (...args) => { var _a; return (_a = curRef.current) == null ? void 0 : _a.apply(null, args); } : () => curRef.current, [] ); } export { useEvent, useGet }; //# sourceMappingURL=useEvent.js.map