【发布时间】:2020-08-30 21:04:13
【问题描述】:
我正在尝试缩短从反应中的 API 调用获得的反应组件中的字符串,但我得到 TypeError: Cannot read property 'substring' of undefined。
这里是sn-p:
shorten(){
let res = this.props.details.substring(100, 0);
return res;
}
我在渲染返回函数中使用该函数:
render(){
return (
<div>
<p className="text-muted font1-1">{this.shorten()}</p>
</div>
);
}
错误代码:
TypeError: Cannot read property 'substring' of undefined
【问题讨论】:
-
你的
details道具没有价值。 -
它有一个值,我通过控制台登录确认。
-
您从 API 中获得了价值。所以你的价值一开始是不确定的。当 API 返回后,你得到了值,你在控制台中看到了这个结果,但是当 react 第一次渲染你的组件时,
details属性没有值。
标签: javascript reactjs