I have this very large Game
object that completely determines the state of the Game, including data that gets rendered and data that does not get rendered. Its nested values are very frequently changed.
Should I store this very large object in a state...
const [game, setGame] = useState(new Game()) // Very large Game object
and use setGame
with each little update to the game? For example,
function playerDamage(dmg) {
const nextState = game.copy();
nextState.player.health -= dmg;
setGame(nextState);
}
It seems to me that it would be extremely inefficient to make a copy of Game and use setGame
with every little change to Game. Am I correct in this assumption, or does React and TS/JS handle this well "under the hood"?
I guess to rephrase this question: what's the best way to frequently update deeply nested values in a React state object?