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

302
Views
¿Cómo guardar un objeto en redux?

Construyo una aplicación en React con Redux y trato de enviar a mi estado un objeto y trato de guardarlo en 'thisUser' pero no sé cómo escribir ese 'return' porque el mío no funciona.

Mi estado Redux:

 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 } }

Método de envío:

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

¿Puede decirme cómo escribir esa devolución, por favor?

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

0

Si desea agregar un nuevo usuario, ¿por qué está utilizando el tipo de objeto? Debe usar 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 } }

O

Si desea guardar solo un objeto de usuario único, cambie solo esa línea en su código:

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

0

Es mejor usar un tipo de matriz si tiene una lista de usuarios.

Si tiene un caso en el que necesita usar un objeto, simplemente cambie los corchetes [] en mi código a llaves {}.

 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!