I am building a detail's page to display the details of a selected object within my json file.
(click on a thumbnail image that takes you to the video page, which includes the video's details)
So far, I have 1) The data file 2) The home page where you will select the item you want to see more information off 3) The page that displays the information.
However, I have not been able to display any json data on the details page. I have tried using the "match" method, I used Request.URL(), both which did not work, but I think the right way to do it is with the useEffect hook (please correct me if I am wrong)
This is what I have at the moment with the useEffect route:
pages/FeaturedVideo.js:
import videos from '../../data/data.json'
const FeaturedVideo = () => {
const [videos, setVideos] = useState(null);
useEffect(() => {
let url = "/src/data/data.json";
fetch(url)
.then(res => res.json())
.then(videos => setVideos(videos))
}, []);
return(
<div>
<p>The video and it's details will go here </p>
{videos.id.map((video) =>{
<div key={video.id} {...videos}></div>
})}
</div>
)
}
export default FeaturedVideo;
What I am trying to do above is to use fetch to call the json file as if it were an API. And then map out the data from the specific video id.
The json file is structured in the following way:
[
{
"id": 2,
"firstLogo": "Minecraft",
"thumbnail": "Thumbnail",
"available": "Unlock 12/6",
"sample": "https://www.youtube.com/watch?v=c_dG_HxHMVI"
},
{
"id": 3,
"firstLogo": "Batman",
"thumbnail": "Thumbnail",
"available": "Unlock 12/13",
"sample": "https://www.youtube.com/watch?v=c_dG_HxHMVI"
}
]
The :id of the video is included into the path in the following way:
<Router>
<Navbar/>
<Switch>
<Route path="/:videoID">
<FeaturedVideo/>
</Route>
<Route exact path="/">
<Home />
</Route>
</Switch>
<Footer />
</Router>
```re