beautiful-react-hooks
Version:
A collection of beautiful (and hopefully useful) React hooks to speed-up your components and hooks development
19 lines (18 loc) • 669 B
JavaScript
import { useEffect, useRef } from 'react';
import isFunction from './shared/isFunction';
import createHandlerSetter from './factory/createHandlerSetter';
/**
* Returns a callback setter for a function to be performed when the component did mount.
*/
const useDidMount = (callback) => {
const mountRef = useRef(false);
const [handler, setHandler] = createHandlerSetter(callback);
useEffect(() => {
if (isFunction(handler === null || handler === void 0 ? void 0 : handler.current) && !mountRef.current) {
handler.current();
mountRef.current = true;
}
}, []);
return setHandler;
};
export default useDidMount;