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

311
Views
How to save an object in redux?

I build an app in React with Redux and I try to send to my state an object and I try to save it in 'thisUser' but I don't know how to write that 'return' because mine doesn't work.

My Redux state:

const initialState = {
    thisUser: {}
}

export function usersReducer(state = initialState, action) {
    switch (action.type) {
        case 'users/addUser':
            return { ...state, thisUser: { ...state.thisUser, ...action.payload} }  //the problem
        default:
            return state
    }
}

Dispatch method:

dispatch({ type: "users/addUser", payload: new_user });

Can you tell me how to write that return, please?

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

0

If you want to append new user then why are you using object type. You should use Array Type thisUser.

const initialState = {
  thisUser: []
}

export function usersReducer(state = initialState, action) {
   switch (action.type) {
      case 'users/addUser':
          return { ...state, thisUser: [ ...state.thisUser,action.payload ] }  
      default:
          return state
  }
}

Or

If you want to save only single user object then change only that line in your code:

   return { ...state, thisUser: action.payload }  
about 4 years ago · Juan Pablo Isaza Report

0

It's better to use an array type for if you have a list of users .

If you have a case when you need to use an object just change the brackets [ ] on my code to curly braces { } .

const initialState = {
    thisUser: [],
}

export function usersReducer(state = initialState, action) {
    switch (action.type) {
        case 'users/addUser':
            return { ...state, thisUser: [ ...state.thisUser, ...action.payload]}
        default:
            return state
    }
}  
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!