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

116
Views
Redux adding new item to state

I have two cases in my reducer:

case GET_CODE:
    return {
      ...state,
      list:[...state.list,{code: action.payload.product.Code}]
    } 
case WEIGHT:
      return {
        ...state,
        list:[...state.list,{weight:action.payload}]
      }  

My state is looking like this:

   list: [
          { weight: ''},
          {weight: '100'},
          {weight: '200'},      
          {code: '63'},
          {code: '64'}   
         ]

What i would like to have is something like:

   list: [
          {weight: ''},
          {weight: '100', code:'63'},   
          {weight: '200', code:'64'}  
        ]

My initial state is list:[] . First empty value of weight is from initial render i think.I dispatch first WEIGHT then GET_CODE action.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should write some sort of helper function that creates the object for you, or the code/weight case should find the object in list it should correspond to.

For the helper function:

const createObject = (code, weight) => {
  const obj = {code: code.product.Code, weight: weight}
  dispatch({type: 'ADD_TO_LIST', payload: obj});
} 

// In your reducer
case ADD_TO_LIST:
  return {
    ...state,
    list: [
      ...state.list,
      action.payload
    ]
  }

Or just use ADD_TO_LIST case and create the object in the reducer

i.e.

case ADD_TO_LIST:
  return {
    ...state,
    list: [
      ...state.list,
      {code: action.payload.code.product.Code, weight: action.payload.weight}
    ]
  }
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!