I’m working on a fan site for a content creator using the Twitch API. I keep running into an issue where I get an error saying that map is not a function when I try to display the fetched information.
import React, { useState, useEffect } from "react";
import axios from 'axios';
export const StreamerPage = () => {
const [isLoading, setIsLoading] = useState(true);
const [videoData, setVideoData] = useState([])
const clientId = "3fjsnq21qnimeiel9b773vwbhmjurk";
const accessToken = "5z79quc5s7z0qbiiqmalqd4ueh4lue";
const apiUrl = "https://api.twitch.tv/helix/clips?broadcaster_id=42365125"
const headers = {
method: "GET",
headers: {
"client-id": clientId,
Authorization: "Bearer " + accessToken,
},
};
useEffect(() => {
const getData = async () => {
setIsLoading(true);
try {
const {data: result} = await axios.get(apiUrl, headers);
setVideoData(result.data);
console.log(result.data)
} catch (err) {
console.error(err.message);
}
setIsLoading(false);
}
getData();
console.log(videoData);
}, []);
return (
<div>
Welcome
{isLoading && <div>Loading content...</div>}
{!isLoading && (
<div>
<h2>Processing video data</h2>
{videoData.map(clip => (<span>{clip.id}</span>))}
</div>
)}
</div>
);
};
I’m trying to create an array from the Twitch response data. The API returns an object that contains an array inside it. I want to use map to iterate through all the streamer’s videos so I can show them on my website. The console shows the data but I still get the map error.