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

183
Views
¿Cómo agregar un nuevo objeto a un objeto anidado en reducer?

¿Cómo agregar un nuevo objeto a un objeto anidado en reducer? Tengo este objeto, el comportamiento actual es una nueva acción simplemente anula, la acción anterior, pero mi objetivo es agregar esa matriz al final. y, por favor, ¿alguna guía general sobre cómo tratar simplemente con el estado? Sé que el siguiente estado es inmutable, por lo tanto, debe copiarse con el operador de propagación, como puede ver en el código que copié dos veces, todavía debe faltar otra copia, pero no tengo idea de dónde experimenté pero no positivo. si agrego ...(state.item[action.grandParentId] ?? []), en [action.grandParentId] me acerco mucho, pero copia el objeto de destino en lugar de actualizarlo.

 //initial state items : {} // State Action const AddItem = (select, id, idparent, grandParentId, index) => { dispatch({ type: ADD_ITEM, ITEM: select.item, id: id, idparent: idparent, grandParentId: grandParentId, index: index, }); };

 // ADD ITEM case ADD_ITEM: return { ...state, Items: { ...state.Items, [action.grandParentId]: [ ...(state.exercises[action.grandParentId] ?? []), { [action.idparent]: [ ...(state.Items[action.idparent] ?? []), { index: action.index, id: action.id, item: action.item, }, ], }, ], }, };

ingrese la descripción de la imagen aquí

ingrese la descripción de la imagen aquí

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

0

Por lo que puedo decir, también necesita copiar superficialmente el "abuelo", pero en este caso, dado que está actualizando un elemento existente de estado intermedio, debe asignar la matriz a una nueva matriz, reemplazando el elemento con la identificación correspondiente.

 // ADD ITEM case ADD_ITEM: return { ...state, Items: { ...state.Items, [action.grandParentId]: (state.Items[action.grandParentId] ?? []).map( item => item[action.idparent] ? { ...item, [action.idparent]: [ ...(item[action.idparent] ?? []), { index: action.index, id: action.id, item: action.item, }, ], } : item ), }, };
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!