Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

230
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda