Here in this example I simply call useState
with an initializer function:
import React, { useState } from "react";
import "./styles.css";
export default function App() {
const [data, setData] = useState(() => {
console.log('Getting initial state...');
return {};
});
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
As you can see from console, useState
function is called twice. Is there any reason for that, it's the normal behavior?