useeffect only first render code example
Example 1: react run useeffect only once
useEffect(() => {
}, []);
Example 2: react useeffect not on first render
import React, { useEffect, useRef } from 'react';
const useDidMountEffect = (func, deps) => {
const didMount = useRef(false);
useEffect(() => {
if (didMount.current) func();
else didMount.current = true;
}, deps);
}
export default useDidMountEffect;
Example 3: useeffect only on mount
import React, { useEffect } from 'react';
function App() {
useEffect(() => {
}, []);
}