Custom hook to check if it is a first render or not.
// /functions/useIsInitRender'
import { useRef, useEffect } from 'react'
export default function useIsInitRender() {
const isInitRender = useRef(true)
useEffect(() => { isInitRender.current = false }, [])
return isInitRender.current
}
import useIsInitRender from '/functions/useIsInitRender'
function Component() {
const [state, setState] = useState(0)
const isInitRender = useIsInitRender()
return (
<>
<div>First render? <b>{isFirstRender.toString()}</b></div>
<div>Counter: <b>{state}</b></div>
<button onClick={() => setState(state + 1)}>Plus +1</button>
</>
)
}