I’m creating React context but it returns a promise. In the file playlistcontext.js I’ve the following code:
JavaScript
x
21
21
1
import React, { useEffect } from 'react';
2
import YouTube from '../services/youtube';
3
4
const playlistsData = YouTube.getPlaylists();
5
// console.log(playlistsData);
6
const PlaylistsDataContext = React.createContext(playlistsData);
7
8
const PlaylistsDataProvider = (props) => {
9
10
const [playlists, setPlaylists] = React.useState(playlistsData);
11
12
useEffect(() =>{
13
const playlistsData = YouTube.getPlaylists();
14
console.log(playlistsData);
15
setPlaylists(playlistsData);
16
},[])
17
18
return <PlaylistsDataContext.Provider value={[playlists, setPlaylists]}>{props.children}</PlaylistsDataContext.Provider>;
19
}
20
21
export {PlaylistsDataContext, PlaylistsDataProvider};
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
1
39
39
1
import axios from 'axios';
2
import { YOUTUBE_API } from '../config/config';
3
4
function Youtube() {
5
6
const handleError = (resp) => {
7
8
let message = '';
9
10
switch (+resp.status) {
11
case 401:
12
message = resp.data.error;
13
break;
14
default:
15
message = 'general error';
16
}
17
18
return message;
19
}
20
21
const getPlaylists = async () => {
22
23
try {
24
const result = await axios.get(YOUTUBE_API + '');
25
26
return result.data;
27
} catch(e) {
28
return Promise.reject(handleError(e.response));
29
}
30
31
}
32
33
return {
34
getPlaylists
35
}
36
}
37
38
const ytMethod = Youtube();
39
export default ytMethod;
then, I have a containers “tutorialcontainer.js” in which I’ve wrapped a component:
JavaScript
1
14
14
1
import React, {useState} from 'react';
2
import { PlaylistsDataProvider } from '../containers/playlistscontext';
3
import Tutorials from '../components/tutorials';
4
5
const TutorialsContainer = (props) => {
6
7
return (
8
<PlaylistsDataProvider>
9
<Tutorials />
10
</PlaylistsDataProvider>
11
);
12
}
13
14
export default TutorialsContainer;
In the last file tutorials.js I have the component. In this file the console.log(playlist) returns me a promise.
JavaScript
1
28
28
1
import React, {useState, useEffect} from 'react';
2
import SectionBoxPlaylist from '../components/html_elements/card_playlist';
3
import Header from '../components/header';
4
import { PlaylistsDataContext } from '../containers/playlistscontext';
5
6
const Tutorials = (props) => {
7
8
const [playlists, setPlaylists] = React.useContext(PlaylistsDataContext);
9
10
return (
11
<div className="app-container">
12
<Header />
13
<div className="section section-one text-center">
14
<div className="section-content">
15
<div className="section-box-items">
16
{
17
Object.keys(playlists).map((item) => {
18
return <SectionBoxPlaylist key={item} id={item} info={playlists[item]} />
19
})
20
}
21
</div>
22
</div>
23
</div>
24
</div>
25
);
26
}
27
28
export default Tutorials;
Can you help and explain me why? Thank you!
Advertisement
Answer
setPlaylists
is called immediately after YouTube.getPlaylists()
.
JavaScript
1
6
1
useEffect(() => {
2
const playlistsData = YouTube.getPlaylists();
3
console.log(playlistsData); // playlistsData is not fetched
4
setPlaylists(playlistsData);
5
},[])
6
You should be able to use .then()
:
JavaScript
1
5
1
YouTube.getPlaylists().then(response => {
2
console.log(response);
3
setPlaylists(response);
4
});
5
You can also create async function inside useEffect()
:
JavaScript
1
10
10
1
useEffect(() => {
2
const getYTPlaylist = async () => {
3
const playlistsData = await YouTube.getPlaylists();
4
console.log(playlistsData);
5
setPlaylists(playlistsData);
6
}
7
8
getYTPlaylist();
9
},[])
10