使用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.
两个问题:
this.getPanes
引用您的函数定义,它不会调用您的函数。您需要函数的结果,应该使用括号来调用它:this.getPanes()
.map
是您的窗格对象,而不是一个窗格对象的数组:-
.map((source) => ({
menuItem: source.sourceName,
render: () => <Tab.Pane>Tab 1 Content</Tab.Pane>
}))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句