Search code examples
reactjsreact-nativeredux

How to change status occupying redux in react native


You needed to change a variable, but this variable is in the store so I share the data

Data

export const lists =[
    {
    "id":1,
   "listname":"2022-09-09 00:00:00",
   "products":[
      {
         "id":1,
         "idlist":1,
         "product":"milk",
         "quantity":1,
         "measure":"liter",
         "status":false
      },
      {
         "id":2,
         "idlist":1,
         "product":"bread",
         "quantity":1,
         "measure":"Kg",
         "status":false
      },
  ]},
]

the data I want to change is status

I came up with this idea in the reduce, in the action the action is defined

case CHANGE_PRODUCT_STATUS:
       const newList = state.lists.products.findIndex(
         (product) => product.id === action.productId
       );
       if (newList === -1) return state;
       return {
         ...state,
         lists: state.lists.products[newList].status = !state.lists.products[newList].status,
       };

it gives me an error undefined is not an object (evaluating 'state.lists.proucts.findIndex')

how can i fix it thanks


Solution

  • There is no lists.products since lists is an array with one element inside. lists[someArrayIndexHere].products should work.