Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Search YouTube with the API, rendering the result with React & Hooks

NotificationsYou must be signed in to change notification settings

JasonToups/YouTube-React-Hooks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Link toReact Tube App

This application is built in React to search the YouTube API and return a short list of 5 videos, utilizing Primitive Hooks & Custom Hooks to fetch data.

The first video shows up in preview in the VideoDetail Component, and the full list is selectable as thumbnails to the side of the main video preview in the VideoList Component.

React Tube Screenshot

Hooks: Primitive & Custom

I've used two Primitive Hooks in this application, and here's how they were used:

SearchBar.js - Primitive Hook

  • useState()

I useduseState() to handle the state of the SearchBar Component input, to track the term that's being entered in the form to search the YouTube API.

constSearchBar=({ onFormSubmit})=>{const[term,setTerm]=useState('')constonSubmit=event=>{event.preventDefault();onFormSubmit(term);};

Here's the form & input JSX in the return statement of the SearchBar Component function:

<formonSubmit={onSubmit}className='form'><divclassName='field'><label>Video Search</label><inputtype='text'value={term}onChange={(event)=>setTerm(event.target.value)}/></div></form>

When the input field changes, theterm state is updated as well.

App.js - Custom & Primitive Hooks

  • useState()
  • useEffect()

I also useduseState() in the App Component function to handle storing the array of videos sent back from the YouTube API, along with tracking the currently selected video in the VideoDetail Component.

const[selectedVideo,setSelectedVideo]=useState(null);const[videos,search]=useVideos('react js');

useVideos() is aCustom Hook that I wrote, to handle fetching videos.

By using the Primitive Hooks,useState &useEffect, I wrote a hook that allows us to not only set the default term of the API query by passing in an argument, but also passing in new search terms when another request is made. This makes the custom hook reusable whenever a list of videos are needed from an API.

It's handy to create custom hooks when fetching data. This happens frequently in apps of a larger scale, so I placeduseVideos in the/hooks directory.

constuseVideos=(defaultSearchTerm)=>{const[videos,setVideos]=useState([]);useEffect(()=>{search(defaultSearchTerm);},[defaultSearchTerm]);constsearch=asyncterm=>{// the youtube function is imported from the /api folderconstresponse=awaityoutube.get('/search',{params:{q:term,part:'snippet',maxResults:5,type:'video',key:process.env.REACT_APP_KEY,},});setVideos(response.data.items);};return[videos,search];};

useEffect() is a Lifecycle Hook that is called when the App Component mounts initially. It submits the default search term we passed as anargument intouseVideos as the search term to make an API request.


This project was bootstrapped withCreate React App.

Available Scripts

In the project directory, you can run:

npm start

Runs the app in the development mode.
Openhttp://localhost:3000 to view it in the browser.

The page will reload if you make edits.
You will also see any lint errors in the console.

npm test

Launches the test runner in the interactive watch mode.
See the section aboutrunning tests for more information.

npm run build

Builds the app for production to thebuild folder.
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.
Your app is ready to be deployed!

See the section aboutdeployment for more information.

npm run eject

Note: this is a one-way operation. Once youeject, you can’t go back!

If you aren’t satisfied with the build tool and configuration choices, you caneject at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands excepteject will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever useeject. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.


[8]ページ先頭

©2009-2025 Movatter.jp