我正在嘗試創(chuàng)建一個(gè)添加到購(gòu)物車的功能,該功能從頁(yè)面中獲取產(chǎn)品并將屬性添加到數(shù)組中。我的功能看起來(lái)像這樣addToCart({ title, desc, price, image }) { // This is what is returning undefined this.setState((state) => ({ cart: [ ...this.state.cart, { title, desc, price, image, }, ], })); }我的物品看起來(lái)像:<Card key={title} style={{ width: '18rem'}}> <Card.Img variant="top" src={imageUrl}width='10rem' height='180rem' /> <Card.Body> <Card.Title>{title}</Card.Title> <Card.Text> {desc} <br /> {price} </Card.Text> <Button variant="primary" onClick={e => this.handleAddToCart(e, this.state.items)} >Add to cart</Button> </Card.Body></Card>我很困惑為什么它要放置一個(gè)看起來(lái)像這樣的數(shù)組0:desc: undefinedimage: undefinedprice: undefinedtitle: undefined這是我在代碼沙盒上的完整代碼: https ://codesandbox.io/s/unruffled-water-2rvjk?file=/src/App.js我提前感謝您抽出時(shí)間來(lái)幫助我。
設(shè)置狀態(tài)返回未定義,無(wú)法解構(gòu)“未定義”的屬性“標(biāo)題”,因?yàn)樗炊x
精慕HU
2022-11-11 16:22:20