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

169
Visualizações
Is it better to use multiple states or one state object in React?

If I had a functional component which required use and manipulation of multiple state items, and I had multiple states going for different things that arent directly related (such as game statistics and UI element states) would it be better performance (or practice) wise to do it like this:

const [state, setState] = React.useState({
    username: 'JoeSchmoe200',
    points: 200,
    isHoveringOverItem: {item1: false, item2: false, item3: false},
    selectedItem: {item1: true, item2: false, item3: false}
})

(where everything held in state is in one object and set with one method), or like this:

const [username, setUsername] = React.useState('JoeSchmoe200')
const [points, setPoints] = React.useState(200)
const [isHoveringOverItem, setIsHoveringOverItem] = React.useState(
    {item1: false, item2: false, item3: false}
)
const [selectedItem, setSelectedItem] = React.useState(
    {item1: true, item2: false, item3: false}
)

where each state is declared individually and set individually. I'm just trying to spark a conversation here to learn more about React.

What do you think about this performance-wise? Readability-wise? Is it a matter of preference or is there an objective best practice?

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

0

Prior to React 18, it was recommended to group state variables that were expected to change together. React used to batch state updates but only those directly within React events such as onClick, onChange, etc. and lifecycle methods such as useEffect. Multiple state updates within async methods e.g. fetch() weren't batched.

However with React 18, all state updates occurring together are automatically batched into a single render. This means it is okay to split the state into as many separate variables as you like.

Source: https://reactjs.org/blog/2022/03/29/react-v18.html#new-feature-automatic-batching

Still, it is a good practice IMO to keep related pieces together for better code readability. But state objects should be kept at a manageable size otherwise useReducer() should be used.

about 4 years ago · Juan Pablo Isaza Relatório

0

With React 18, state updates are automatically batched so you don't really need to mash everything together in a single state object. Read the section on Automatic Batching here. Knowing that, here are some tips I got from This Article

  • Keep your state as flat as possible. Nested objects are unruly to manage and can introduce re-rendering bugs.
  • Not everything needs to be a state object.
  • Use succinct and descriptive names.
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