I've initialize a react app using npx create-react-app:
import React from 'react';
class Expensive {
constructor() {
console.log('constructing expensive class');
}
}
function App() {
const expensiveRef = React.useRef(new Expensive());
return (
<div className="App">
app
</div>
);
}
export default App;
However, I am seeing that constructing expensive class is printed to the console twice. Why is this?
I get the same result if I replace
const expensiveRef = React.useRef(new Expensive());
with
const [expensiveState, setExpensiveState] = React.useState(new Expensive());