如何将函数的返回值传递给react组件的属性?对于以下示例,将引发 TypeError:props.columns未定义。
<ReactDataGrid
columns={this.props.data['head'] && this.props.data['head']['vars'].map(function(heading) {
return {key: heading, name: heading, editable : true}
})}
rowGetter={this.rowGetter} />
答案 0 :(得分:1)
解决方案是将null检查放在属性之外:
if (this.props.data['head']) {
return (
<ReactDataGrid
columns={this.props.data['head']['vars'].map(function(heading) {
return {key: heading, name: heading, editable : true}
})}
rowGetter={this.rowGetter} />
)
} else {
return (<span>Loading</span>)
}