【问题标题】:Rendering different youtube videos on button click在按钮单击时呈现不同的 youtube 视频
【发布时间】:2021-12-24 11:40:20
【问题描述】:

我创建了一个 react 应用程序,我在其中显示不同的视频游戏,该应用程序决定玩哪个游戏。我还有一个存储视频游戏数据的文件。我试图实现的目标是在使用 React Hooks 时单击按钮时呈现相应视频游戏的 youtube 视频预告片。我一直在使用 npm 包 react-player。如果有人可以提供帮助,我将不胜感激。

这是视频游戏组件的代码:

import React from 'react';
import { Button, message } from 'antd';
import { Row, Col } from 'antd';
import GameEntry from '../GameEntry';
import Games from '../Games';

function VideoGameSection() {
    const chooseGame = () => {
        var randomGameTitle = [
            'Gears 5',
            'Halo',
            'Hellblade',
            'It takes two',
            'A Plague Tale',
            'Psychonauts',
            'Twelve Minutes',
            'Ori',
            'Streets of Rage',
            'Last of Us',
            'Boodborne',
            'Geenshin Impact',
            'Dragon Ball Z:KAKAROT',
            'Ghost Tsushima',
            'Naruto',
            'Overcooked',
            'Horizon',
            'Tomb Raider',
            'Uncharted',
            'Person 5 Royal',
            'Ratchet',
            'Spider-Man',
        ];

        var randomIndex = Math.floor(Math.random() * randomGameTitle.length);

        return message.info(
            'The game you will play is: ' + randomGameTitle[randomIndex] + '.',
        );
    };

    return (
        <div id="video-game" className="block bgGray">
            <div className="container-fluid">
                <div className="titleHolder">
                    <h2>Video Games</h2>
                    <p>A list of current games</p>
                    <div className="site-button-ghost-wrapper">
                        <Button
                            className="gameButton"
                            type="primary"
                            danger
                            ghost
                            onClick={chooseGame}
                        >
                            Pick for me
                        </Button>
                    </div>
                </div>
                <Row gutter={[16, 24]}>
                    {Games.map((videogame, i) => (
                        <Col span={8}>
                            <GameEntry
                                id={i}
                                key={i}
                                title={videogame.title}
                                imgURL={videogame.imgURL}
                                description={videogame.console}
                            />
                        </Col>
                    ))}
                </Row>
            </div>
        </div>
    );
}

export default VideoGameSection;

这是我的游戏入口组件的代码:

import React, { useState } from 'react';
import { Card, Button, Modal } from 'antd';
import YoutubeSection from './Home/YoutubeSection';

const { Meta } = Card;

function GameEntry(props) {
    const [isModalVisible, setIsModalVisible] = useState(false);

    const showModal = () => {
        setIsModalVisible(true);
    };

    const handleClose = () => {
        setIsModalVisible(false);
    };

    const handleCancel = () => {
        setIsModalVisible(false);
    };

    return (
        <div>
            <Card
                className="gameCard"
                hoverable
                cover={<img className="cardImg" alt={props.title} src={props.imgURL} />}
            >
                <div className="cardTitle">
                    <Meta title={props.title} description={props.description} />
                </div>
                <>
                    <Button
                        className="trailerButton"
                        type="primary"
                        block
                        style={{
                            color: '#fff',
                            borderColor: '#fff',
                            backgroundColor: '#e6544f',
                        }}
                        onClick={showModal}
                    >
                        Click for trailer
                    </Button>
                    <Modal
                        title={props.title}
                        width={'725px'}
                        visible={isModalVisible}
                        onOk={handleClose}
                        onCancel={handleCancel}
                    >
                        <YoutubeSection />
                    </Modal>
                </>
            </Card>
        </div>
    );
}

export default GameEntry;

这是 youtube 组件的代码:

import React, { useState } from 'react';
import ReactPlayer from 'react-player';

function YoutubeSection(props) {
    return (
        <div className="container-fluid">
            <ReactPlayer
                // url={videoTrailer}
                muted={false}
                playing={true}
                controls={true}
            />
        </div>
    );
}

export default YoutubeSection;

数据文件示例:

const Games = [
    {
        id: 1,
        title: 'Gears 5',
        imgURL: '../Images/gears-5.jpeg',
        console: 'Xbox',
        videoID: 'SEpWlFfpEkU&t=7s',
    },

【问题讨论】:

    标签: reactjs react-hooks react-player


    【解决方案1】:

    您可以保留一个 Modal 组件并将其用于此目的。

    ModalView.js

    import React, { useState } from "react";
    import YoutubeSection from "./YoutubeSection";
    import { Modal } from "antd";
    
    const ModalView = ({
      title,
      isModalVisible,
      handleClose,
      handleCancel,
      videoID
    }) => {
      return (
        <Modal
          title={title}
          width={"725px"}
          visible={isModalVisible}
          onOk={handleClose}
          onCancel={handleCancel}
        >
          <YoutubeSection videoID={videoID} />
        </Modal>
      );
    };
    
    export default ModalView;
    

    ModalView 及其state 控制功能移至VideoGameSection

    VideoGameSection.js

    import React, { useState } from "react";
    import { Button, message } from "antd";
    import { Row, Col } from "antd";
    import GameEntry from "./GameEntry";
    import Games from "./Games";
    import ModalView from "./ModalView";
    
    function VideoGameSection() {
      const [isModalVisible, setIsModalVisible] = useState(false);
      const [currentVideoID, setCurrentVideoID] = useState("");
    
      const showModal = () => {
        setIsModalVisible(true);
      };
    
      const handleClose = () => {
        setIsModalVisible(false);
      };
    
      const handleCancel = () => {
        setIsModalVisible(false);
      };
    
      const chooseGame = () => {
        var randomGameTitle = [
          "Gears 5",
          "Halo",
          "Hellblade",
          "It takes two",
          "A Plague Tale",
          "Psychonauts",
          "Twelve Minutes",
          "Ori",
          "Streets of Rage",
          "Last of Us",
          "Boodborne",
          "Geenshin Impact",
          "Dragon Ball Z:KAKAROT",
          "Ghost Tsushima",
          "Naruto",
          "Overcooked",
          "Horizon",
          "Tomb Raider",
          "Uncharted",
          "Person 5 Royal",
          "Ratchet",
          "Spider-Man"
        ];
    
        var randomIndex = Math.floor(Math.random() * randomGameTitle.length);
    
        return message.info(
          "The game you will play is: " + randomGameTitle[randomIndex] + "."
        );
      };
    
      return (
        <div id="video-game" className="block bgGray">
          <div className="container-fluid">
            <div className="titleHolder">
              <h2>Video Games</h2>
              <p>A list of current games</p>
              <div className="site-button-ghost-wrapper">
                <Button
                  className="gameButton"
                  type="primary"
                  danger
                  ghost
                  onClick={chooseGame}
                >
                  Pick for me
                </Button>
              </div>
            </div>
            <Row gutter={[16, 24]}>
              {Games.map((videogame, i) => (
                <Col span={8}>
                  <GameEntry
                    id={i}
                    key={i}
                    title={videogame.title}
                    imgURL={videogame.imgURL}
                    description={videogame.console}
                    videoID={videogame.videoID}
                    setCurrentVideoID={setCurrentVideoID}
                    showModal={showModal}
                  />
                </Col>
              ))}
              <ModalView
                videoID={currentVideoID}
                handleClose={handleClose}
                isModalVisible={isModalVisible}
              />
            </Row>
          </div>
        </div>
      );
    }
    
    export default VideoGameSection;
    
    

    访问通过ModalView 传递的videoID。您可以保存gameId 而不是videoID 以获取游戏的任何其他信息例如:titleYoutubeSection.js

    import React, { useState } from "react";
    import ReactPlayer from "react-player";
    
    function YoutubeSection(props) {
      return (
        <div className="container-fluid">
          <ReactPlayer
            url={`https://www.youtube.com/watch?v=${props.videoID}`}
            muted={false}
            playing={true}
            controls={true}
          />
        </div>
      );
    }
    
    export default YoutubeSection;
    

    GameEntry.js

    import React, { useState } from "react";
    import { Card, Button, Modal } from "antd";
    import YoutubeSection from "./YoutubeSection";
    
    const { Meta } = Card;
    
    function GameEntry(props) {
      return (
        <div>
          <Card
            className="gameCard"
            hoverable
            cover={<img className="cardImg" alt={props.title} src={props.imgURL} />}
          >
            <div className="cardTitle">
              <Meta title={props.title} description={props.description} />
            </div>
            <>
              <Button
                className="trailerButton"
                type="primary"
                block
                style={{
                  color: "#fff",
                  borderColor: "#fff",
                  backgroundColor: "#e6544f"
                }}
                onClick={() => {
                  props.setCurrentVideoID(props.videoID);
                  props.showModal();
                }}
              >
                Click for trailer
              </Button>
            </>
          </Card>
        </div>
      );
    }
    
    export default GameEntry;
    
    

    代码沙箱 => https://codesandbox.io/s/flamboyant-dan-z0kc0?file=/src/ModalView.js

    【讨论】:

      猜你喜欢
      • 2012-02-10
      • 1970-01-01
      • 2017-05-12
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 2019-11-13
      • 2012-12-17
      相关资源
      最近更新 更多