【发布时间】:2022-01-05 09:18:27
【问题描述】:
很抱歉,我将如何在下面的示例中使用 typescript。经过一个小时的尝试,感谢您的帮助!
下面的 map 函数使用了 sortOptions,我尝试在声明 const SortOptionsProps 时使用 Typescript 定义它们。因此,我很想弄清楚如何将 SortOptionsProps 传递给下面的export default function Layout()。
import { Menu } from '@headlessui/react'
declare const SortOptionsProps: {
name: string
href: string
}[]
const sortOptions = [
{ name: 'Most Popular', href: '#' },
{ name: 'Best Rating', href: '#' },
{ name: 'Newest', href: '#' },
{ name: 'Price: Low to High', href: '#' },
{ name: 'Price: High to Low', href: '#' },
]
export default function Layout() {
return (
<>
{sortOptions.map((option) => (
<Menu.Item key={option}>
{({ active }) => (
<a
href={option.href}
className={classNames(
active
? 'bg-gray-100'
: '',
'block px-4 py-2 text-sm font-medium text-gray-900'
)}
>
{option.name}
</a>
)}
</Menu.Item>
))}
</>
)
}
【问题讨论】:
-
这可能是stackoverflow.com/a/35435879的副本
标签: reactjs typescript