[英]error while setting variants option array Cannot read property '0' of undefined
hello iam creating an ecomerce shop in my chec dasboard i set variants of sizes large small but when i try to implement options drop down on options array i get the error Cannot read property '0' of undefined in line12 which is let finalSizeArray = props.product.variants[0].options.map(option =你好,我在我的 chec 仪表板上创建了一个电子商务商店,我将大小的变体设置为大小,但是当我尝试在选项数组上实现选项下拉菜单时,我收到错误无法读取第 12 行中未定义的属性“0”,这让 finalSizeArray = props。 product.variants[0].options.map(option =
productcard.js产品卡.js
import React, { useState, useEffect } from 'react';
import { Card, Image, Button, Icon, Dropdown } from 'semantic-ui-react';
const ProductCard = (props) => {
console.log(props.product, 'props from Container')
const [sizes, setSizes] = useState([])
const [variantInfo, setVariantInfo] = useState()
useEffect(() => {
let finalSizeArray = props.product.variants[0].options.map(option => {
let sizeInfo = {}
sizeInfo.key = option.name
sizeInfo.text = option.name
sizeInfo.value = option.id
return sizeInfo
})
setSizes(finalSizeArray)
}, [])
const handleSize = (e, {value}) => {
setVariantInfo({[props.product.variants[0].id]: value})
}
const handleButtonAddCart = e => {
e.preventDefault()
props.addToCart(props.product.id, variantInfo)
// Funtion to Clear Select Input for Dropdown - Needs work.
// let selectInput = document.querySelectorAll('.sizes-drop')
// selectInput.forEach((input,i) => {
// input.children[0].innerHTML = 'Select Size'
// // input.children[0].classList.add('default')
// })
}
return (
<Card>
<Image src={props.product.media.source} />
<Card.Content>
<Card.Header>{props.product.name}</Card.Header>
<Card.Meta>{props.product.price.formatted_with_symbol}</Card.Meta>
<Card.Description>{props.product.description.replace(/(<([^>]+)>)/ig,"")}</Card.Description>
<Dropdown
className="sizes-drop"
fluid
placeholder='Select Size'
selection
options={sizes}
/>
<Button fluid className='add-button' onClick={handleButtonAddCart}>
Add to Cart
<Icon name='arrow right' />
</Button>
</Card.Content>
</Card>
);
};
export default ProductCard;
I thinks its because your props not yet loaded... You can give handle to your code like this我认为这是因为你的道具还没有加载......你可以像这样处理你的代码
useEffect(() => {
let finalSizeArray = props?.product?.variants[0]?.options?.map(option => {
let sizeInfo = {}
sizeInfo.key = option.name
sizeInfo.text = option.name
sizeInfo.value = option.id
return sizeInfo
})
setSizes(finalSizeArray)
}, [])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.