Last active
November 6, 2020 11:06
-
-
Save codebubb/d728e70c0a167f04e086f9537894a949 to your computer and use it in GitHub Desktop.
React YouTube Viewer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { useState, useEffect } from 'react'; | |
| import { Search } from './Search'; | |
| import { Video } from './Video'; | |
| export function App() { | |
| const [currentChannelId, setCurrentChannelId] = useState(); | |
| const [videos, setVideos] = useState([]); | |
| const baseUrl = 'https://api.rss2json.com/v1/api.json?rss_url=https%3A%2F%2Fwww.youtube.com%2Ffeeds%2Fvideos.xml%3Fchannel_id%3D'; | |
| useEffect(() => { | |
| (async () => { | |
| if (currentChannelId) { | |
| try { | |
| const data = await fetch(`${baseUrl}${currentChannelId}`).then(response => response.json()); | |
| setVideos(data.items); | |
| } catch (error) { | |
| console.log(error); | |
| } | |
| } | |
| })(); | |
| }, [currentChannelId]); | |
| return ( | |
| <div className="app-container"> | |
| <h1>Latest YouTube Videos</h1> | |
| <Search setCurrentChannelId={id => setCurrentChannelId(id)} /> | |
| <div className="videos"> | |
| {videos.map(video => <Video key={video.guid} video={video} />)} | |
| </div> | |
| </div> | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment