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

327
Views
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;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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 };
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!