CryptoKG94 / MRT_DeFi

GNU General Public License v3.0
5 stars 0 forks source link

Why my dispatch won't change the state? "Redux Toolkit" #2

Open CryptoKG94 opened 1 year ago

CryptoKG94 commented 1 year ago

I'm practicing Redux and in my project when i dispatch an action i will see that in the dev tools but the state won't change and it will remain an empty Array. why is this happening ? i will appreciate a little help.

shopping cart.js `import { createSlice, createAction } from "@reduxjs/toolkit";

// Action Creator

export const itemAdd = createAction("products/itemAdded");

const slice = createSlice({ name: "shoppingCart", initialState: [], reducers: { itemAdded: (cart, action) => cart.push(action.payload), }, });

export const { itemAdded} = slice.actions; export default slice.reducer;`

Dispatch `import { useDispatch, useSelector } from "react-redux"; import { itemAdd } from "../../store/state/shoppingCart";

// It's an onClick Event Handler const handleAddItem = (item) => { dispatch(itemAdd(item)); };`

johnc746 commented 1 year ago

Because you are importing itemAdd

import { itemAdd } from "../../store/state/shoppingCart";

instead of itemAdded

import { itemAdded } from "../../store/state/shoppingCart";`

CryptoKG94 commented 1 year ago

Great! It works well. Thank you.