import { useEffect, useRef } from "react";
export default function Test() {
const testRef = useRef(0);
useEffect(() => {
console.log(testRef);
testRef.current = 1;
console.log(testRef);
}, []);
return <div></div>;
}
log from this code is :
{ current : 0 }
{ current : 1 }
Which is expected and understandable. However, when I give useRef an object like this:
import { useEffect, useRef } from "react";
export default function Test() {
const testRef = useRef({
num: 0,
});
useEffect(() => {
console.log(testRef);
testRef.current.num = 1;
console.log(testRef);
}, []);
return <div></div>;
}
The console output is : { current : 1 } { current : 1 }
What is confusing me is how the first log prints 1 instead of 0. How does it log a state which is not yet updated ?