I'm working on a react project. I wrote a script to get auth token to be passed to a custom hook in order to retrieve information. However, when I try to render this react component it would rerender itself to get the result. Here is the code:
const SpotifyLogin = () => {
const [token, setToken] = useState(getToken().accessToken);
const albums = useSpotifyData(token);
console.log("Rendering ------");
console.log("Has album:" + !!albums);
};
Here is the custom hook useSpotifyData(token)
export function useSpotifyData(token) {
const [albums, setAlbums] = useState();
useEffect(() => {
SpotifyRequester("me/albums", token).then((data) => setAlbums(data));
}, []);
return albums;
}
The console would give me this on render:
Rendering ------
Has album: false
Rendering ------
Has album: true
How can I resolve this? What might be causing this rendering issue?
I've tried using logging token before passing into the custom hook. It has data before its been passed in. I have no clue why the rendering is triggered.
Any help is appreciated!