reactjs - 我如何在 React 中返回一个数字?
问题描述
在反应中,我想做的就是查找并返回一个数字。我有:
getVAL = async () => {
const pathParam = this.props.match.params.myVal;
const url = "get-myVal?myVal=" + pathParam;
const response = await this._axios.get(url);
const intmyVal = Number(response.data);
return Number (intmyVal)
};
我用这个:
myVal=this.getVAL();
但是当我渲染...
myVal={this.myVal}
我Type 'Promise<number>' is not assignable to type 'number'.
对这个奇怪的世界完全陌生,所以任何指针都值得赞赏。
编辑:在 jonrsharpe 的帮助下(任何错误,我的),我现在有:
myIdPromise=this.getID();
myIdPromise.then(don't know what to put here to just get a number {
...
})
解决方案
这有效:
async getIDPromise(): Promise<Number> {
...
return intmyVal
}
async getID(): Promise<Number> {
const value = await this.getIDPromise()
推荐阅读
- angular - 任何人都可以向我解释给定的代码片段吗?它能做什么?
- java - 否则,如何在 JTextfield 中显示 JComboBox 的选定项目?
- drupal-8 - D8覆盖核心类
- java - JavaFX FXML - 使用 fx:id 获取 MenuItem
- html - 如何在AngularJS中循环多个表TH
- python - 用ffmpeg切割多个片段
- jsf - Liferay 7 JSF 动作未触发
- python - 电影评论类别错误 - NTLK
- symfony - Symfony3 以一种形式持久化每个 ManytoOne 实体字段?
- ms-word - 如何使用 word addin 在页眉中插入文档页码?