英文:
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 (
<Space direction="vertical" size={16}>
<Dropdown
menu={{
items,
}}
trigger={['click']}
arrow={{pointAtCenter: true}}
>
</Dropdown>
</Space>
)
}
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:
[
{
"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"
},
]
}
]
答案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 (
<Space direction="vertical" size={16}>
<Dropdown
menu={{
items,
}}
trigger={['click']}
arrow={{pointAtCenter: true}}
>
</Dropdown>
</Space>
)
}
and:
function PackageSelection({data, switchPackage}) {
return (
<Space direction="vertical" size={16}>
<Dropdown
menu={{
data,
}}
trigger={['click']}
arrow={{pointAtCenter: true}}
>
</Dropdown>
</Space>
)
}
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 (
<Space direction="vertical" size={16}>
<Dropdown
menu={{
items: data,
}}
trigger={['click']}
arrow={{pointAtCenter: true}}
>
</Dropdown>
</Space>
)
}
it should work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论