npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@njmyers/react-you-tube

v2.0.5

Published

React component for downloading and using the YouTube iFrame API

Downloads

11

Readme

React YouTube

This is the new hooks based version of the old react-youtube-component! I have also changed the package name so that it is namespaced under my organization @njmyers. See the updated installation instructions below.

A thin wrapper around the YouTube IFrame API. The goal is to make it easier to use the API in a react centered way. The component uses hooks to downloads the API and create a player instance. The player API is unchanged so you can use the IFrame API documentation directly on your YouTube element to load videos, create playlists and show your content.

Installation

npm install @njmyers/react-you-tube;

or

yarn add @njmyers/react-you-tube;

YouTube Basic

This is the most basic and easy way to use this component. Simply use the component and pass in all of required props as dictated by the YouTube IFrame API documentation

import React from 'react';
import YouTube from '../src/YouTube';

function YouTubeBasic() {
  return (
    <YouTube
      width="640"
      height="390"
      videoId="Z1BCujX3pw8"
      playerVars={{ autoplay: 0, controls: 1 }}
    />
  );
}

export default YouTubeBasic;

YouTube Controlled

You can also get a reference to the player if you want to map additional controls. Just use the onPlayer callback.

import React, { useState } from 'react';
import YouTube from '../src/YouTube';

function YouTubeControlled() {
  const [player, setPlayer] = useState(null);
  const [videoId, setVideoId] = useState('');

  return (
    <React.Fragment>
      <YouTube
        width="640"
        height="390"
        videoId="Z1BCujX3pw8"
        playerVars={{ autoplay: 0, controls: 1 }}
        onPlayer={player => setPlayer(player)}
      />
      <div>
        <label className="sans">Enter Video ID</label>
        <input
          name="videoId"
          value={videoId}
          onChange={e => setVideoId(e.target.value)}
        />
        <button onClick={() => player.loadVideoById(videoId)}>
          <span className="button_text-sans">Change Video</span>
        </button>
      </div>
    </React.Fragment>
  );
}

export default YouTubeControlled;

YouTube Hook

For functional components, you can easily build out your own control use the useYouTube hook. This makes it simple to control the player reference and build out additional user interfaces. The hook is the most flexible API and is probably easier then the controlled example if you need flexibility.

import React, { useState } from 'react';
import useYouTube from '../src/use-you-tube';

function YouTubeHook() {
  const [videoId, setVideoId] = useState('');
  const [node, setNode] = React.useState(null);
  const handleRef = React.useCallback(node => {
    if (node !== null) {
      setNode(node);
    }
  }, []);

  const player = useYouTube(node, {
    width: 640,
    height: 390,
    videoId: 'Z1BCujX3pw8',
    playerVars: {
      autoplay: 0,
      controls: 1,
    },
  });

  return (
    <React.Fragment>
      <div ref={handleRef} />
      <div>
        <label className="sans">Enter Video ID</label>
        <input
          name="videoId"
          value={videoId}
          onChange={e => setVideoId(e.target.value)}
        />
        <button onClick={() => player.loadVideoById(videoId)}>
          <span className="button_text-sans">Change Video</span>
        </button>
      </div>
    </React.Fragment>
  );
}

export default YouTubeHook;

YouTube Multiple

You can easily render multiple YouTube videos on a page. Simply use the YouTube component in a loop. The component will handle loading the YouTube IFrame API and will ensure that all of your videos load asynchronously

import React from 'react';
import YouTube from '../src/YouTube';

const videoIds = [
  'Z1BCujX3pw8',
  'ue80QwXMRHg',
  'QwievZ1Tx-8',
  'pWdKf3MneyI',
  'UUkn-enk2RU',
];

function YouTubeMultiple() {
  return (
    <div>
      {videoIds.map(videoId => {
        return (
          <YouTube
            key={videoId}
            width="640"
            height="390"
            videoId={videoId}
            playerVars={{ autoplay: 0, controls: 1 }}
          />
        );
      })}
    </div>
  );
}

export default YouTubeMultiple;