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

172
Visualizações
Is it okay to recreate class component state behavior using useReducer?

so in react hooks we may have multiple states in our component:

const [stateA, setStateA] = useState(0);
const [stateB, setStateB] = useState('Hello World');
const [stateC, setStateC] = useState(true);

but there is a case that we need to update multiple states at once. let say when clicking event:

const handleClick = () => {
  setStateA(prev => prev+1);
  setStateB('John Doe');
  setStateC(false);
}

As we can see I need to update these states 1 by 1. So to update all states at once and ensure all updates in the same batch, I tried making this reducer:

const stateReducer => (prev, next) {
  switch (typeof next) {
    case 'object':
      return { ...prev, ...next };
    case 'function':
      return { ...prev, ...next(prev) };
    default:
      return prev;
  }

...

const [state, setState] = useReducer(stateReducer, {
  stateA: 0,
  stateB: 'Hello World',
  stateC: true
});

...

// just set single state
setState({stateA: 0});

// set multiple state
setState({
  stateA: 5,
  stateB: 'John Doe'
});

// set multiple state with prev state
setState(prev => ({
  stateA: prev.stateA +1,
  stateB: 'John Doe',
  stateC: !prev.stateC,
}));

It's works! but how it gonna affect the performace? or this is okay?

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

0

Your performance will not be affected.

In the note section of the page Functional updates in the react hooks reference they post useReducer as an alternative for useState when we need to manage different state variables.

Another option is useReducer, which is more suited for managing state objects that contain multiple sub-values.

The performance key does not reside in the usage of useState or useReducer itself, it's more about why do you need to use it, and as they say in the useReducer documentation you are doing a good usage of it simplifying the way you handle this 3 state variables.

useReducer is usually preferable to useState when you have complex state logic that involves multiple sub-values or when the next state depends on the previous one.

Not 100% sure about this but I think useState use useReducer internally.

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