1import React, { useState } from "react";
2import ReactDOM from "react-dom";
3
4function Counter() {
5 const [count, setCount] = useState(0);
6
7 return (
8 <div>
9 <h1>{count}</h1>
10 <button onClick={() => setTimeout(() => setCount(count + 1), 2000)}>
11 Delayed Counter (basic)
12 </button>
13 <button onClick={() => setTimeout(() => setCount(x => x + 1), 2000)}>
14 Delayed Counter (functional)
15 </button>
16 <button onClick={() => setCount(count + 1)}>Immediate Counter</button>
17 </div>
18 );
19}
20
21const rootElement = document.getElementById("root");
22ReactDOM.render(<Counter />, rootElement);
23
1const [arrayOfObjs, handleObjSelection] = useState([]);
2
3// on a buttton for example
4<button
5 onClick={selectedObj => handleObjSelection(
6 prevSelected => [...prevSelected, selectedObj],
7 ))}
8>
9