{"id":28009,"date":"2022-03-24T20:13:27","date_gmt":"2022-03-24T20:13:27","guid":{"rendered":"http:\/\/embed-a-youtube-player-into-a-react-app"},"modified":"2025-02-09T04:25:15","modified_gmt":"2025-02-09T12:25:15","slug":"embed-a-youtube-player-into-a-react-app","status":"publish","type":"post","link":"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/","title":{"rendered":"Embed a YouTube Player into a React App"},"content":{"rendered":"<div class=\"wp-block-cloudinary-markdown \"><p><a href=\"https:\/\/www.youtube.com\/\">YouTube<\/a> is a video-sharing platform owned by Google. The YouTube team offers an API that can be used to integrate the YouTube video playback experience into web applications.\nIn this post, we will go through the process of embedding a YouTube player in a <a href=\"https:\/\/reactjs.org\/\">React<\/a> application. We\u2019ll build a simple blog and use the <code>react-youtube<\/code> package to embed a YouTube player in it. <code>react-youtube<\/code> provides a React component that acts as an abstraction layer over the traditional YouTube IFrame Player API. Let\u2019s get started!<\/p>\n<p>Here is a <a href=\"https:\/\/codesandbox.io\/s\/embed-youtube-player-oh5lrq\">link<\/a> to the demo on CodeSandbox.<\/p>\n<\/div>\n  \n  <div class=\"wp-block-cloudinary-code-sandbox \">\n    <iframe\n      src=\"https:\/\/codesandbox.io\/embed\/embed-youtube-player-oh5lrq?theme=dark&amp;codemirror=1&amp;highlights=&amp;editorsize=50&amp;fontsize=14&amp;expanddevtools=0&amp;hidedevtools=0&amp;eslint=0&amp;forcerefresh=0&amp;hidenavigation=0&amp;initialpath=%2F&amp;module=&amp;moduleview=0&amp;previewwindow=&amp;view=&amp;runonclick=1\"\n      height=\"500\"\n      style=\"width: 100%;\"\n      title=\"embed-youtube-player-oh5lrq\"\n      loading=\"lazy\"\n      allow=\"accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking\"\n      sandbox=\"allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts\"\n    ><\/iframe>\n  <\/div>\n\n  <div class=\"wp-block-cloudinary-markdown \"><h2>Project Setup<\/h2>\n<p>Run this command in your terminal to create a simple React application:<\/p>\n<pre class=\"js-syntax-highlighted\"><span><code class=\"hljs shcb-wrap-lines\">    npx create-react-app youtube-player-demo\n<\/code><\/span><\/pre>\n<p>Next, navigate into the newly created directory and run the command below to install the dependencies we\u2019ll need for our app.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\">    <span class=\"hljs-selector-tag\">npm<\/span> <span class=\"hljs-selector-tag\">install<\/span> <span class=\"hljs-selector-tag\">react-youtube<\/span> <span class=\"hljs-selector-tag\">react-router-dom<\/span><span class=\"hljs-keyword\">@6<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>After the installation, create a folder called <code>components<\/code> in the <code>\/src<\/code> directory to hold the application\u2019s components.<\/p>\n<h2>Building the Blog<\/h2>\n<p>First, let\u2019s create a component called <code>Home<\/code> that will serve as the index page and hold a list of all blog posts in the application. Create a file called <code>Home.js<\/code> in the <code>src\/components<\/code> folder and add the following to it:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">    <span class=\"hljs-keyword\">import<\/span> React <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n    <span class=\"hljs-keyword\">import<\/span> { Link } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-router-dom'<\/span>;\n    <span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">Home<\/span>(<span class=\"hljs-params\">{ data }<\/span>) <\/span>{\n      <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"cards\"<\/span>&gt;<\/span>\n          {data.posts.map((post) =&gt; (\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"card\"<\/span> <span class=\"hljs-attr\">key<\/span>=<span class=\"hljs-string\">{post.id}<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"img-wrapper\"<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">img<\/span>\n                  <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">{<\/span>`<span class=\"hljs-attr\">https:<\/span>\/\/<span class=\"hljs-attr\">ytimg.googleusercontent.com<\/span>\/<span class=\"hljs-attr\">vi<\/span>\/${\n                    <span class=\"hljs-attr\">post.url.split<\/span>('<span class=\"hljs-attr\">v<\/span>=<span class=\"hljs-string\">')&#91;1]\n                  }\/sddefault.jpg`}\n                  alt={post.title}\n                &gt;&lt;\/img&gt;\n              &lt;\/div&gt;\n              &lt;div&gt;\n                &lt;h3&gt;{post.title}&lt;\/h3&gt;\n                &lt;p&gt;{post.content.slice(0, 100) + '<\/span><span class=\"hljs-attr\">...<\/span>'}&lt;\/<span class=\"hljs-attr\">p<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Link<\/span> <span class=\"hljs-attr\">to<\/span>=<span class=\"hljs-string\">{<\/span>`\/<span class=\"hljs-attr\">blog<\/span>\/${<span class=\"hljs-attr\">post.id<\/span>}`}&gt;<\/span>Learn more...<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">Link<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n          ))}\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n      );\n    }\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>The <code>Home<\/code> component accepts a <code>data<\/code> prop and renders a list of blog post cards. Each card has an automatically generated YouTube video thumbnail, details about the post, and a link to the post\u2019s page.<\/p>\n<p>To keep things simple, we will be working with locally generated sample data. Create a file called <code>data.json<\/code> in the <code>\/src<\/code> directory and populate it with the JSON data from this <a href=\"https:\/\/codesandbox.io\/s\/embed-youtube-player-oh5lrq?file=\/src\/data.json\">codeSandbox file<\/a>.<\/p>\n<p>The file holds a <code>JSON<\/code> object made up of an array of sample posts. Now replace the code in your <code>App.js<\/code> file with the following:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">    <span class=\"hljs-keyword\">import<\/span> { Routes, Route, Link } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-router-dom'<\/span>;\n    <span class=\"hljs-keyword\">import<\/span> Home <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/Home'<\/span>;\n    <span class=\"hljs-keyword\">import<\/span> BlogPost <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/BlogPost'<\/span>;\n    <span class=\"hljs-keyword\">import<\/span> data <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/data.json'<\/span>;\n    <span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">'.\/App.css'<\/span>;\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">App<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n      <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"App\"<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Link<\/span> <span class=\"hljs-attr\">to<\/span>=<span class=\"hljs-string\">\"\/\"<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>React Blog<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">Link<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Routes<\/span>&gt;<\/span>\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Route<\/span> <span class=\"hljs-attr\">path<\/span>=<span class=\"hljs-string\">\"\/\"<\/span> <span class=\"hljs-attr\">element<\/span>=<span class=\"hljs-string\">{<\/span>&lt;<span class=\"hljs-attr\">Home<\/span> <span class=\"hljs-attr\">data<\/span>=<span class=\"hljs-string\">{data}<\/span> \/&gt;<\/span>} \/&gt;\n              <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Route<\/span> <span class=\"hljs-attr\">path<\/span>=<span class=\"hljs-string\">\"\/blogpost\/:id\"<\/span> <span class=\"hljs-attr\">element<\/span>=<span class=\"hljs-string\">{<\/span>&lt;<span class=\"hljs-attr\">BlogPost<\/span> <span class=\"hljs-attr\">data<\/span>=<span class=\"hljs-string\">{data}<\/span> \/&gt;<\/span>} \/&gt;\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">Routes<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n      );\n    }\n    <span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> App;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>We imported the basic routing components, the previously created <code>Home<\/code> component, and the sample data in the code above. Then we configured our route inside the <code>App<\/code> component. <code>Route<\/code> accepts an element prop set to the <code>Home<\/code> component.<\/p>\n<p>With that done, we need to wrap our application with the <code>BrowserRouter<\/code> component as required by <a href=\"https:\/\/reactrouter.com\/\">React Router<\/a>. Update your <code>index.js<\/code> file in the <code>\/src<\/code> folder with the following:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">    <span class=\"hljs-keyword\">import<\/span> React <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n    <span class=\"hljs-keyword\">import<\/span> ReactDOM <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-dom'<\/span>;\n    <span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">'.\/index.css'<\/span>;\n    <span class=\"hljs-keyword\">import<\/span> App <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/App'<\/span>;\n    <span class=\"hljs-keyword\">import<\/span> { BrowserRouter } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-router-dom'<\/span>;\n    ReactDOM.render(\n      <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">React.StrictMode<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">BrowserRouter<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">App<\/span> \/&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">BrowserRouter<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">React.StrictMode<\/span>&gt;<\/span><\/span>,\n      <span class=\"hljs-built_in\">document<\/span>.getElementById(<span class=\"hljs-string\">'root'<\/span>)\n    );\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Let\u2019s add some styles to give the application a decent look. Add the following to your <code>App.css<\/code> file:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\">    <span class=\"hljs-selector-class\">.App<\/span> {\n      <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">1024px<\/span>;\n      <span class=\"hljs-attribute\">max-width<\/span>: <span class=\"hljs-number\">90%<\/span>;\n      <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span> auto;\n    }\n    <span class=\"hljs-selector-class\">.App<\/span> &gt; <span class=\"hljs-selector-tag\">div<\/span> {\n      <span class=\"hljs-attribute\">display<\/span>: flex;\n      <span class=\"hljs-attribute\">justify-content<\/span>: center;\n    }\n    <span class=\"hljs-selector-class\">.App<\/span> &gt; <span class=\"hljs-selector-tag\">div<\/span> &gt; <span class=\"hljs-selector-tag\">a<\/span> {\n      <span class=\"hljs-attribute\">color<\/span>: inherit;\n      <span class=\"hljs-attribute\">text-decoration<\/span>: none;\n    }\n    <span class=\"hljs-selector-class\">.cards<\/span> {\n      <span class=\"hljs-attribute\">display<\/span>: grid;\n      <span class=\"hljs-attribute\">grid-template-columns<\/span>: <span class=\"hljs-built_in\">repeat<\/span>(auto-fit, minmax(<span class=\"hljs-number\">250px<\/span>, <span class=\"hljs-number\">1<\/span>fr));\n      <span class=\"hljs-attribute\">grid-gap<\/span>: <span class=\"hljs-number\">2rem<\/span>;\n    }\n    <span class=\"hljs-selector-class\">.card<\/span> {\n      <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">20rem<\/span>;\n      <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">1px<\/span> solid <span class=\"hljs-built_in\">rgba<\/span>(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0.1<\/span>);\n    }\n    <span class=\"hljs-selector-class\">.card<\/span> &gt; <span class=\"hljs-selector-class\">.img-wrapper<\/span> {\n      <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">50%<\/span>;\n    }\n    <span class=\"hljs-selector-class\">.card<\/span> &gt; <span class=\"hljs-selector-class\">.img-wrapper<\/span> &gt; <span class=\"hljs-selector-tag\">img<\/span> {\n      <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;\n      <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">100%<\/span>;\n      <span class=\"hljs-attribute\">object-fit<\/span>: cover;\n    }\n    <span class=\"hljs-selector-class\">.card<\/span> &gt; <span class=\"hljs-selector-tag\">div<\/span><span class=\"hljs-selector-pseudo\">:last-child<\/span> {\n      <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">0.5rem<\/span>;\n      <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">50%<\/span>;\n    }\n    <span class=\"hljs-selector-class\">.card<\/span> &gt; <span class=\"hljs-selector-tag\">div<\/span> &gt; <span class=\"hljs-selector-tag\">h3<\/span> {\n      <span class=\"hljs-attribute\">text-align<\/span>: center;\n      <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-number\">0.9rem<\/span>;\n    }\n    <span class=\"hljs-selector-class\">.card<\/span> &gt; <span class=\"hljs-selector-tag\">div<\/span> &gt; <span class=\"hljs-selector-tag\">p<\/span> {\n      <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-number\">0.8rem<\/span>;\n      <span class=\"hljs-attribute\">color<\/span>: <span class=\"hljs-number\">#555<\/span>;\n    }\n    <span class=\"hljs-selector-class\">.card<\/span> &gt; <span class=\"hljs-selector-tag\">div<\/span> &gt; <span class=\"hljs-selector-tag\">a<\/span> {\n      <span class=\"hljs-attribute\">text-decoration<\/span>: none;\n      <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">0.2rem<\/span> <span class=\"hljs-number\">0.5rem<\/span>;\n      <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">1px<\/span> solid <span class=\"hljs-number\">#4cf<\/span>;\n      <span class=\"hljs-attribute\">color<\/span>: inherit;\n      <span class=\"hljs-attribute\">font-weight<\/span>: <span class=\"hljs-number\">700<\/span>;\n      <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-number\">0.8rem<\/span>;\n      <span class=\"hljs-attribute\">display<\/span>: inline-block;\n    }\n    <span class=\"hljs-selector-class\">.blog<\/span> &gt; <span class=\"hljs-selector-tag\">h3<\/span> {\n      <span class=\"hljs-attribute\">margin-bottom<\/span>: <span class=\"hljs-number\">1rem<\/span>;\n    }\n    <span class=\"hljs-selector-class\">.blog<\/span> * {\n      <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;\n    }\n    <span class=\"hljs-selector-class\">.btn<\/span> {\n      <span class=\"hljs-attribute\">display<\/span>: inline;\n      <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">0.5rem<\/span> <span class=\"hljs-number\">2rem<\/span>;\n      <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0.7rem<\/span> <span class=\"hljs-number\">0<\/span>;\n      <span class=\"hljs-attribute\">width<\/span>: auto;\n      <span class=\"hljs-attribute\">background<\/span>: <span class=\"hljs-number\">#fff<\/span>;\n      <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">1px<\/span> solid <span class=\"hljs-number\">#4cf<\/span>;\n      <span class=\"hljs-attribute\">cursor<\/span>: pointer;\n      <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-number\">1rem<\/span>;\n      <span class=\"hljs-attribute\">font-weight<\/span>: <span class=\"hljs-number\">600<\/span>;\n    }\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Save the changes, and your application should now look like this:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/cloudinary-marketing-res.cloudinary.com\/image\/upload\/c_limit,w_2000\/f_auto\/q_auto\/media_jams\/s_90A3F15235320E5EE15BEFBCFE29C22CB59EFE7F6515BF3E50471B1B5BA60AA3_1646253726815_CleanShot+2022-03-03+at+00.41.482x.png\" alt=\"\" loading=\"lazy\" class=\"c-transformed-asset\"  width=\"2000\" height=\"980\"\/><\/p>\n<h2>Embed YouTube Player<\/h2>\n<p>The <code>react-youtube<\/code> package simplifies the integration of the YouTube player into React applications by providing a utility React component, <code>&lt;YouTube \/&gt;<\/code>, which accepts a <code>videoId<\/code> prop, an <code>onReady<\/code> prop, and some other optional props. The <code>videoId<\/code> prop gives the <code>id<\/code> of a particular YouTube video, while  <code>onReady<\/code>  hold a function that will be executed when the player is ready.<\/p>\n<p>The following code illustrates the basic use of the component as well as its available props:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">    &lt;YouTube\n      videoId={string} <span class=\"hljs-comment\">\/\/ defaults -&gt; null<\/span>\n      id={string} <span class=\"hljs-comment\">\/\/ defaults -&gt; null<\/span>\n      className={string} <span class=\"hljs-comment\">\/\/ defaults -&gt; null<\/span>\n      containerClassName={string} <span class=\"hljs-comment\">\/\/ defaults -&gt; ''<\/span>\n      title={string} <span class=\"hljs-comment\">\/\/ defaults -&gt; null<\/span>\n      opts={obj} <span class=\"hljs-comment\">\/\/ defaults -&gt; {}<\/span>\n      onReady={func} <span class=\"hljs-comment\">\/\/ defaults -&gt; noop<\/span>\n      onPlay={func} <span class=\"hljs-comment\">\/\/ defaults -&gt; noop<\/span>\n      onPause={func} <span class=\"hljs-comment\">\/\/ defaults -&gt; noop<\/span>\n      onEnd={func} <span class=\"hljs-comment\">\/\/ defaults -&gt; noop<\/span>\n      onError={func} <span class=\"hljs-comment\">\/\/ defaults -&gt; noop<\/span>\n      onStateChange={func} <span class=\"hljs-comment\">\/\/ defaults -&gt; noop<\/span>\n      onPlaybackRateChange={func} <span class=\"hljs-comment\">\/\/ defaults -&gt; noop<\/span>\n      onPlaybackQualityChange={func} <span class=\"hljs-comment\">\/\/ defaults -&gt; noop<\/span>\n    \/&gt;;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Open your <code>App.js<\/code> file and add a dynamic route to configure a dynamic path based on the <code>id<\/code> of each blog post.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">    <span class=\"hljs-keyword\">import<\/span> BlogPost <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/BlogPost'<\/span>;\n   \n    <span class=\"hljs-comment\">\/\/...<\/span>\n    \n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Routes<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Route<\/span> <span class=\"hljs-attr\">path<\/span>=<span class=\"hljs-string\">\"\/\"<\/span> <span class=\"hljs-attr\">element<\/span>=<span class=\"hljs-string\">{<\/span>&lt;<span class=\"hljs-attr\">Home<\/span> <span class=\"hljs-attr\">data<\/span>=<span class=\"hljs-string\">{data}<\/span> \/&gt;<\/span>} \/&gt;\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Route<\/span> <span class=\"hljs-attr\">path<\/span>=<span class=\"hljs-string\">\"\/blogpost\/:id\"<\/span> <span class=\"hljs-attr\">element<\/span>=<span class=\"hljs-string\">{<\/span>&lt;<span class=\"hljs-attr\">BlogPost<\/span> <span class=\"hljs-attr\">data<\/span>=<span class=\"hljs-string\">{data}<\/span> \/&gt;<\/span>} \/&gt;\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">Routes<\/span>&gt;<\/span><\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Now, let\u2019s create a  <code>BlogPost.js<\/code> file in the <code>src\/components<\/code> directory and add the following to it:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">    <span class=\"hljs-keyword\">import<\/span> React <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n    <span class=\"hljs-keyword\">import<\/span> YouTube <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-youtube'<\/span>;\n    <span class=\"hljs-keyword\">import<\/span> { useParams } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-router'<\/span>;\n    <span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">BlogPost<\/span>(<span class=\"hljs-params\">{ data }<\/span>) <\/span>{\n      <span class=\"hljs-keyword\">const<\/span> params = useParams();\n      <span class=\"hljs-keyword\">const<\/span> post = data.posts.find(<span class=\"hljs-function\">(<span class=\"hljs-params\">dataItem<\/span>) =&gt;<\/span> dataItem.id === params.id);\n      <span class=\"hljs-keyword\">const<\/span> youtubeID = post.url.split(<span class=\"hljs-string\">'v='<\/span>)&#91;<span class=\"hljs-number\">1<\/span>];\n      <span class=\"hljs-keyword\">const<\/span> onReady = <span class=\"hljs-function\">(<span class=\"hljs-params\">e<\/span>) =&gt;<\/span> {\n        <span class=\"hljs-built_in\">console<\/span>.log(e.target);\n      };\n      <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"blog\"<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h3<\/span>&gt;<\/span>{post.title}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h3<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>{post.content}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"player\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">YouTube<\/span> <span class=\"hljs-attr\">videoId<\/span>=<span class=\"hljs-string\">{youtubeID}<\/span> <span class=\"hljs-attr\">onReady<\/span>=<span class=\"hljs-string\">{onReady}<\/span> \/&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n      );\n    }\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>We imported the <code>useParams<\/code> hook, which is used to extract the <code>params<\/code> object from the dynamic path. Then we created a <code>BlogPost<\/code> component that accepts our sample local data as a prop. The data is then traversed to find the post object whose id matches the params id.<\/p>\n<p>The video id of a YouTube video is usually contained in the video URL, mostly specified as a query parameter <code>v<\/code>. We performed some JavaScript string manipulation to extract the video id from the URL. The component then renders some content and, most importantly, the YouTube Player that takes the <code>videoId<\/code> and an <code>onReady<\/code> prop as props.<\/p>\n<p>To view a blog post, save the changes and click on any blog post on the home page. You should see the embedded YouTube Player with an already loaded video.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/cloudinary-marketing-res.cloudinary.com\/image\/upload\/c_limit,w_2000\/f_auto\/q_auto\/media_jams\/s_90A3F15235320E5EE15BEFBCFE29C22CB59EFE7F6515BF3E50471B1B5BA60AA3_1646253769891_CleanShot+2022-03-03+at+00.42.352x.png\" alt=\"\" loading=\"lazy\" class=\"c-transformed-asset\"  width=\"2000\" height=\"907\"\/><\/p>\n<h2>Customizing the Player<\/h2>\n<p>The YouTube IFrame API provides a lot of freedom by defining many parameters that make the YouTube player more customizable. For example, you can easily define the player controls externally, set the video to play repeatedly, cause the player to play the loaded video automatically after rendering, etc.<\/p>\n<p>However, the <code>react-youtube<\/code> package has an <code>opts<\/code> object prop that can help manage the video player\u2019s preferences. The <code>opts<\/code> prop has a <code>playerVars<\/code> nested object that allows you to utilize the usual YouTube IFrame API parameters and set a custom <code>width<\/code> and <code>height<\/code> for the player.<\/p>\n<p>Let\u2019s look at some of the customization possibilities.<\/p>\n<h3>Define External Player Controls<\/h3>\n<p>Users can disable the default player controls and then create their logic by altering the API\u2019s state. For simplicity, we will include only the pause and play controls.<\/p>\n<p>Update the code in your <code>BlogPost.js<\/code> file to look like this:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">    <span class=\"hljs-keyword\">import<\/span> React, { useState } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n    <span class=\"hljs-keyword\">import<\/span> YouTube <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-youtube'<\/span>;\n    <span class=\"hljs-keyword\">import<\/span> { useParams } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-router'<\/span>;\n    <span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">BlogPost<\/span>(<span class=\"hljs-params\">{ data }<\/span>) <\/span>{\n      <span class=\"hljs-keyword\">const<\/span> &#91;player, setPlayer] = useState(<span class=\"hljs-literal\">null<\/span>);\n      <span class=\"hljs-keyword\">const<\/span> params = useParams();\n      <span class=\"hljs-keyword\">const<\/span> post = data.posts.find(<span class=\"hljs-function\">(<span class=\"hljs-params\">dataItem<\/span>) =&gt;<\/span> dataItem.id === params.id);\n      <span class=\"hljs-keyword\">const<\/span> youtubeID = post.url.split(<span class=\"hljs-string\">'v='<\/span>)&#91;<span class=\"hljs-number\">1<\/span>];\n      <span class=\"hljs-keyword\">const<\/span> onReady = <span class=\"hljs-function\">(<span class=\"hljs-params\">e<\/span>) =&gt;<\/span> {\n        setPlayer(e.target);\n      };\n      <span class=\"hljs-keyword\">const<\/span> onPlayHandler = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n        player.playVideo();\n      };\n      <span class=\"hljs-keyword\">const<\/span> onPauseHandler = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n        player.pauseVideo();\n      };\n      <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"blog\"<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h3<\/span>&gt;<\/span>{post.title}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h3<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>{post.content}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"player\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">YouTube<\/span>\n              <span class=\"hljs-attr\">videoId<\/span>=<span class=\"hljs-string\">{youtubeID}<\/span>\n              <span class=\"hljs-attr\">onReady<\/span>=<span class=\"hljs-string\">{onReady}<\/span>\n              <span class=\"hljs-attr\">opts<\/span>=<span class=\"hljs-string\">{{<\/span>\n                <span class=\"hljs-attr\">playerVars:<\/span> {\n                  <span class=\"hljs-attr\">controls:<\/span> <span class=\"hljs-attr\">0<\/span>,\n                },\n              }}\n            \/&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">onClick<\/span>=<span class=\"hljs-string\">{onPlayHandler}<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"btn\"<\/span>&gt;<\/span>\n            Play\n          <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">onClick<\/span>=<span class=\"hljs-string\">{onPauseHandler}<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"btn\"<\/span>&gt;<\/span>\n            Pause\n          <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n      );\n    }\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>We created a new state variable called <code>player<\/code> to hold the player state in the <code>onReady<\/code> function when rendering the YouTube player component. We then use this player state to call the <code>playVideo<\/code> and <code>pauseVideo<\/code> methods in their respective play and pause handlers.\nThe controls key in the <code>playerVars<\/code> object takes 0 and 1 as values. When set to 0, the player controls do not display in the player and vice versa.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/cloudinary-marketing-res.cloudinary.com\/image\/upload\/c_limit,w_2000\/f_auto\/q_auto\/media_jams\/s_90A3F15235320E5EE15BEFBCFE29C22CB59EFE7F6515BF3E50471B1B5BA60AA3_1646254483052_CleanShot+2022-03-03+at+00.54.19.gif\" alt=\"\" loading=\"lazy\" class=\"c-transformed-asset\"  width=\"800\" height=\"424\"\/><\/p>\n<h3>Autoplay Video on Page Load<\/h3>\n<p>The API has an <code>autoplay<\/code> parameter which indicates whether the video should start playing automatically when the player loads or not. It takes the values 0 and 1 as well. When set to 1, it enables autoplay, allowing instant video playing without any human interaction with the player. While 0, which is the default value, disables autoplay.\nTo enable autoplay, set the value of the <code>autoplay<\/code> key in the <code>playerVars<\/code> object to 1.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\">    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">YouTube<\/span>\n      <span class=\"hljs-attr\">videoId<\/span>=<span class=\"hljs-string\">{youtubeID}<\/span>\n      <span class=\"hljs-attr\">onReady<\/span>=<span class=\"hljs-string\">{onReady}<\/span>\n      <span class=\"hljs-attr\">opts<\/span>=<span class=\"hljs-string\">{{<\/span>\n        <span class=\"hljs-attr\">playerVars:<\/span> {\n          <span class=\"hljs-attr\">autoplay:<\/span> <span class=\"hljs-attr\">1<\/span>,\n        },\n      }}\n    \/&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<h3>Create a Video Playlist<\/h3>\n<p>The YouTube IFrame API also provides a <code>playlist<\/code> parameter that allows you to create a playlist of videos. To create a playlist, add a <code>playlist<\/code> object key to the <code>playerVars<\/code> object with a string value containing comma-separated IDs of the videos to form the playlist.<\/p>\n<p>To demonstrate this in our application, update the <code>opts<\/code> props in the rendered YouTube component to look like this:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\">    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">YouTube<\/span>\n      <span class=\"hljs-attr\">videoId<\/span>=<span class=\"hljs-string\">{youtubeID}<\/span>\n      <span class=\"hljs-attr\">onReady<\/span>=<span class=\"hljs-string\">{onReady}<\/span>\n      <span class=\"hljs-attr\">opts<\/span>=<span class=\"hljs-string\">{{<\/span>\n        <span class=\"hljs-attr\">playerVars:<\/span> {\n          <span class=\"hljs-attr\">playlist:<\/span> '<span class=\"hljs-attr\">00AkMN9IAAY<\/span>,<span class=\"hljs-attr\">4oCVDkb_EIs<\/span>,<span class=\"hljs-attr\">7fPXI_MnBOY<\/span>',\n        },\n      }}\n    \/&gt;<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>In the code, we used hardcoded video ids to define the playlist. Save the file to view the changes in the browser.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/cloudinary-marketing-res.cloudinary.com\/image\/upload\/c_limit,w_2000\/f_auto\/q_auto\/media_jams\/s_90A3F15235320E5EE15BEFBCFE29C22CB59EFE7F6515BF3E50471B1B5BA60AA3_1646254951000_CleanShot+2022-03-03+at+01.02.202x.png\" alt=\"\" loading=\"lazy\" class=\"c-transformed-asset\"  width=\"2000\" height=\"869\"\/><\/p>\n<p><strong>Manually Set the Start and Stop Time.<\/strong>\nThe API accepts <code>start<\/code> and <code>stop<\/code> parameters that help determine when the player should start or stop playing the provided video in seconds relative to the start of the video.<\/p>\n<pre class=\"js-syntax-highlighted\"><span><code class=\"hljs shcb-wrap-lines\">    &lt;YouTube\n        videoId={youtubeID}\n        onReady={onReady}\n        opts={\n          { playerVars: {\n            start: &lt;&lt;start-time&gt;&gt;,\n            stop: &lt;&lt;stop-time&gt;&gt;\n          } }\n        }\n    \/&gt;\n<\/code><\/span><\/pre>\n<p>There are so many customization settings in the YouTube IFrame API that we can\u2019t possibly cover them all in this article. However, you can <a href=\"https:\/\/developers.google.com\/youtube\/player_parameters\">click here<\/a> to learn more about other parameters to personalize your YouTube player.<\/p>\n<p>You can find the complete project <a href=\"https:\/\/github.com\/ifeoma-imoh\/custom-youtube-player\">here<\/a> on Github.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this post, we saw how to embed a YouTube player into a React app using the react-youtube package. In addition, we looked at a few of the options for personalization.<\/p>\n<p><strong>Resources you may find helpful:<\/strong><\/p>\n<ul>\n<li>\n<a href=\"https:\/\/www.npmjs.com\/package\/react-youtube\">The react-youtube Package<\/a>\n<\/li>\n<li>\n<a href=\"https:\/\/developers.google.com\/youtube\/iframe_api_reference\">IFrame Player API<\/a>\n<\/li>\n<li>\n<a href=\"https:\/\/developers.google.com\/youtube\/player_parameters\">YouTube Embedded Players and Player Parameters<\/a>\n<\/li>\n<\/ul>\n<\/div>","protected":false},"excerpt":{"rendered":"","protected":false},"author":41,"featured_media":28010,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_cloudinary_featured_overwrite":false,"footnotes":""},"categories":[1],"tags":[134,382,246,371],"class_list":["post-28009","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-guest-post","tag-player-video","tag-react","tag-under-review"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.6 (Yoast SEO v26.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Embed a YouTube Player into a React App<\/title>\n<meta name=\"description\" content=\"In this post, we will go through the process of embedding a YouTube player in a React application using the react-youtube package. We will also look at some of the options for personalization.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Embed a YouTube Player into a React App\" \/>\n<meta property=\"og:description\" content=\"In this post, we will go through the process of embedding a YouTube player in a React application using the react-youtube package. We will also look at some of the options for personalization.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloudinary Blog\" \/>\n<meta property=\"article:published_time\" content=\"2022-03-24T20:13:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-09T12:25:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/v1681925690\/Web_Assets\/blog\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c-png?_i=AA\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"NewsArticle\",\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/\"},\"author\":{\"name\":\"\",\"@id\":\"\"},\"headline\":\"Embed a YouTube Player into a React App\",\"datePublished\":\"2022-03-24T20:13:27+00:00\",\"dateModified\":\"2025-02-09T12:25:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/\"},\"wordCount\":8,\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925690\/Web_Assets\/blog\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c.png?_i=AA\",\"keywords\":[\"Guest Post\",\"Player Video\",\"React\",\"Under Review\"],\"inLanguage\":\"en-US\",\"copyrightYear\":\"2022\",\"copyrightHolder\":{\"@id\":\"https:\/\/cloudinary.com\/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/\",\"url\":\"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/\",\"name\":\"Embed a YouTube Player into a React App\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925690\/Web_Assets\/blog\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c.png?_i=AA\",\"datePublished\":\"2022-03-24T20:13:27+00:00\",\"dateModified\":\"2025-02-09T12:25:15+00:00\",\"description\":\"In this post, we will go through the process of embedding a YouTube player in a React application using the react-youtube package. We will also look at some of the options for personalization.\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/#primaryimage\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925690\/Web_Assets\/blog\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c.png?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925690\/Web_Assets\/blog\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c.png?_i=AA\",\"width\":1280,\"height\":720},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudinary.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Embed a YouTube Player into a React App\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"name\":\"Cloudinary Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cloudinary.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\",\"name\":\"Cloudinary Blog\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"width\":312,\"height\":60,\"caption\":\"Cloudinary Blog\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Embed a YouTube Player into a React App","description":"In this post, we will go through the process of embedding a YouTube player in a React application using the react-youtube package. We will also look at some of the options for personalization.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/","og_locale":"en_US","og_type":"article","og_title":"Embed a YouTube Player into a React App","og_description":"In this post, we will go through the process of embedding a YouTube player in a React application using the react-youtube package. We will also look at some of the options for personalization.","og_url":"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/","og_site_name":"Cloudinary Blog","article_published_time":"2022-03-24T20:13:27+00:00","article_modified_time":"2025-02-09T12:25:15+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/v1681925690\/Web_Assets\/blog\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c-png?_i=AA","type":"image\/png"}],"twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/#article","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/"},"author":{"name":"","@id":""},"headline":"Embed a YouTube Player into a React App","datePublished":"2022-03-24T20:13:27+00:00","dateModified":"2025-02-09T12:25:15+00:00","mainEntityOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/"},"wordCount":8,"publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925690\/Web_Assets\/blog\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c.png?_i=AA","keywords":["Guest Post","Player Video","React","Under Review"],"inLanguage":"en-US","copyrightYear":"2022","copyrightHolder":{"@id":"https:\/\/cloudinary.com\/#organization"}},{"@type":"WebPage","@id":"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/","url":"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/","name":"Embed a YouTube Player into a React App","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/#primaryimage"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925690\/Web_Assets\/blog\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c.png?_i=AA","datePublished":"2022-03-24T20:13:27+00:00","dateModified":"2025-02-09T12:25:15+00:00","description":"In this post, we will go through the process of embedding a YouTube player in a React application using the react-youtube package. We will also look at some of the options for personalization.","breadcrumb":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/#primaryimage","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925690\/Web_Assets\/blog\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c.png?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925690\/Web_Assets\/blog\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c.png?_i=AA","width":1280,"height":720},{"@type":"BreadcrumbList","@id":"https:\/\/cloudinary.com\/blog\/guest_post\/embed-a-youtube-player-into-a-react-app\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudinary.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Embed a YouTube Player into a React App"}]},{"@type":"WebSite","@id":"https:\/\/cloudinary.com\/blog\/#website","url":"https:\/\/cloudinary.com\/blog\/","name":"Cloudinary Blog","description":"","publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cloudinary.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/cloudinary.com\/blog\/#organization","name":"Cloudinary Blog","url":"https:\/\/cloudinary.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","width":312,"height":60,"caption":"Cloudinary Blog"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":""}]}},"jetpack_featured_media_url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925690\/Web_Assets\/blog\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c\/6678c54ddf3d70c631f1c343f53deb20452702e8-1280x720-1_28010b8e8c.png?_i=AA","_links":{"self":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/28009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/comments?post=28009"}],"version-history":[{"count":1,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/28009\/revisions"}],"predecessor-version":[{"id":36765,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/28009\/revisions\/36765"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media\/28010"}],"wp:attachment":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media?parent=28009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/categories?post=28009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/tags?post=28009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}