如何将函数的返回值传递给 React 组件的属性?对于以下示例,会抛出 TypeError: props.columns is undefined。
<ReactDataGrid
columns={this.props.data['head'] && this.props.data['head']['vars'].map(function(heading) {
return {key: heading, name: heading, editable : true}
})}
rowGetter={this.rowGetter} />
最佳答案
解决方案是将 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>)
}
关于javascript - 如何将函数的返回值作为属性传递给 react 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37967895/