I'm quite new to react, and I have this problem that when trying to map an array to a list of React stateful component, all my components lose their state. My code:
const { useState, useEffect} = React;
const htmlRoot = document.getElementById('root');
const reactRoot = ReactDOM.createRoot(htmlRoot);
const App = () => {
const Item = (props) => {
const [count, setCount] = useState(0);
useEffect(() => {
const timer = setInterval(() => {
setCount((lastCount) => lastCount + 1);
}, 1000);
return () => clearInterval(timer);
}, []);
return <h1>{props.children + ' ' + count}</h1>;
};
const arr = [1, 2, 3];
const [arrState, setArrState] = useState(arr);
const clickHandler = () => {
setArrState((lastArrState) => [
...lastArrState,
lastArrState.at(-1) + 1,
]);
};
return (
// Unfortunately, Stack Snippets don't support the shorthand form
// of fragments
<React.Fragment>
{arrState.map((num, index) => (
<Item key={index}>{num}</Item>
))}
<button onClick={clickHandler}>Click Me</button>
</React.Fragment>
);
};
reactRoot.render(<App />);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.1.0/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.1.0/umd/react-dom.development.js"></script>
Is this even possible for them to keep their state? If it is, how? Update: