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

239
Views
Updating state in Redux using Redux toolkit in NextJS based application

I am trying to make a cart based ecommerce website. I am using Redux and Redux Toolkit to simplify things. I am fairly new to this and my code is not working as expected.

I am trying to dispatch data to state but the state does not update. Redux Devtools show that data is being indeed dispatched to store, but the state remains the same.

Redux Devtools action

Redux devtools state

In the action, I am console logging the state items and I think the problem lies here which I can't understand.

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

let initialState = [
  {
    name: "",
    id: "",
    quantity: "",
    mode: "",
    bookType: "",
  },
];

const cartSlice = createSlice({
  name: "cart",
  initialState,
  reducers: {
    add: (state, action) => {
      return state.map((item) => {
        console.log(item);
        if (item.id !== action.payload.id) {
          return item;
        }

        return {
          ...item,
          added: true,
        };
      });
    },
    remove: (state, action) => {
      return state.map((item) => {
        if (item.id !== action.payload.id) {
          return item;
        }

        return {
          ...item,
          added: false,
        };
      });
    },
  },
});

export const { add, remove } = cartSlice.actions;
export const getCart = (state) => state.cart;

export default cartSlice.reducer;

Here is console output: Console output

This is how I am dispatching in my code:

function formSubmit(event) {
    event.preventDefault();
    const requiredCourse = courses[0];
    const dispatchItem = {
      name: requiredCourse.name,
      id: requiredCourse.id,
      quantity: 1,
      mode: mode,
      bookType: bookType,
    };
    if (userLoggedIn) {
      console.assert("About to dispatch");
      dispatch(add(dispatchItem));
      setTimeout(() => {
        Router.push("/cart");
      }, 2000);
    } else {
      openModal();
      setTimeout(() => {
        Router.push({
          pathname: "/user/signup",
          query: {
            redirect: requiredCourse.url,
          },
        });
      }, 6000);
    }
  }

Every help is really appreciated.

about 4 years ago · Juan Pablo Isaza
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!