I'm developing a board game and to test both players I have this piece of code in my componentDidMount:
window.swap = () => {
this.setState({
self: rotate(this.state.game[0].players, this.state.game[0].players.indexOf(this.state.self))[1]
})
}
This basically chooses the next player in the array of players and sets self to that so I can impersonate that player.
When I call swap the first time, it works fine, but when I call it again it it crashes the page. However, if I add a console.log before, like so:
window.swap = () => {
console.log('what?')
this.setState({
self: rotate(this.state.game[0].players, this.state.game[0].players.indexOf(this.state.self))[1]
})
}
it works fine every time. Even if I do a simple loop for(let i=0; i<100; i++) swap() it works fine, printing "what?" 100 times.
If I close DevTools and add something like this
setTimeout(() => {
window.swap()
window.swap()
}, 5000)
it works fine, with or without a console.log on swap. What?
EDIT:
Tested on:
- Chrome 70 (crash)
- Chrome Canary (72), Chromium, Firefox, Edge (ok)
So apparently this bug is fixed in next Chrome versions