React项目出现TypeError:无法读取undefined的'id'属性求助
React应用报错:Cannot read properties of undefined (reading 'id')
控制台抛出以下错误:
index.tsx:23 Uncaught TypeError: Cannot read properties of undefined (reading 'id') at handleIncrease (index.tsx:23:22) at HTMLUnknownElement.callCallback2 (react-dom.development.js:4164:14) at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:16) at invokeGuardedCallback (react-dom.development.js:4277:31) at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:4291:25) at executeDispatch (react-dom.development.js:9041:3) at processDispatchQueueItemsInOrder (react-dom.development.js:9073:7) at processDispatchQueue (react-dom.development.js:9086:5) at dispatchEventsForPlugins (react-dom.development.js:9097:3) at react-dom.development.js:9288:12
我第一次来这里求助(平时自己搜资料就能解决)。我开发的一个简单React应用出现问题:点击按钮调用对应函数时触发上述错误。相关函数定义在Context文件中,理论上应该正常运行,但实际报错。我尝试修改coffee.id的类型,但没有效果,现寻求帮助。
相关代码如下:
interface CoffeeContextProps { coffee: CartItem } export function Checkout({ coffee }: CoffeeContextProps) { const {changeCartItemsQuantity, removeCartItem} = useCart() const {cartItems} = useContext(CartContext); function handleDecrease() { changeCartItemsQuantity(coffee.id, 'decrease') } function handleIncrease() { console.log(coffee.id); changeCartItemsQuantity(coffee.id, 'increase') } function handleRemovedItem () { removeCartItem(coffee.id) }
内容的提问来源于stack exchange,提问作者Isaac Andrade
相关产品推荐
相关产品推荐

