Skip to content
Advertisement

REACT createContext with async function return axios data

I’m creating React context but it returns a promise. In the file playlistcontext.js I’ve the following code:

JavaScript

In the file youtube.js, that I use it like a service, I’have the code below. In this function a console.log(result.data) return me the correct data.

JavaScript

then, I have a containers “tutorialcontainer.js” in which I’ve wrapped a component:

JavaScript

In the last file tutorials.js I have the component. In this file the console.log(playlist) returns me a promise.

JavaScript

Can you help and explain me why? Thank you!

Advertisement

Answer

setPlaylists is called immediately after YouTube.getPlaylists().

JavaScript

You should be able to use .then():

JavaScript

You can also create async function inside useEffect():

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement