Skip to content

Instantly share code, notes, and snippets.

@codebubb
Last active November 6, 2020 11:06
Show Gist options
  • Select an option

  • Save codebubb/d728e70c0a167f04e086f9537894a949 to your computer and use it in GitHub Desktop.

Select an option

Save codebubb/d728e70c0a167f04e086f9537894a949 to your computer and use it in GitHub Desktop.
React YouTube Viewer
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