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

274
Visualizações
How to destructure redux state in react redux?

I am not getting the point of using ...in ...state. When I have watched a tutorial of react redux they mentioned that it is destructuring of state variable but can you explain me the point mentioned .

export const productreducer=(state=initialstate,{type,payload})=>{

switch(type)
 case ActionTypes.SETPRODUCTS:
     return{...state,...payload}; //here I have a doubt
 default:
     return state
}
};
   
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

So {...} refers to spread or expand out the iterable elements, Lets take an example, you cannot change the state directly in the redux store and therefore you need to make a copy and append the new payload to the state and return the state.

state={ x:1, y:2 } payload={ y:5 , z:6 }

{...state, ...payload} // { x: 1, y: 5, z: 6 }

This will update the old state to the new state.

about 4 years ago · Juan Pablo Isaza Relatório

0

Generally, assume you have

const a = { foo: 1, bar: 2 }

then the following two are equivalent:

return { foo: a.foo, bar: b.bar }

and

return { ...a }

so essentially, ... copies over all the elements from the old object into a new object.

That said, modern Redux doesn't do this. Modern Redux also does not do switch..case reducers, ACTION_TYPES or connect. You might have been following a very outdated tutorial.

I would really recommend you to follow the official Redux Essentials tutorial that will teach you modern Redux from the beginning - it's much safer against accidental errors and only a fourth of the code.

about 4 years ago · Juan Pablo Isaza Relatório

0

It's Spread syntax. In your case, it gets all key: value pairs from objects and combines them into a new object. Hope with snippet it will be more clear

const state = {a: 1, b: 2}
const payload = {c: 3, d: 4}

const result = {...state, ...payload}
console.log(result)

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