環境
package.json
1 "dependencies": { 2 "next": "12.1.6", 3 "react": "18.1.0", 4 "react-dom": "18.1.0", 5 "react-router-dom": "^6.3.0" 6 }, 7 "devDependencies": { 8 "@types/node": "17.0.31", 9 "@types/react": "18.0.8", 10 "@types/react-dom": "18.0.3", 11 "classnames": "^2.3.1", 12 "typescript": "^4.6.4", 13...省略 14}
実現したいこと
getStaticProps()を使って連想配列を出力したいと考えています。
しかし、List.map(() => {
の部分で
TypeError: Cannot read properties of undefined (reading 'map')
となってしまいます。
const CardList = ({ List }) => {
のところで implicitly has an 'any' type
となっているため、ここが原因なのは分かるのですがそれ以上のことが自分ではわからないため、ご教授いただければと思います。
ソースコード
list.ts
1export const List: { [key: string]: string }[] = [ 2 { "title": "hoge", "image": "hogehoge.jpg", "description": "hogehoge", }, 3 { "title": "piyo", "image": "piyo.jpg", "description": "piyopiyo", }, 4 { "title": "dummy", "image": "dummy.jpg", "description": "dummydummy", }, 5];
CardList/index.tsx
1import React from 'react' 2import List from '@/common/types/List' 3import Card from '@/components/molecules/Card' 4import { List } from '@/path-to/List' 5 6type Props = { 7 list: List[] 8}; 9 10const CardList: React.FC<Props> = ({ list }) => { 11 return ( 12 <ul> 13 { 14 list.map((item) => { 15 <Card 16 title = { item.title } 17 media = { item.image } 18 description = { item.description } 19 /> 20 }) 21 } 22 < /ul> 23 ) 24} 25 26export const getStaticProps: GetStaticProps = async () => { 27 return { props: { list } }; 28} 29 30export default CardList
types/Works.ts
1type ListType = { 2 title: string; 3 image: string; 4 description: string; 5} 6 7export default ListType
Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'map')
参考にした記事
[Next.js]mapを使って連想配列を出力してみる
getStaticProps
blog-starter-typescript
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/05/08 09:17 編集
2022/05/08 09:41
2022/05/08 17:14
2022/05/09 11:47 編集
2022/05/09 11:57 編集