【问题标题】:How in React do I return a number?我如何在 React 中返回一个数字?
【发布时间】:2021-03-10 12:51:52
【问题描述】:

在反应中,我想做的就是查找并返回一个数字。我有:

  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 {
   ...
})

【问题讨论】:

标签: reactjs typescript


【解决方案1】:

这行得通:

async getIDPromise(): Promise<Number> {
...
  return   intmyVal
}

async getID(): Promise<Number> {

  const value = await this.getIDPromise()

【讨论】:

    猜你喜欢
    • 2020-09-29
    • 2020-03-18
    • 2011-06-24
    • 2010-10-16
    • 1970-01-01
    • 2022-01-04
    • 2011-01-18
    • 2011-11-29
    • 2021-03-08
    相关资源
    最近更新 更多