実現したいこと
配列の要素をブラウザに表示させたい
発生している問題・分からないこと
titleプロパティが未定義と認識されて、配列の情報が画面に反映できない
エラーメッセージ
error
1Cannot read properties of undefined (reading 'title') 2TypeError: Cannot read properties of undefined (reading 'title') 3 at ForItem (http://localhost:3000/static/js/bundle.js:338:28) 4 at react-stack-bottom-frame (http://localhost:3000/static/js/bundle.js:18249:18) 5 at renderWithHooks (http://localhost:3000/static/js/bundle.js:9566:20) 6 at updateFunctionComponent (http://localhost:3000/static/js/bundle.js:10835:17) 7 at beginWork (http://localhost:3000/static/js/bundle.js:11453:16) 8 at runWithFiberInDEV (http://localhost:3000/static/js/bundle.js:6794:14) 9 at performUnitOfWork (http://localhost:3000/static/js/bundle.js:14038:93) 10 at workLoopSync (http://localhost:3000/static/js/bundle.js:13932:38) 11 at renderRootSync (http://localhost:3000/static/js/bundle.js:13916:7) 12 at performWorkOnRoot (http://localhost:3000/static/js/bundle.js:13677:42)
該当のソースコード
index.js
1import React from 'react'; 2import ReactDOM from 'react-dom/client'; 3import './index.css'; 4import ForNest from './ForNest.js'; 5import books from './Books.js'; 6import reportWebVitals from './reportWebVitals'; 7 8const root = ReactDOM.createRoot(document.getElementById('root')); 9root.render( 10 <React.StrictMode> 11 <ForNest books={books} /> 12 </React.StrictMode> 13) 14 15reportWebVitals();
ForNest.js
1import ForItem from "./ForItem"; 2 3export default function ForNest({books}) { 4 return ( 5 <dl> 6 {books.map(element => 7 <ForItem books={element} key={element.isbn} /> 8 )} 9 </dl> 10 ); 11}
ForItem.js
1export default function ForItem({element}) { 2 return ( 3 <> 4 <dt> 5 <a href={'https://wings.msn.to/books/${element.isbn}/${element.isbn}.jpg'}> 6 {element.title}({element.price}円) 7 </a> 8 </dt> 9 <dd>{element.summary}</dd> 10 </> 11 ); 12}
Books.js
1const books = [ 2 { 3 isbn : "978-4-8156-1336-5", 4 title : "これからはじめるJavascript", 5 price : 3800, 6 summary : "javascriptの構文・実務で使える知識について", 7 download : true 8 }, 9 10 { 11 isbn : "988777", 12 title : "Ruby on Rails", 13 price : 3000, 14 summary : "rubyとそのフレームワークの使用方法について解説", 15 download : true 16 } 17]; 18 19export default books;
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
ForItem.jsの{element.title}を削除すると、次はpriceプロパティが未定義と表示されます
Books.jsが正常に読み込まれていないと推測していますが、解決が出来ません。
解決策をご教示していただきたく思います。
よろしくお願いします。
補足
特になし

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2024/12/26 04:04
2024/12/26 04:18