前提
TypeScriptで型定義を行いました。
その際に起こったエラーが解決できないのでご協力をお願いしたいです
行ったこと
型定義前
ShopCard.tsx
1import Shop from '../types/shops' 2 3export const ShopCard = (props: Shop) => { 4 const { name, description, price, image, holidays, comments } = props
変更後
ShopCard.tsx
1import Shop from '../types/shops' 2 3type Props = { 4 shop: Shop 5} 6 7const ShopCard: React.FC<Props> = ({ shop }) => { 8 const { name, description, price, image, holidays, comments } = shop
変更前にエラーはありませんでした
発生している問題・エラーメッセージ
TypeError: Cannot destructure property 'name' of 'shop' as it is undefined.
該当のソースコード
/types/shops.tsはこのように書いています
shops.ts
1type Shop = { 2 name: string 3 description: string 4 price: string 5 holidays: ShopHoliday[] 6 image: string 7 comments: Comment[] 8} 9 10export type ShopHoliday = { 11 wday: string 12} 13 14export type Comment = { 15 content: string 16 nickname: string 17 createdAt: string 18} 19 20export default Shop 21 22
ぜひご教授お願い致します。
追記
index.tsx
1import { ShopCard } from '../components' 2import { mockData } from 'mockData/ShopData' 3 4const Home: NextPage = () => { 5 return ( 6 <div> 7 <div className="gap-4 mx-auto px-6 py-3 w-fit max-w-[1200px] grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3"> 8 {mockData.map((item, index) => ( 9 <div key={index} className="border rounded-2xl border-gray-200 bg-white overflow-hidden max-w-[400px]"> 10 <Link href="/"> 11 <ShopCard 12 name={item.name} 13 description={item.description} 14 price={item.price} 15 image={item.image} 16 holidays={item.holidays} 17 comments={item.comments} 18 /> 19 </Link> 20 </div> 21 ))} 22 </div> 23 </div> 24 ) 25} 26 27export default Home
回答1件
あなたの回答
tips
プレビュー