传递数组作为React Typescript中的prop。

huangapple go评论69阅读模式
英文:

Pass array as prop in React Typescript

问题

我有一个Navbar组件,其中我正在创建一个NavDropdown组件,以在Navbar内部使用。我想将数组作为属性传递给NavDropdown组件,以显示其下拉项。
数组的结构如下:

DropDownItems: [
  {
    title: "Keywords",
    to: "/app/keywords",
  },
  {
    title: "Followers",
    to: "/app/followers",
  },
  {
    title: "Following",
    to: "/app/following",
  },
]

然而,我遇到了以下两个错误:

  1. 属性'map'不存在于类型'SubItemsProps'上。
  2. 参数'subItem'隐式具有'any'类型。

Navbar.tsx

type SubItemsProps = {
  subItems: DropdownItem[];
};

interface DropdownItem {
  subTitle: string;
  subLink: string;
}

const NavDropdown = ({
  title,
  subItems
}: {
  title: string;
  subItems: SubItemsProps;
}) => {
  return (
    <div>
      <div>{title}</div>
      <div>
        <ul>
          {subItems.map(subItem => (
            <Link href={subItem.subLink}>
              <li>
                <span>{subItem.subTitle}</span>
              </li>
            </Link>
          ))}
        </ul>
      </div>
    </div>
  );
};

/* Navbar component */

请注意,我只翻译了代码部分,没有添加其他内容。

英文:

I've Navbar component inside which I'm creating a NavDropdown component to use inside the Navbar. I want to pass array as prop to the NavDropdown component to display its dropdown items.
The array will be of structure:

DropDownItems: [
        {
          title: &quot;Keywords&quot;,
          to: &quot;/app/keywords&quot;,
        },
        {
          title: &quot;Followers&quot;,
          to: &quot;/app/followers&quot;,
        },
        {
          title: &quot;Following&quot;,
          to: &quot;/app/following&quot;,
        },
      ]

However, I'm getting the following 2 errors:

  1. Property 'map' does not exist on type 'SubItemsProps'.
  2. Parameter 'subItem' implicitly has an 'any' type.

Navbar.tsx


    type SubItemsProps = {
              subItems: DropdownItem[]
            }
        
        interface DropdownItem {
          subTitle: string,
          subLink: string
        }
        const NavDropdown = ({
                title,
                subItems
              }: {
                title: string;
                subItems: SubItemsProps;
              }) =&gt; {
                return (
                  &lt;div&gt;
                    &lt;div&gt;{title}&lt;/div&gt;
                    &lt;div&gt;
                      &lt;ul&gt;
                        {subItems.map(subItem =&gt; (
                          &lt;Link href={subItem.subLink}&gt;
                            &lt;li&gt;
                              &lt;span&gt;
                                {subItem.subTitle}
                              &lt;/span&gt;
                            &lt;/li&gt;
                          &lt;/Link&gt;
                        ))}
                      &lt;/ul&gt;
                    &lt;/div&gt;
                  &lt;/div&gt;
                );
              };


/* Navbar component */

答案1

得分: 0

类型 SubItemsProps = 下拉项[]
英文:

just edit:

type SubItemsProps = DropdownItem[]

答案2

得分: 0

以下是翻译好的部分:

你可以尝试以下方式来解决这个问题:

<!-- language: lang-js -->

    {subItems.subItems.map(subItem => (
        <Link href={subItem.subLink}>
            <li>
                <span>
                   {subItem.subTitle}
                </span>
            </li>
        </Link>
     ))}
你定义的类型具有数组作为属性。这就是为什么你必须调用属性来获取数组。

<!-- language: lang-js -->

     type SubItemsProps = {
          subItems: DropdownItem[]
     }
英文:

You might be able to solve the problem the following way:

<!-- language: lang-js -->

{subItems.subItems.map(subItem =&gt; (
    &lt;Link href={subItem.subLink}&gt;
        &lt;li&gt;
            &lt;span&gt;
               {subItem.subTitle}
            &lt;/span&gt;
        &lt;/li&gt;
    &lt;/Link&gt;
 ))}

The type which you have defined has the array as attribute. That is why you have to call to the attribute to get the array.

<!-- language: lang-js -->

 type SubItemsProps = {
      subItems: DropdownItem[]
 }

huangapple
  • 本文由 发表于 2023年2月10日 13:51:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定