我有以下功能
churnModel = () => {
if (this.props.churnModel === undefined || this.props.churnModel.length === 0) {
return("N/A")
} else {
this.props.churnModel.map((churn) => {
if (churn === undefined || churn.length === 0) {
return("N/A")
} else {
return(churn.map((model) => {
this.service(model.scoreModelId)
}))
}
})
}
};
this.service函数看起来像这样......
service(e) {
switch(e.toString().toLowerCase()) {
case "in":
return <span>in</span>
case "rpt_callr":
return <span>repeat</span>
default:
return <span>na</span>
}
}
我希望在这里显示结果:
<div className="riskScore">{this.churnModel()}</div>
什么都没有显示,但是当我放入日志时,会打印出来。
这里发生了什么?
你需要在return
.this.props.churnModel.map
之前放置this.service(model.scoreModelId)
function
将返回undefined
。map()
进行回调并将数组的每个元素更改为该回调的return
值。如果你不return
什么所有元素将是undefined
你也可以通过删除return
摆脱this.service(model.scoreModelId)
之前的{}
。就像这样。
return(churn.map((model) => this.service(model.scoreModelId)))
这是代码
churnModel = () => {
if (this.props.churnModel === undefined || this.props.churnModel.length === 0) {
return("N/A")
} else {
return this.props.churnModel.map((churn) => {
if (churn === undefined || churn.length === 0) {
return("N/A")
} else {
return(churn.map((model) => {
return this.service(model.scoreModelId)
}))
}
})
}
};
你必须在几行中使用return
语句:
churnModel = () => { if (this.props.churnModel === undefined || this.props.churnModel.length === 0) { return("N/A") } else { return this.props.churnModel.map((churn) => { if (churn === undefined || churn.length === 0) { return("N/A") } else { return(churn.map((model) => { return this.service(model.scoreModelId) })) } }) } };
你为什么需要回来?这是因为你正在使用curly braces。