I am using a Spotify API to fetch songs and I am trying to display the first song for testing.
I am trying to display the first song as text but currently I am getting the error "Objects are not valid as a React child (found: Object with keys {_U, _V, _W, _X}). If you meant to render a collection of children use a array instead.
I am confused as I am just trying to the first thing from the JSON and display it as a text on the stats screen
import React, {useState} from "react";
import axios from "axios";
import { getSpotifyToken } from "../../hooks/spotifyAuth";
const getTopTracks = async () => {
//Getting spotify token
const spotifyToken = getSpotifyToken();
console.log("Getting access Token for TopSongs:", spotifyToken );
// const [songName, setSongName] = useState("");
const api_url = "https://api.spotify.com/v1/me/top/tracks?time_range=short_term&limit=5";
// const api_url = "https://api.spotify.com/v1/me/top/artists?time_range=short_term&limit=1&offset=5";
// console.log(api_url);
try{
const response = await axios.get(api_url, {
headers: {
'Authorization': `Bearer ${spotifyToken}`
}
});
const myJSON = response.data.items[0].name.toString();
console.log("My JSON:", myJSON); //this just prints the song name
return myJSON;
}catch(error){
console.log(error);
}
};
const StatsScreen = ({ navigation }) => {
const topSong = getTopTracks();
return (
<View>
<Text>StatsScreen</Text>
<Text>{topSong}</Text>
</View>
);
};
export default StatsScreen;