简体   繁体   中英

Get value of promise returned by a function in React

I am working on a ReactJs project.

I have a function that returns a promise with the user data {Promise < user >}. This function is called using window.myAuth.isUserLoggedIn() in the.tsx file.

The response can be seen in the console- 在此处输入图像描述

I want to store the value returned by this promise in some local variable of the tsx file.

How can i do this?

if you use functional component then you can use useState

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

If you use class component then you can use state object

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM