Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
Two state variables change but only want to re-render once

https://codesandbox.io/s/ancient-rain-19d4rb?file=/src/App.js

I created a sandbox above to demonstrate the problem that I am having. Please play with it and look at the code to see what it's doing.

enter image description here The left side has selections for the 'gamemode' and for each gamemode there exists options 'options' on the right side.

The API call requires both of these values to be inputted.

Whenever I change the option on the right, it should do an API call in Game.js If I change the gamemode on the left, it should also automatically change the option on the right to be the 0th index. With my current logic, it does two API calls whenever I change the gamemode since it's also changing the option. (Look at the console when changing modes)

How can I change the design or how can I adjust things to get the result I'm looking for?

Let me know if further clarification is needed.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is that you have two pieces of state that depend on each other, being observed independently by another object causing unnecessary useEffect calls. One way to solve this is to create another state object that combines both gamemode and option and have Game.js observe this object only. This way, you can add your own custom logic to dictate exactly when the API should be called.

Here's a sandbox with the solution implemented: https://codesandbox.io/s/dazzling-galileo-sj29fh?file=/src/App.js

The first useEffect depends on option and immediately updates the game object when changed. The second useEffect depends on gamemode and first checks if the option needs to be updated to the 0th index, otherwise it updates the game directly.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda