为什么React Dropdown组件需要将数据传递给一个const变量,否则无法渲染。

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

Why react Dropdown component need pass the data to a const variable or it could not be rendered

问题

Here is the translated code part:

function PackageSelection({ data, switchPackage }) {
  const items = data;
  return (
    <Space direction="vertical" size={16}>
      <Dropdown
        menu={{
          items,
        }}
        trigger={['click']}
        arrow={{ pointAtCenter: true }}
      >
      </Dropdown>
    </Space>
  )
}

And the translated data:

[
  {
    "key": "official",
    "type": "group",
    "label": "official item",
    "children": [
      {
        "key": "01.00.00.01_official",
        "label": "01.00.00.01"
      },
      {
        "key": "01.00.00.02_official",
        "label": "01.00.00.02"
      }
    ]
  }
]
英文:

I am new to react. Rencently I met a condition that the drop down component could not directly render the data that we passed into.

My component code:

function PackageSelection({data, switchPackage}) {
  const items = data
  return (
    &lt;Space direction=&quot;vertical&quot; size={16}&gt;
      &lt;Dropdown
        menu={{
          items,
        }}
        trigger={[&#39;click&#39;]}
        arrow={{pointAtCenter: true}}
      &gt;
      &lt;/Dropdown&gt;
    &lt;/Space&gt;
  )

}

I found if we do not have this code "const items = data", component would be rendered failed. For now, I could not figured out why?

The data passed-into is:

[
    {
        &quot;key&quot;: &quot;official&quot;,
        &quot;type&quot;: &quot;group&quot;,
        &quot;label&quot;: &quot;official item&quot;,
        &quot;children&quot;: [
            {
                &quot;key&quot;: &quot;01.00.00.01_official&quot;,
                &quot;label&quot;: &quot;01.00.00.01&quot;
            },
            {
                &quot;key&quot;: &quot;01.00.00.02_official&quot;,
                &quot;label&quot;: &quot;01.00.00.02&quot;
            },
          ]
     }
]

答案1

得分: 0

Here is the translated content:

在这里,翻译的内容:

没有真正的区别:

function PackageSelection({data, switchPackage}) {
  const items = data
  return (
    <Space direction="vertical" size={16}>
      <Dropdown
        menu={{
          items,
        }}
        trigger={['click']}
        arrow={{pointAtCenter: true}}
      >
      </Dropdown>
    </Space>
  )
}

和:

function PackageSelection({data, switchPackage}) {
  return (
    <Space direction="vertical" size={16}>
      <Dropdown
        menu={{
          data,
        }}
        trigger={['click']}
        arrow={{pointAtCenter: true}}
      >
      </Dropdown>
    </Space>
  )

}

我唯一能想到的解释是传递给 menu 的对象属性应该叫做 items

在JavaScript中,

{
foo,
}

与以下相同:

{
foo: foo
}

因此,如果尝试:

function PackageSelection({data, switchPackage}) {
  return (
    <Space direction="vertical" size={16}>
      <Dropdown
        menu={{
          items: data,
        }}
        trigger={['click']}
        arrow={{pointAtCenter: true}}
      >
      </Dropdown>
    </Space>
  )
}

它应该可以工作。

英文:

There really is no difference between:

function PackageSelection({data, switchPackage}) {
  const items = data
  return (
    &lt;Space direction=&quot;vertical&quot; size={16}&gt;
      &lt;Dropdown
        menu={{
          items,
        }}
        trigger={[&#39;click&#39;]}
        arrow={{pointAtCenter: true}}
      &gt;
      &lt;/Dropdown&gt;
    &lt;/Space&gt;
  )
}

and:

function PackageSelection({data, switchPackage}) {
  return (
    &lt;Space direction=&quot;vertical&quot; size={16}&gt;
      &lt;Dropdown
        menu={{
          data,
        }}
        trigger={[&#39;click&#39;]}
        arrow={{pointAtCenter: true}}
      &gt;
      &lt;/Dropdown&gt;
    &lt;/Space&gt;
  )

}

The only explanation I can think of is that the property of the object passed to menu should be called items


In javaScript

{
foo,
}

is the same as :

{
foo: foo
}

so if you try:

function PackageSelection({data, switchPackage}) {
  return (
    &lt;Space direction=&quot;vertical&quot; size={16}&gt;
      &lt;Dropdown
        menu={{
          items: data,
        }}
        trigger={[&#39;click&#39;]}
        arrow={{pointAtCenter: true}}
      &gt;
      &lt;/Dropdown&gt;
    &lt;/Space&gt;
  )
}

it should work.

huangapple
  • 本文由 发表于 2023年5月11日 11:46:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224019.html
匿名

发表评论

匿名网友

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

确定