While working on React today, I got stuck somewhere because my basic JavaScript knowledge is not good.
I want to change the value of the "name" property of any object in this array.
Codesandbox: https://codesandbox.io/s/dank-rain-udjcxe?file=/src/App.js
const data = [
{ id: 1, name: "jonas" },
{ id: 2, name: "mark" },
{ id: 3, name: "elon" },
];
My App.js file
const App = () => {
const [people, setPeople] = useState(data);
const changePerson = (id) => {
// if I click on the button, the "name" value of the current object will change to anything
};
return (
<main>
<section>
{people.map((person) => {
return (
<div className="card" key={person.id}>
{person.name}
<button onClick={() => changePerson(person.id)}>change</button>
</div>
);
})}
</section>
</main>
);
};
export default App;