-1

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- enter image description here

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

How can i do this?

thewebtud
  • 455
  • 2
  • 4
  • 21
  • 2
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Emile Bergeron May 18 '20 at 19:57

1 Answers1

0

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});
Khabir
  • 5,370
  • 1
  • 21
  • 33