Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

333
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda