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]]: Objectpor 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"
Los métodos CRUD updateOne y updateMany solo realizan fusiones superficiales. Entonces, si desea actualizar un campo anidado más profundo, tiene dos opciones:
ingredients nuevosYo 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; } } }