I am pretty new to react and can't wrap my head around how to go about doing this.
So I have my state as follows: Main.js
import React, { Component } from "react";
import Users from "./Users";
class App extends Component {
state = {
users: [
{ id: 0, name: "a", count: 0 },
{ id: 1, name: "b", count: 0 },
{ id: 2, name: "c", count: 0 },
],
};
increment = (id) => {
this.setState({
// No idea how to interface this
});
};
render() {
return (
<div className="App container">
<h1 className="center blue-text">Counter</h1>
<Users users={this.state.users} increment={this.increment} />
</div>
);
}
}
export default App;
Users.js
import React from "react";
const Users = ({ users, increment }) => {
const userList = users.map((user) => {
return (
<div className="collection-item" key={user.id}>
<p>
{user.name} : {user.count}{" "}
</p>
<button onClick={() => increment(user.count)}>+</button>
</div>
);
});
return <div className="users collection">{userList}</div>;
};
export default Users;
How do I go about updating the count value via function, I can't figure out how to use this.setState to update the count while its nested. Any ideas? Thank you for any guidance.