カートに1つ以上のバッチがついているアイテムのうち、番号が早いものを優先に1つだけイメージを表示する仕組みを、Reactで実装したいと考えているのですがTypeErrorが出てしまい困っています。
エラー内容
おそらく、levelの取り方が間違っているのかもしれません。
あるいは、この時点では取得ができないのかもしれません。
TypeError: Cannot read property 'batch' of undefined
ソースコード
JavaScript
1// items, cartItemsを定義する 2const items = response.item_list.map((item, index) => ({ 3 batch: 0, 4・・・・ 5}); 6const cartItems = []; 7 8// 表示部分 9render() { 10 // cartItemsを取ってくる 11 const cartItems = this.state.cartItems; 12 return ( 13 // cartItemsの番号が早いもの順で1以上バッチがついているものを探す 14 {cartItems === [] ? ( 15 <div /> 16 ) : cartItems[0].batch > 0 ? ( 17 <div> 18 <img src={items[0].image} alt="" /> 19 </div> 20 ) : cartItems[1].batch > 0 ? ( 21 <div> 22 <img src={items[1].image} alt="" /> 23 </div> 24 ) : cartItems[2].batch > 0 ? ( 25 <div> 26 <img src={items[2].image} alt="" /> 27 </div> 28 ) : ( 29 <div /> 30 )} 31 ) 32}
cartItemsが無理そうであれば、itemsのインデックスを使って処理するのも可能なのですが、同様のエラーが発生しています。
また、console.logをやってみると、items、cartItemsともに、batchは子要素として表示されています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/16 01:19
2018/10/16 01:37