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

231
Views
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 answers
Answer question

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 Report

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