useEffectAfterFirstRender.ts 519 B

123456789101112131415161718192021
  1. import {useEffect, useRef} from 'react';
  2. const useEffectAfterFirstRender = (
  3. cb: React.EffectCallback,
  4. deps: React.DependencyList
  5. ): void => {
  6. const firstRender = useRef<boolean>(true);
  7. useEffect(() => {
  8. if (firstRender.current) {
  9. firstRender.current = false;
  10. return;
  11. }
  12. cb();
  13. // Dependencies are explicitly managed and the deps warning is enabled for the custom hook.
  14. // eslint-disable-next-line react-hooks/exhaustive-deps
  15. }, deps);
  16. };
  17. export {useEffectAfterFirstRender};