Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
Should I store the state of a large object in React?

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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Maybe you should try Redux to manage state and use a store to manage the state of game.

https://react-redux.js.org/

about 4 years ago · Juan Pablo Isaza Report

0

Yes, your current approach could well be pretty inefficient if you copy the whole object:

const nextState = game.copy();

If the game is that large, making such a deep copy frequently could be an issue. But, for this particular situation, there's a simple solution: don't copy the whole thing, only copy the objects that need to be changed. There are only two objects that need to be changed (plus the new final outer state object), so only change those, instead of recursively copying everything.

function playerDamage(dmg) {
  setGame({
    ...game,
    player: {
      ...game.player,
      health: game.player.health - dmg
    }
  });
}

This version of the playerDamage function would be quite cheap.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!