am using an youtube api of rapidapi developing this project through javascript mastery youtube channel. am getting an error of null reading snippet. The below code is from VideoDetail component.
I am currently working on developing a YouTube clone using RapidAPI's YouTube-V3 API. However, I am encountering an error related to "null reading snippet." I am seeking assistance to understand the cause of this error and find a solution. Any insights or guidance would be greatly appreciated. Thank you in advance for your help!
import { useState, useEffect } from "react";
import { Link, useParams } from "react-router-dom";
import ReactPlayer from "react-player";
import { Box, Typography, Stack } from "@mui/material";
import { CheckCircle } from "@mui/icons-material";
import { Videos } from "./";
import { fetchFromAPI } from "../utils/FetchFromAPI";
const VideoDetail = () => {
const [videoDetail, setVideoDetail] = useState(null);
const { id } = useParams;
useEffect(() => {
fetchFromAPI(`videos?part=snippet,statistics&id=${id}`).then((data) =>
setVideoDetail(data.items[0])
);
}, [id]);
// if (!videoDetail?.snippet) return "Loading...";
return (
<Box minHeight="95vh">
<Stack direction={{ xs: "column", md: "row" }}>
<Box flex={1}>
<Box sx={{ width: "100%", position: "sticky", top: "86px" }}>
<ReactPlayer
url={`https://www.youtube.com/watch?v=${id}`}
className="react-player"
controls
/>
<Typography color="#fff" variant="h5" fontWeight="bold" p={2}>
{videoDetail.snippet.title}
</Typography>
</Box>
</Box>
</Stack>
</Box>
);
};
export default VideoDetail;
`
this is fetchApi code where am using the rapidapi youtube-V3 api.
import axios from "axios";
const BASE_URL = "https://youtube-v31.p.rapidapi.com";
const options = {
params: {
maxResult: "100",
},
headers: {
"X-RapidAPI-Key": process.env.REACT_APP_RAPID_API_KEY,
"X-RapidAPI-Host": "youtube-v31.p.rapidapi.com",
},
};
export const fetchFromAPI = async (url) => {
const { data } = await axios.get(`${BASE_URL}/${url}`, options);
return data;
};