我有两个数组。但是当其中之一为 null 时,会出现以下错误:
Cannot read property 'toJS' of undefined in that line
这是触发错误的相关调用:{groupsGuney.toJS()}
这是我的变量声明 let groupsGuney, groupsKuzey;
最后是我的两个数组。但是当其中之一为 null 时,它会给出错误:
...
if (muso == 1) {
groupsGuney = this.props.groups
.groupBy((group, idx) => idx % maxRows)
.map((ggs, idx) => {
return this.renderGroups(ggs, idx);
}).toList().flatten(true);
}
if (muso == 2) {
groupsKuzey = this.props.groups
.groupBy((group, idx) => idx % maxRows)
.map((ggs, idx) => {
return this.renderGroups(ggs, idx);
}).toList().flatten(true);
}
var result = (
<div>
<div className={classSelector + ' discard-mini-box-area'} >
{ groupsGuney.toJS() }
</div>
<div className={classSelector + ' discard-mini-box-area'} >
{ groupsKuzey.toJS() }
</div>
</div>
);
return result;
}
}
export default DiscardMiniBoxArea;
最佳答案
而不是做:
<div>
<div className={classSelector + ' discard-mini-box-area'} >
{groupsGuney.toJS()}
</div>
....
你应该这样做:
<div>
<div className={classSelector + ' discard-mini-box-area'} >
{groupsGuney && groupsGuney.toJS()}
</div>
....
在您的对象上调用函数之前,您需要确保它存在。如果您不确定您的对象是否始终具有该功能,您将需要进行额外的检查,以确保 toJS
存在并且它是一个有效的功能。
如果是这种情况,请将容器内的内容更新为:
{groupsGuney && typeof groupsGuney.toJS === 'function' && groupsGuney.toJS()}
然而,理想情况下,如果您想要渲染的内容不存在,您根本不会渲染这个特定的组。您应该将这些检查移至呈现组件之前。
关于reactjs - 无法读取未定义的属性 'toJS',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38128034/