so I have this website that randomly chooses a Pokemon from a large json file and returns the value to the user. There is a button the user can press to re-roll.
constructor(props) {
super(props);
this.state = {
history: [],
pokemon: []
};
}
spinAgain() {
this.setState({history: this.state.pokemon})
console.log(this.state.history);
fetch('/api/pokemon')
.then(res => res.json())
.then(pokemon => this.setState({pokemon}, this.setState({history: pokemon}), () => console.log('Pokemon fetched...', pokemon)));
}
render() {
return (
<div>
<h2>Pokemon</h2>
<ul>
{this.state.pokemon.map(pokemon =>
<li key={pokemon.id}>{pokemon.name} {pokemon.galar_dex}</li>
)}
</ul>
<button onClick={() => this.spinAgain()}>Spin Again</button>
</div>
);
}
What I have not been able to figure out is how to push the Pokemon to the history state instead of changing the state over and over.