这是redux片,我想使用本地存储添加产品到购物车,但它不工作在下一个js13应用程序,我用使用客户端方法也
import { createSlice } from "@reduxjs/toolkit";
import toast from "react-hot-toast";
const initialState = {
cartState: false,
cartItems: localStorage.getItem("cart")
? JSON.parse(localStorage.getItem("cart"))
: [], // Let Suppose Database
cartTotalAmount: 0,
cartTotalQantity: 0,
};
const CartSlice = createSlice({
initialState,
name: "cart",
reducers: {
setOpenCart: (state, action) => {
state.cartState = action.payload.cartState;
},
setCloseCart: (state, action) => {
state.cartState = action.payload.cartState;
},
setAddItemToCart: (state, action) => {
const itemIndex = state.cartItems.findIndex(
(item) => item.id === action.payload.id
);
if (itemIndex >= 0) {
state.cartItems[itemIndex].cartQuantity += 1;
toast.success(`Item QTY Increased`);
} else {
const temp = { ...action.payload, cartQuantity: 1 };
state.cartItems.push(temp);
toast.success(`${action.payload.title} added to Cart`);
}
localStorage.setItem("cart", JSON.stringify(state.cartItems));
},
setRemoveItemFromCart: (state, action) => {
const removeItem = state.cartItems.filter(
(item) => item.id !== action.payload.id
);
state.cartItems = removeItem;
localStorage.setItem("cart", JSON.stringify(state.cartItems));
toast.success(`${action.payload.title} Removed From Cart`);
},
setIncreaseItemQTY: (state, action) => {
const itemIndex = state.cartItems.findIndex(
(item) => item.id === action.payload.id
);
if (itemIndex >= 0) {
state.cartItems[itemIndex].cartQuantity += 1;
toast.success(`Item QTY Increased`);
}
localStorage.setItem("cart", JSON.stringify(state.cartItems));
},
setDecreaseItemQTY: (state, action) => {
const itemIndex = state.cartItems.findIndex(
(item) => item.id === action.payload.id
);
if (state.cartItems[itemIndex].cartQuantity > 1) {
state.cartItems[itemIndex].cartQuantity -= 1;
toast.success(`Item QTY Decreased`);
}
localStorage.setItem("cart", JSON.stringify(state.cartItems));
},
setClearCartItems: (state, action) => {
state.cartItems = [];
toast.success(`Cart Cleared`);
localStorage.setItem("cart", JSON.stringify(state.cartItems));
},
setGetTotals: (state, action) => {
let { totalAmount, totalQTY } = state.cartItems.reduce((cartTotal, cartItem)=> {
const { price, cartQuantity } = cartItem;
const totalPrice = price * cartQuantity;
cartTotal.totalAmount += totalPrice;
cartTotal.totalQTY += cartQuantity;
return cartTotal;
}, {
totalAmount: 0,
totalQTY: 0,
});
state.cartTotalAmount = totalAmount;
state.cartTotalQantity = totalQTY;
},
},
});
export const {
setOpenCart,
setCloseCart,
setAddItemToCart,
setRemoveItemFromCart,
setIncreaseItemQTY,
setDecreaseItemQTY,
setClearCartItems,
setGetTotals
} = CartSlice.actions;
export const selectCartState = (state) => state.cart.cartState;
export const selectCartItems = (state) => state.cart.cartItems;
export const selectTotalAmount = (state) => state.cart.cartTotalAmount;
export const selectTotalQTY = (state) => state.cart.cartTotalQantity;
export default CartSlice.reducer;
字符串
这是redux slice,我想使用本地存储将产品添加到购物车,但它在next.js 13应用程序上不起作用,我使用了使用客户端方法,也可以使用本地存储API
1条答案
按热度按时间mf98qq941#
即使是带有
use client
的组件,在第一次页面加载时也会呈现在服务器上。您必须将if (typeof window !== "undefined")
检查添加到服务器上不可用的所有内容。此外,确保在服务器上,您的存储永远不会更改-因为如果您将其作为
export const store =
类型的变量,服务器端,该存储将在SSRed的 * 所有 * 请求之间共享,因此如果您在其中添加数据,则数据会混淆。