beautiful-react-hooks
Version:
A collection of beautiful (and hopefully useful) React hooks to speed-up your components and hooks development
18 lines (17 loc) • 613 B
JavaScript
import { useEffect, useRef } from 'react';
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 (handler && handler.current && typeof handler.current === 'function' && !mountRef.current) {
handler.current();
mountRef.current = true;
}
}, []);
return setHandler;
};
export default useDidMount;