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

232
Visualizações
Dispatch is working but it is not updating the state

I am working on a application which uses redux for state management. There, at a some condition, I want to update the state.

My initial state and reducer function looks like this:

import { createSlice } from '@reduxjs/toolkit';

const filterDataTemplate = {
  programId: '',
  year: '',
};

const initialState = {
   //some other state
  filterData: { ...filterDataTemplate },
};

const slice = createSlice({
  name: 'editFilterSlice',
  initialState: initialState,
  reducers: {    
    updateFilterProgramId: (state, action) => {
      state.filterData.programId = action.payload;
    },
    updateFilterYear: (state, action) => {
      state.filterData.year = action.payload;
    },
    
  },
});

export const {
  updateFilterYear,
  updateFilterProgramId, 
} = slice.actions;

export default slice.reducer;

So filter details containg year and programId is obtained with the help of this code:

const filterDetails = useAppSelector(
   (state) => state.locationsFilter.filterData
 );

Let's say I have filter data initially:

filterDetails: {year:2021, programId: "Ameria"}

And i want to have my new filter data to be

filterDetails: {year: "", programId: "Ameria"}

So for this what I am doing:

const handleDelete = (e) => {    
    e.preventDefault();
    if (//some condition) {
      console.log("delete is called");
      dispatch(updateFilterYear(''));
    } else {
      dispatch(updateFilterProgramId(''));      
    }
}

handleDelete function is getting called properly when I am clicking a button because I am getting value inside console.

But after running this code my filter data is not updating. I am not sure what I am doing wrong. Please help with this.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Action.payload is of object type. So You should reference action.payload.year. I hope this example will be of any use

 
 ​    ​setTodoDate​: ​{ 
 ​      ​reducer​: ​(​state​,​ ​action​: ​PayloadAction​<​TodoDate​>​)​ ​=>​ ​{ 
 ​        ​state​.​currentDate​ ​=​ ​action​!​.​payload​.​date​; 
 ​      ​}​, 
 ​      ​prepare​: ​(​value​)​ ​=>​ ​(​{ 
 ​        ​payload​: ​{​ ...​value​ ​}​, 
 ​      ​}​)​, 
 ​    ​}
about 4 years ago · Juan Pablo Isaza Relatório

0

I think the issue is because you are trying to mutate your state directly. This is bad practice, and Redux state (and more generally react) is intended to be immutable. Reducers should return a copy of the state, along with the updated values. Documentation linked below.

Redux Documentation

Try writing your reducers like the following

updateFilterYear: (state, action) => {
     return {
         ...state,
         filterData: {
            ...state.filterData,
            year: action.payload
        }
    },

updateFilterProgramId: (state, action) => {
      return {
         ...state,
         filterData: {
            ...state.filterData,
            programId: action.payload
        }
    },
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