@larissa
To play a YouTube video in React.js, you can follow these steps:
- Install the react-youtube package using a package manager like npm or yarn:
1
|
npm install react-youtube
|
- Import the YouTube component from the react-youtube package:
1
|
import YouTube from 'react-youtube';
|
- Create a functional component and define the YouTube video ID that you want to play:
1
2
3
4
5
6
7
8
9
|
const MyVideoPlayer = () => {
const videoId = 'abcdef'; // Replace with your desired YouTube video ID
return (
<div>
<YouTube videoId={videoId} />
</div>
);
}
|
- Render the MyVideoPlayer component in your main React component:
1
|
ReactDOM.render(<MyVideoPlayer />, document.getElementById('root'));
|
This will render the YouTube video player with the specified video ID on your webpage. Make sure to replace 'abcdef'
with the actual ID of the YouTube video you want to play.