For example, let's say we have to trigger a function when component mounts. For me, I have two options to doing it.
first one is using useRef()
hook.
import { useCallback } from "react";
export default function Test() {
const ref = useCallback((node) => {
if (node) {
console.log("mounts");
}
}, []);
return (
<main>
<div ref={ref}>TEST</div>
</main>
);
}
second one is using useEffect()
hook.
import { useEffect } from "react";
export default function Test() {
useEffect(() => {
console.log("mounts");
}, []);
return (
<main>
<div>TEST</div>
</main>
);
}
expected behaviors are showing 'mounts' in console when component mounts and both of them work properly.
The first one might not work if <div>
is somehow out of DOM but I'll suppose there are no conditional differences.
In this situation, Is there any possible disadvantages using either of them or differences?
From this question, I want to know about
- difference between two methods of displaying 'mounts' in console.
- possible disadvantage or advantage using either of them