// Filename – App.js
import React, { useState } from ‘react’;
function App() {
const [click, setClick] = useState([]);
**const addNumber = () => {
setClick([
...click,
{
id: click.length,
value: Math.random() * 10
}**
]);
};
return (
<div>
<ul>
{click.map(item => (
** <li key={item.id}>{item.value}</li>**
))}
</ul>
<button onClick={addNumber}>
Click me
</button>
</div>
);
}
export default App;
I am trying to the understand the bold lines of code, that what it it trying to do in the usestate() example. Can someone please help me understand the code in bold. I have taken this example from “https://www.geeksforgeeks.org/reactjs-usestate-hook/?ref=lbp”.
New contributor
IshaAlok is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.