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

228
Visualizações
React performance of nested state

I'm reading a lot that of posts on here that recommend against using deeply nested state objects in React.

However, do these downsides apply to a state of a single-level object? Is there any performance difference between these two examples?

Will the first example cause rerenders just as much as the second? Does it even matter at such a small scale like this?

const [example, setExample] = useState({
    group1property1: '',
    group1property2: '',
    group2property1: '',
    group2property2: '',
});
const [example2, setExample2] = useState({
    group1: {
        property1: '',
        property2: '',
    },
    group2: {
        property1: '',
        property2: '',
    }
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Is there any performance difference between these two examples?

No. When the state atom is reassigned (and you should already know you must not just internally modify an object/array state atom), the component is updated.

// not good; will not cause update since identity of `example` doesn't change
example.group1property1 = 8;
setExample(example);
// good; example is shallow-copied and updated
setExample(example => ({...example, group1property1: 8}));

Will the first example cause rerenders just as much as the second?

Yes, since you will need to shallow-copy the outer state atom to have React pick up changes in an inner object anyway. It's just that the updating of a deep object gets a bit tedious unless you use something like immer.

// not good; will not cause update, etc. etc.
example.group1.property1 = 8;
setExample(example);
// good; example is shallow-copied, as is group1
setExample(example => ({...example, group1: {...example.group1, ...property1: 8}}));

Does it even matter at such a small scale like this?

Probably not.

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