使用React.js
和Semantic UI React
我想动态创建标签。
当使用表示制表符对象数组的const
时,这样可以正常工作。
const panes = [
{
menuItem: 'Record 1',
render: () => <Tab.Pane>Tab 1 Content</Tab.Pane>
}
]
...
<Tab panes={panes}/>
但是,我需要根据状态变量source
动态添加这些:
getPanes() {
return this
.state
.source
.map((source) => [{
menuItem: source.sourceName,
render: () => <Tab.Pane>Tab 1 Content</Tab.Pane>
}])
}
...
<Tab panes={this.getPanes}/>
这导致:
Warning: Failed prop type: Invalid prop `panes` of type `function` supplied to `Tab`, expected an array.
答案 0 :(得分:1)
两个问题:
this.getPanes
指的是你的函数定义,它不会调用
你的功能。如果你想要你的功能结果
应该使用括号调用:this.getPanes()
.map
内返回的是您的窗格对象,而不是一个窗格对象的数组:-
.map((source) => ({
menuItem: source.sourceName,
render: () => <Tab.Pane>Tab 1 Content</Tab.Pane>
}))