Search code examples
javascriptreduxredux-toolkit

How to mutate state with redux toolkit


The data is provided dynamically and I don't know its value to assign it in initialState. It causes me a bug that I can't deal with.

How to update the object in the state if there was no object in initialState?

ERROR

filteringSlice.ts:12 

Uncaught TypeError: Cannot read properties of undefined (reading 'products')
    at addFilter (filteringSlice.ts:12:1)
    at createReducer.ts:280:1
    at produce (immerClass.ts:94:1)
    at createReducer.ts:279:1
    at Array.reduce (<anonymous>)
    at reducer (createReducer.ts:246:1)
    at reducer (createSlice.ts:325:1)
    at combination (redux.js:560:1)
    at k (<anonymous>:2235:16)
    at D (<anonymous>:2251:13)

CALL ACTION

onChange={(selectedValue) => {
  dispatch(
    addFilter({
      products: { category__name: { filter: selectedValue } },
    })
  );
}}

SLICE

import { createSlice } from "@reduxjs/toolkit";
const initialState = {} as any;
const filteringSlice = createSlice({
  name: "filtering",
  initialState,
  reducers: {
    addFilter: (state, action) => {
      const key = Object.keys(action.payload)[0];
      const key2 = Object.keys(action.payload[key])[0];
      const values = Object.values(action.payload[key])[0];
      //@ts-ignore
      state.filters[key][key2] = { ...state.filters[key][key2], ...values };
    },
  },
});
const { reducer, actions } = filteringSlice;
export const { addFilter } = actions;
export default reducer;

Solution

  • So your state is an empty object at first:

    const initialState = {} as any;
    

    But then you're accessing at as if it had a more deeply nested structure:

    state.filters[key][key2] = ...
    

    That doesn't work because there is no state['filters'], and no state['filters']['products'], etc.

    You need to create every level of this nesting manually for this to work (or think about a better, flatter structure for your state):

    /*
    action.payload = {
      products: {
        category__name: {
          filter: selectedValue
        }
      }
    }
    */
    const key = Object.keys(action.payload)[0]; // 'products'
    const key2 = Object.keys(action.payload[key])[0]; // 'category__name'
    const values = Object.values(action.payload[key])[0]; // selectedValue
    
    if (!state.filters) {
      state.filters = {};
    }
    if (!state.filters[key]) {
      state.filters[key] = {}; 
    }
    if (!state.filters[key][key2]) {
        state.filters[key][key2] = {};
    }
    
    state.filters[key][key2] = { ...state.filters[key][key2], ...values };