Я довольно новичок в React JS, и сейчас я работаю над проектом для школы. Это система подсчета очков для команд, всего 4 команды. В каждой команде 5 детей. Теперь я создал массив, чтобы иметь 5 отсчетов с идентификатором. 5 счетчиков (количество) предназначены для детей. Я использую React в Laravel.
Мой код:
export default class Gryffindor extends Component {
constructor(props) {
super(props);
this.state = { counters: [] };
this.fetchCounters = this.fetchCounters.bind(this);
this.counterIncrement = this.counterIncrement.bind(this);
this.counterDecrement = this.counterDecrement.bind(this);
}
componentDidMount() {
this.fetchCounters();
}
fetchCounters() {
const counters = [
{ id: 1, amount: 0 },
{ id: 2, amount: 0 },
{ id: 3, amount: 0 },
{ id: 4, amount: 0 },
{ id: 5, amount: 0 }
];
this.setState({ counters });
}
counterIncrement(e) {}
counterDecrement() {
if (counters.amount > 0) {
this.setState({ counters: amount - 5 });
}
}
render() {
return <CounterBody counters={this.state.counters} />;
}
}
const CounterBody = ({ counters }) => (
<table>
<thead>
<tr>
<th />
<th />
<th />
</tr>
</thead>
<tbody>
{counters.map(counter => (
<tr key={counter.id}>
<td>
<button onClick={this.counterDecrement}>Delete</button>
</td>
<td>{counter.amount}</td>
<td>
<button onClick={this.counterIncrement}>Add</button>
</td>
</tr>
))}
</tbody>
</table>
);
if (document.getElementById("Gryffindor")) {
ReactDOM.render(<Gryffindor />, document.getElementById("Gryffindor"));
}
Я пытаюсь обновить сумму в счетчиках, но по идентификатору.
Как видите, я пытался создать для него функции, но понятия не имею, что делать.
Может кто-нибудь помочь?