Reactjs 为什么我的新字符串值在React.js中没有定义?

Reactjs 为什么我的新字符串值在React.js中没有定义?,reactjs,undefined,Reactjs,Undefined,我正在React.js中构建一个购物车,我让所有东西都正常工作。除了价格、描述、标题和图像等现有字符串值之外,我还想添加SKU值。在我的cartReducer.js文件中,我有以下内容: import Item1 from './images/apple_gala.jpg'; import Item2 from './images/apple_greenleaf.jpg' import Item3 from './images/apple_granny.jpg' import Item4 fro

我正在React.js中构建一个购物车,我让所有东西都正常工作。除了价格、描述、标题和图像等现有字符串值之外,我还想添加SKU值。在我的
cartReducer.js
文件中,我有以下内容:

import Item1 from './images/apple_gala.jpg';
import Item2 from './images/apple_greenleaf.jpg'
import Item3 from './images/apple_granny.jpg'
import Item4 from './images/apple_pinklady.jpg'
import { ADD_TO_CART, REMOVE_ITEM } from '../actions/action-types/cart-actions.js'

const initState = {
    items: [
        {id:1,title:'Royal Gala', desc: "Gala apples have a blush of pink in their skin. It’s dense, sweet juicy flesh makes it ideal for eating fresh.", price:7,SKU:A,img:Item1},
        {id:2,title:'Greenleaf', desc: "Our own original produce, the Greenleaf Original apple is award winning for the crispness of it's skin.",price:8,SKU:B,img: Item2},
        {id:3,title:'Granny Smith', desc: "Our French Granny Smith apples have a distinct sharp taste – perfect for eating fresh or using in cooking.", price:5,SKU:C,img: Item3},
        {id:4,title:'Pink Lady', desc: "A distinct and refreshing flavour with uniquely pink colouring - it can only be our Pink Lady apples.", price:5,SKU:D,img: Item4},
    ],
    addedItems:[],
    total: 0

}
const cartReducer= (state = initState,action)=>{

    if(action.type === ADD_TO_CART){
          let addedItem = state.items.find(item=> item.id === action.id)

         let existed_item= state.addedItems.find(item=> action.id === item.id)
         if(existed_item)
         {
            addedItem.quantity += 1 
             return{
                ...state,
                 total: state.total + addedItem.price 
                  }
        }
         else{
            addedItem.quantity = 1;

            let newTotal = state.total + addedItem.price 

            return{
                ...state,
                addedItems: [...state.addedItems, addedItem],
                total : newTotal
            }

        }
    }
    if(action.type === REMOVE_ITEM){
        let itemToRemove= state.addedItems.find(item=> action.id === item.id)
        let new_items = state.addedItems.filter(item=> action.id !== item.id)

        let newTotal = state.total - (itemToRemove.price * itemToRemove.quantity )
        console.log(itemToRemove)
        return{
            ...state,
            addedItems: new_items,
            total: newTotal
        }
    }

  else{
    return state
    }

}

export default cartReducer
在项目数组中,我有4个项目,最右边是新添加的SKU值,简单地说就是ABCD。当我保存文件时,我得到一个编译失败的错误,因为a、B、C和D都未定义。奇怪的是,其他字符串值也没有在任何地方定义,那么为什么SKU会失败呢


任何帮助都将不胜感激

您在哪里定义了A、B、C变量?您在这里使用它们作为变量,而不是字符串。您需要将它们放在引号中,使其成为字符串文字

{id:1,标题:'Royal Gala',desc:“Gala苹果的果皮呈粉红色。它的果肉浓密、甜美多汁,非常适合新鲜食用。”,价格:7,SKU:“a”,img:Item1},

这就是为什么您只在SKU上出现此错误的原因

就好像我忘记了将值定义为字符串的引号一样,谢谢您帮我发现了这一点-在漫长的一天之后,一定是得到了代码大脑。当我可以的时候,我会标记为正确答案谢谢!没问题,这是经常发生的。试着密切关注你得到的错误,有时我会停止仔细阅读错误代码,它最终也会咬我。