I'm trying to understand the nuances of using something like a game loop inside of React (version 16+). I'm confused as to how React's rendering strategy conflicts (or doesn't conflict) with another rendering scheduler - in this case: request animation frame.
Refer to the following example where a game loop is used to set state:
class Loop extends Component {
constructor(props) {
super(props);
this.state = { x: 0 };
}
componentDidMount() {
let then = performance.now();
const loop = now => {
if (this.state.x < 400)
window.requestAnimationFrame(loop);
const dt = now - then;
then = now;
this.setState(prevState => ({ x: prevState.x + (dt * 0.1) }));
};
loop(then);
}
render() {
const { x } = this.state;
return <div style={{
backgroundColor: "green",
height: "50px",
width: `${x}px`
}}></div>;
}
}
Will this work similarly to if one had manipulated the DOM directly? Or, will react do something like batch state updates to render, defeating the purpose of using a request animation frame?
