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

154
Views
¿Cómo actualizar un objeto anidado creado por EntityAdapter redux?

Usé redux createEntityAdapter() para administrar mi estado que creé con createSlice() , el problema es que mi estado está anidado y no sé cómo actualizarlo

así es como se ve mi estado

 {ids: Array(1), entities: {…}, status: 'idle', error: null} entities: aLPii2RQz4IWitooG4lqB: burger: ingredients: Array(6) 0: {title: 'bread-tops'} 1: {title: 'cheese', Qty: 0} 2: {title: 'meatsss', Qty: 0} 3: {title: 'salad', Qty: 0} 4: {title: 'bacon', Qty: 0} 5: {title: 'bread-bottom'} length: 6 [[Prototype]]: Array(0) [[Prototype]]: Object id: "aLPii2RQz4IWitooG4lqB" [[Prototype]]: Object [[Prototype]]: Object error: null ids: ['aLPii2RQz4IWitooG4lqB'] status: "idle" [[Prototype]]: Object

por ejemplo, me gustaría aumentar mi cantidad de queso en Ingredientes

 burgerAdapter.updateOne(state, { id, changes: { burger: { ingredients: { } } }, });

No sé cómo debo recorrer los ingredientes Array para encontrar queso en la propiedad "updateOne"

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

0

Los métodos CRUD updateOne y updateMany solo realizan fusiones superficiales. Entonces, si desea actualizar un campo anidado más profundo, tiene dos opciones:

  • Cree primero toda la matriz de ingredients nuevos
  • No use los métodos CRUD aquí: escriba la lógica de actualización como una actualización de estado "mutante" normal basada en Immer

Yo optaría por el segundo enfoque, personalmente. Ya tiene el ID del elemento y solo hay una cosa que debe actualizarse. Entonces, esto debería funcionar:

 ingredientAdded(state, action) { const {id, ingredientName} = action.payload; const item = state.entities[id]; if (item) { const ingredient = item.ingredients.find(ingredient => ingredient.title === ingredientName); if (ingredient) { ingredient.Qty += 1; } } }
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!