useEffectAfterFirstRender.ts 392 B

123456789101112131415161718192021
  1. import {useEffect, useRef} from 'react';
  2. const NOOP: [] = [];
  3. const useEffectAfterFirstRender = (
  4. cb: React.EffectCallback,
  5. deps: React.DependencyList = NOOP
  6. ): void => {
  7. const firstRender = useRef<boolean>(true);
  8. useEffect(() => {
  9. if (firstRender.current) {
  10. firstRender.current = false;
  11. return;
  12. }
  13. cb();
  14. }, deps);
  15. };
  16. export {useEffectAfterFirstRender};