【问题标题】:Get value of promise returned by a function in React获取 React 中函数返回的 promise 的值
【发布时间】:2020-05-18 19:56:10
【问题描述】:

我正在做一个 ReactJs 项目。

我有一个函数返回一个带有用户数据 {Promise } 的承诺。 此函数在 .tsx 文件中使用 window.myAuth.isUserLoggedIn() 调用。

可以在控制台中看到响应-

我想将这个 promise 返回的值存储在 tsx 文件的某个局部变量中。

我该怎么做?

【问题讨论】:

标签: javascript reactjs typescript


【解决方案1】:

如果你使用功能组件,那么你可以使用useState

// This is declaration
const [loginInfo, setLoginInfo] = useState({});
// in .then() you can set the loginInfo
setLoginInfo(response.data);

如果你使用类组件,那么你可以使用state object

// in constructor
this.state = {
   loginInfo = {}
}
// in .then() you can set the loginInfo
this.state({loginInfo: response.data});

【讨论】:

    猜你喜欢
    • 2021-01-26
    • 1970-01-01
    • 2020-05-12
    • 2023-03-11
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 2017-04-08
    • 1970-01-01
    相关资源
    最近更新 更多