【问题标题】:Passing array of arrays as props将数组数组作为道具传递
【发布时间】:2020-07-12 18:16:08
【问题描述】:

谁能解释我在这里做错了什么......

<Something
content={[
    {
        heading: "Add Company",
        icon: "plus",
        options: {[
            {
                image: "srcimage",
                text: "New Company",
                link: "test"
            }, {  //Errors here
                image: "srcimage",
                text: "Existing Company",
                link: "test"
            }
        ]}
    }, {
        heading: "Import Company",
        icon: "download",
        options: {[
            {
                image: "/src/image",
                text: "Class",
                link: "/test"
            },
            {
                image: "/src/image",
                text: "BGL",
                link: "/test"
            },
            {
                image: "/src/image",
                text: "SuperMate",
                link: "/test"
            }
        ]}
    }]
} />

我收到错误消息...Unexpected token, expected "]",上面写着error here。最终,我想根据这样传入的内容创建一些内容块......

谢谢

【问题讨论】:

    标签: reactjs components react-props


    【解决方案1】:

    {[]} 不是对象的有效语法。

    content={[
      {
        heading: "Add Company",
        icon: "plus",
        options: [ // remove the curly boy that was here
          {
            image: "srcimage",
            text: "New Company",
            link: "test"
          }, { 
            image: "srcimage",
            text: "Existing Company",
            link: "test"
          }
        ] // and here
      }, {
        heading: "Import Company",
        icon: "download",
        options: [ // and here
          {
            image: "/src/image",
            text: "Class",
            link: "/test"
          },
          {
            image: "/src/image",
            text: "BGL",
            link: "/test"
          },
          {
            image: "/src/image",
            text: "SuperMate",
            link: "/test"
          }
        ] // and here
      }]}
    

    【讨论】:

      【解决方案2】:

      对于你需要去掉花括号的选项属性,只需传递数组

      options: [
        {
          image: "srcimage",
          text: "New Company",
          link: "test"
        }, 
        {
          image: "srcimage",
          text: "Existing Company",
          link: "test"
        }
      ]
      

      【讨论】:

        猜你喜欢
        • 2022-11-11
        • 2021-02-17
        • 2016-12-18
        • 2019-07-03
        • 1970-01-01
        • 1970-01-01
        • 2020-08-28
        • 2018-08-01
        • 2017-01-31
        相关资源
        最近更新 更多