Based on @Piotr Siupa's answer, I find it more convenient to use it as a callback:
import { useRef } from 'react';
export default function useFirstRender(callback) {
const ref = useRef(true);
if (ref.current) {
callback();
}
ref.current = false;
}
In a component:
...
useFirstRender(() => {
console.log("first");
});
...