Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

273
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!