I was building a project connecting it spotify api. I've grabbed the playlist data from the api and set it into a playlists variable but it is not rendering. Below is the whole file for reference.
import { useState, useEffect } from "react"
import useAuth from "./useAuth"
import { Container, ListGroup, ListGroupItem } from "react-bootstrap"
import SpotifyWebApi from "spotify-web-api-node"
import axios from "axios"
const URL = 'https://api.spotify.com/v1/'
const spotifyApi = new SpotifyWebApi({
clientId: "My_SAMPLE_TOKEN",
})
export default function UserDashboard({ code }) {
const accessToken = useAuth(code)
const [user, setUser] = useState()
const [playlists, setPlaylists] = useState([])
useEffect(() => {
if (!accessToken) return
spotifyApi.setAccessToken(accessToken)
}, [accessToken])
let Playlists = []
useEffect(async () => {
if (!accessToken) return
const response = await spotifyApi.getMe()
if (response.statusCode == 200)
setUser(response.body.display_name)
console.log(response)
const response2 = await axios.get(URL + 'me/playlists', {
headers: {
Authorization: "Bearer " + accessToken,
}
})
if (response2.status == 200) {
response2.data.items.forEach(async (l) => {
const response3 = await axios.get(URL + 'playlists/' + l.id + '/tracks', {
headers: {
Authorization: "Bearer " + accessToken,
}
})
Playlists.push({
name: l.name,
items: response3.data.items
})
})
setPlaylists(Playlists)
console.log(Playlists)
}
}, [accessToken])
return (
<Container className="d-flex flex-column py-2" style={{ height: "100vh" }}>
<div>
{user}
</div>
<div>
<ListGroup>
{playlists.map(p => {
<ListGroupItem>
<div>{p.name}</div>
</ListGroupItem>
})}
</ListGroup>
</div>
</Container>
)
}
I have data in the playlists state variable now but I don't know why it is not rendering on the page.Please let me know on where I'm going wrong.