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

242
Visualizações
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
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