49
loading...
This website collects cookies to deliver better user experience
npx create-react-app videos
cd videos
npm start
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.querySelector('#root'));
<div>
element with id, root in public/index.html. This <div>
element is the React container node. When the render() function is first called, the whole of the container node is replaced with App component.import React from 'react';
function App(props) {
return ();
}
<Component />
tag.import React from 'react';
class App extends React.Component {
render() {
return ();
}
}
state
, as key-value pairs.setState()
. when setState() is called, React knows the state has changed, and calls the render() method again to learn what should be on the screen.
For example,
state = {selectedVideo: null}
//Wrong
this.state.selectedVideo = 'Something'
//Correct
this.setState({ selectedVideo: 'Something' })
<h1>{ 2 + 5 * 8 }</h1>
<p> Current Selected Video is : {this.state.selectedVideo} </p>
<VideoDetail video={this.state.selectedVideo} />
video
is a defined prop here and contains selectedVideo data. Then we can pass data with props like we’re giving an argument to a function:const VideoDetail = (props) => {
// code
}
this.props.video
<link rel = 'stylesheet' href = "https://cdnjs.cloudflare.com/ajax/libs/semanticui/2.4.1/semantic.min.css"
integrity = "sha512-8bHTC73gkZ7rZ7vpqUQThUDhqcNFyYi2xgDgPDHc+GXVGHXq+xPjynxIopALmOPqzo9JZj0k6OqqewdGO3EsrQ==" crossorigin = "anonymous" />
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
ReactDOM.render(<App />, document.querySelector('#root'));
import axios from 'axios';
// create your youtube api key and place it here
const KEY = YOUR_API_KEY;
// setting up base instance in which you can define a URL and
// any other configuration elements.
// and exporting as default.
export default axios.create({
baseURL: 'https://www.googleapis.com/youtube/v3',
params: {
part: 'snippet',
maxResults: 5,
key: KEY
}
});
// we can import this instance from other files and no longer need to write
// whole URL everytime we call API
import './VideoItem.css';
import React from 'react';
// takes props video object and onVideoSelect function
// we call onVideoSelect when user clicks the video
// What does a VideoItem Component contains?
// yepp, Video Thumbnail and Video title
const VideoItem = ({ video, onVideoSelect }) => {
return (
<div onClick={() => onVideoSelect(video)} className="item video-item">
<img alt={video.snippet.title} className="ui image" src={video.snippet.thumbnails.medium.url} />
<div className="content">
<div className="header">{video.snippet.title}</div>
</div>
</div>
);
}
export default VideoItem;
.video-item{
display: flex !important;
align-items: center !important;
cursor: pointer;
}
.video-item.item img{
max-width: 180px;
}
import React from 'react';
import VideoItem from './VideoItem';
// takes videos (video array) and onVideoSelect function
// What does a VideoList contain?
// yepp, List of Video (specifically VideoItems)
// So we iterate over videos array and make a VideoItem for each
// Note We are passing video and OnVideoSelect as props to VideoItem
const VideoList = ({ videos, onVideoSelect }) => {
const renderedList = videos.map((video) => {
return <VideoItem key={video.id.videoId} onVideoSelect={onVideoSelect} video={video} />;
})
return <div className="ui relaxed divided list">{renderedList}</div>;
}
export default VideoList;
import React from 'react';
// videoDetail takes the selectedVideo and displays its info.
const VideoDetail = ({ video }) => {
if (!video) {
return <div>Loading...</div>;
}
//This url is for fetching selectedVideo
const videoSrc = `https://www.youtube.com/embed/${video.id.videoId}`;
return (
<div>
<div className="ui embed">
<iframe title="video player" src={videoSrc} />
</div>
<div className="ui segment">
<h4 className="ui header">{video.snippet.title}</h4>
<p>{video.snippet.description}</p>
</div>
</div>
);
}
export default VideoDetail;
import React from 'react';
// state variable "term" stores what user types in searchBar
class SearchBar extends React.Component {
state = { term: '' };
// this sets the "term" to what user types in. (in sync)
onInputChange = (e) => {
this.setState({ term: e.target.value });
}
// it is called when user submits the "term"
// which in turn calls the onTermSubmit() function passed as its prop
onSearchBarSubmit = (e) => {
e.preventDefault();
this.props.onTermSubmit(this.state.term);
}
render() {
return (
<div className="ui segment search-bar" style={{ marginTop: '20px' }}>
<form onSubmit={this.onSearchBarSubmit} className="ui form">
<div className="field">
<label>Video Search</label>
<input
style={{ backgroundColor: 'whitesmoke' }}
type="text" value={this.state.term}
onChange={this.onInputChange}
/>
</div>
</form>
</div>
);
}
}
export default SearchBar;
import React from 'react';
import SearchBar from './SearchBar';
import VideoList from './VideoList';
import youtube from '../apis/youtube';
import VideoDetail from './VideoDetail';
class App extends React.Component {
state = { videos: [], selectedVideo: null };
// videos - array of videos based on term that user passed in searchbar (initally empty)
// selectedVideo - video selected to display on left
// this lifecycle method is called when App component gets mounted
componentDidMount() {
this.onTermSubmit('dev.to');
}
// Note that here 'dev.to' is initial term for which videos will be searched
// It is random
// This function is the one that accepts the term and fetches videos
// and set "videos" state variable to fetched videos and
// selectedVideo to the first video of videos
onTermSubmit = async (term) => {
const response = await youtube.get('/search', {
params: {
q: term
}
});
this.setState({ videos: response.data.items, selectedVideo: response.data.items[0] });
}
onVideoSelect = (video) => {
this.setState({ selectedVideo: video });
};
render() {
return (
<div className="ui container" style={{ backgroundColor: 'whitesmoke', padding: '40px' }}>
<SearchBar onTermSubmit={this.onTermSubmit} />
<div className="ui grid">
<div className="ui row">
<div className="eleven wide column">
<VideoDetail video={this.state.selectedVideo} />
</div>
<div className="five wide column">
<VideoList onVideoSelect={this.onVideoSelect} videos={this.state.videos} />
</div>
</div>
</div>
</div>
);
}
}
export default App;