useSelector does not update the value after dispatch

Viewed 47

I do not understand, the value is dispatch but, useSelector does not update, it's only showing an empty value.

cart inside value

In the above image, the cart contains the value.

selector does not update

see this above image, console Array does not contain any values.

Code:


    const dispatch = useDispatch();
    const selectorCart = useSelector((state) => state.cart);
    const selectorLogin = useSelector((state) => state.login);

    function handleAddItemInCart(product) {
        let isProductAllReadyExit = true;
        for(let item of selectorCart) {
            if (product.id === item.id && product.title === item.title) {
                isProductAllReadyExit = false;
                break;
            }
        }

        if (isProductAllReadyExit) {
            dispatch(addItemInCart(product));
            console.log("2. Selector Cart Value : ", selectorCart);
            handleAddCartItemSave();
        }

cartslice


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

const cartSlice = createSlice({
    name: "cart",
    initialState: [],
    reducers : {
        addItemInCart : (state, action) => {
            state.push(action.payload);
        },
        removeItemInCart : (state, action) => {
            return state.product.filter(product => product.id !== action.payload && product.title !== action.payload.title);
        },
    },
});

export const {addItemInCart, removeItemInCart} = cartSlice.actions;
export default cartSlice.reducer; 

what is this mistake in the above's code, Please suggest and correct me. And please don't mark Duplicate question.

0 Answers
Related