Junior — Senior
The influence of useRef on class instance creation
livecode
Task condition
It is necessary to find out how many times console.log will be executed inside the constructor of the MyClass class if an object of this class is created using useRef, and the component re-renders each time the button is pressed.
import { useRef, useState } from "react";
class MyClass {
constructor(value: number) {
console.log(value);
}
}
export default function MyComponent() {
const myClassRef = useRef(new MyClass(new Date().getTime()));
const [, updateState] = useState<Symbol>(Symbol());
return (
<div>
<h1>Hello world</h1>
<button onClick={() => updateState(Symbol())}>
update state
</button>
</div>
);
}