Это решение обновляет результаты матча на основе выбора пользователя.
ОДИНОЧНОЕ МАТЧА ПОЛУЧИТ ОДИН РЕЗУЛЬТАТ, т. Е. РЕЗУЛЬТАТЫ БУДУТ ПЕРЕЗАПИСАТЬ
Также для отслеживанияMatch Number, я использовал matchNumber
в качестве индекса.
Ваш getSelection
будет выглядеть примерно так.
getSelection = (val: object, matchNumber: number) => {
// 1. Make a shallow copy of the items
let bets = [...this.state.bets];
// 2. Make a shallow copy of the item you want to mutate
let bet = { ...bets[matchNumber] };
// 3. Replace the property you're intested in
bet = val;
// 4. Put it back into our array. N.B. we *are* mutating the array here, but that's why we made a copy first
bets[matchNumber] = bet;
// 5. Set the state to our new copy
this.setState({ bets }, () => console.log(this.state.bets));
Обновите фон:
<button
style={{ background: winner == home.name ? "lightblue" : "white" }}
onClick={() => this.props.getSelection(home, matchNumber)}
>
{home.name}
</button>
<button
style={{ background: winner == draw.name ? "lightblue" : "white" }}
onClick={() => this.props.getSelection(draw, matchNumber)}
>
{draw.name}
</button>
<button
style={{ background: winner == away.name ? "lightblue" : "white" }}
onClick={() => this.props.getSelection(away, matchNumber)}
>
{away.name}
</button>
Проверьте это рабочее решение.https://codesandbox.io/s/9lpnvx188y