对状态变化做出错误的反应
React wrong result on the change of state
我不熟悉如何处理简单的电子商务应用程序。当它被添加多次时,我正在更改购物车中的数量,但它给出了错误的结果(正如我们在输出中看到的,我打印的 'quantity' 和对象中的不同,例如。之前:1 after:2 但在对象中它是 3)。我将不胜感激任何帮助。谢谢!!
这是我的 reducer.js
export const initialState = {
basket : [],
}
const reducer = (state, action) => {
switch(action.type){
case "ADD_TO_BASKET":
const newIndex = state.basket.findIndex((basketItem)=> basketItem.id==action.item.id)
if(newIndex >= 0){
const newBasket = [...state.basket];
console.log(newBasket);
console.log("quantity "+newBasket[newIndex].quantity);
newBasket[newIndex].quantity+=action.item.quantity;
console.log(newBasket);
console.log("quantity "+newBasket[newIndex].quantity);
return{
...state,
basket: [...newBasket]
}
}
return{
...state,
basket: [...state.basket ,action.item]
}
.
.
.
export default reducer;
这是我的 checkout.js:
import { useStateValue } from "./StateProvider"
function Checkout() {
const [{basket}, dispatch] = useStateValue();
return (
<div className='checkout'>
<div className="checkout__left">
<img src="https://images-na.ssl-images-amazon.com/images/G/02/UK_CCMP/TM/OCC_Amazon1._CB423492668_.jpg" alt="" className="checkout__ad" />
<div>
<h2 className='checkout__title'>
Your Shopping Basket
</h2>
{basket.map(item => (
// console.log("checkout product quantity: "+JSON.stringify(item.quantity)),
<CheckoutProduct
key={i++}
id = {item.id}
title = {item.title}
image = {item.image}
price = {item.price}
rating = {item.rating}
quantity = {item.quantity}
/>
))}
.
.
.
StateProvider.js:
import React, { createContext, useContext, useReducer } from 'react'
//prepares the data layer
export const StateContext = createContext();
//wrap our app and provide the data layer
export const StateProvider = ({ reducer, initialState, children }) => (
<StateContext.Provider value={useReducer(reducer, initialState)}>
{children}
</StateContext.Provider>
);
//pull infromation from the data layer
export const useStateValue = () => useContext(StateContext);
控制台输出:
-> [{…}]0: {---, quantity: 3}length: 1[[Prototype]]: Array(0)
-> quantity 1
->[{…}]0: {---, quantity: 3}---
-> quantity 2
请删除 index.js 文件中的 React.StrictMode 包装器。
严格模式是通过故意重复调用一些函数来完成的。
我不熟悉如何处理简单的电子商务应用程序。当它被添加多次时,我正在更改购物车中的数量,但它给出了错误的结果(正如我们在输出中看到的,我打印的 'quantity' 和对象中的不同,例如。之前:1 after:2 但在对象中它是 3)。我将不胜感激任何帮助。谢谢!!
这是我的 reducer.js
export const initialState = {
basket : [],
}
const reducer = (state, action) => {
switch(action.type){
case "ADD_TO_BASKET":
const newIndex = state.basket.findIndex((basketItem)=> basketItem.id==action.item.id)
if(newIndex >= 0){
const newBasket = [...state.basket];
console.log(newBasket);
console.log("quantity "+newBasket[newIndex].quantity);
newBasket[newIndex].quantity+=action.item.quantity;
console.log(newBasket);
console.log("quantity "+newBasket[newIndex].quantity);
return{
...state,
basket: [...newBasket]
}
}
return{
...state,
basket: [...state.basket ,action.item]
}
.
.
.
export default reducer;
这是我的 checkout.js:
import { useStateValue } from "./StateProvider"
function Checkout() {
const [{basket}, dispatch] = useStateValue();
return (
<div className='checkout'>
<div className="checkout__left">
<img src="https://images-na.ssl-images-amazon.com/images/G/02/UK_CCMP/TM/OCC_Amazon1._CB423492668_.jpg" alt="" className="checkout__ad" />
<div>
<h2 className='checkout__title'>
Your Shopping Basket
</h2>
{basket.map(item => (
// console.log("checkout product quantity: "+JSON.stringify(item.quantity)),
<CheckoutProduct
key={i++}
id = {item.id}
title = {item.title}
image = {item.image}
price = {item.price}
rating = {item.rating}
quantity = {item.quantity}
/>
))}
.
.
.
StateProvider.js:
import React, { createContext, useContext, useReducer } from 'react'
//prepares the data layer
export const StateContext = createContext();
//wrap our app and provide the data layer
export const StateProvider = ({ reducer, initialState, children }) => (
<StateContext.Provider value={useReducer(reducer, initialState)}>
{children}
</StateContext.Provider>
);
//pull infromation from the data layer
export const useStateValue = () => useContext(StateContext);
控制台输出:
-> [{…}]0: {---, quantity: 3}length: 1[[Prototype]]: Array(0)
-> quantity 1
->[{…}]0: {---, quantity: 3}---
-> quantity 2
请删除 index.js 文件中的 React.StrictMode 包装器。 严格模式是通过故意重复调用一些函数来完成的。