// Is React.StrictMode is supposed to run the code twice updating any state twice initially. If not so then why am I having such a problem?
// index.js
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
// App.js
import { useEffect, useState } from "react";
const App = () => {
const [arr, setArr] = useState([]);
useEffect(() => {
setArr((prev) => [...prev, 1]);
}, []);
return (
<div>
{arr.map((value) => {
return <p>{value}</p>;
})}
</div>
);
};
export default App;
// Expected output in console: [1]
// but getting: [1,1]
// THIS MEANS THAT STRICT MODE IS UPDATING arr FROM []->[1]->[1,1], BUT IT SHOULD ONLY RUN ONCE AT INITIAL RENDER. AND WHEN I RUN WITHOUT THE StrictMode IT GIVES ONLY [1] AS OUTPUT, WHICH IS THE EXPECTED OUTPUT.