Redux -Entwickler -Tools Aktualisierung, Konsole oder Status nicht aktualisiert
Posted: 13 Apr 2025, 00:19
In meinem nächsten.js -Projekt, bei dem ich Redux verwende, führe ich die Addtocart -Aktion mit gebrauchten Spispatch aus und er erreicht den Reduzierer, aber der Zustand wird nicht aktualisiert. Alt = "Konsole" src = "https://i.sstatic.net/0kqmaguc.png"/>
Sie können die Codes direkt dort überprüfen
I tried to change the code more than once, I thought that the action of the code did not trigger this situation because I use micro front-end, but the action comes as shown in the screenshot.
Strangely, even if I add initial state, it does not show it in the log
I am open to all kinds of ideas :/
Sie können die Codes direkt dort überprüfen
Code: Select all
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
const initialState: BasketState = {
items: [],
};
export const basketSlice = createSlice({
name: "basket",
initialState,
reducers: {
addToBasket: (
state,
action: PayloadAction
) => {
console.log("ACTION: ", action.payload);
const existingItem = state.items.find(
(item) => item.id === action.payload.id
);
console.log("EXISTING ITEM: ", existingItem);
if (existingItem) {
existingItem.quantity += 1;
} else {
state.items.push({ ...action.payload, quantity: 1 });
}
console.log("STATE: ", state.items);
},
removeFromBasket: (state, action: PayloadAction) => {
state.items = state.items.filter((item) => item.id !== action.payload);
},
updateQuantity: (
state,
action: PayloadAction
) => {
const item = state.items.find((item) => item.id === action.payload.id);
if (item) {
item.quantity = action.payload.quantity;
}
},
},
});
export const { addToBasket, removeFromBasket, updateQuantity } =
basketSlice.actions;
export default basketSlice.reducer;
Strangely, even if I add initial state, it does not show it in the log
I am open to all kinds of ideas :/