react useEffect prevent first time 1

react useEffect prevent first time

import { useRef, useEffect } from 'react';

export function useFirstRender() {
  const firstRender = useRef(true);

  useEffect(() => {
    firstRender.current = false;
  }, []);

  return firstRender.current;
}

Here is what the above code is Doing:
1. We create a ref with useRef.
2. We set the ref’s current property to true.
3. We use useEffect to set the ref’s current property to false.
4. We return the ref’s current property.

Now, we can use this hook in our component:

Similar Posts