如何使每个按钮下拉显示单击的视频? ReactJS

发布于 2025-01-10 09:29:12 字数 3089 浏览 0 评论 0原文

我正在将 props 传递到 Buttons.jsx 文件。每个按钮都会下拉一个视频。我的问题是,当我单击一个按钮时,所有视频都会出现。我希望只有被点击的特定视频才会显示。

这是仓库的链接

https://noahfarinas.github.io/stretching/

提前致谢!

Buttons.jsx

import { useState } from "react";
import ReactPlayer from "react-player";

export const Buttons = (props) => {
  const { content } = props;
    const [showVideo,setShowVideo] = useState(false);
    
    const handleOnClick = () =>  setShowVideo(true); 

   

  return (
    <div className="buttonContainer">
      {content.map((item) => (
        <div className="buttonSpace">
        
            <button id="btn" onClick={handleOnClick}>{item.title}</button>
            {showVideo ? <ReactPlayer url={item.embed} /> : null}    
         
        </div>
      ))}
    </div>
  );
};

export default Buttons;



**App.js**


import "./App.css";
import Search from "./Search";
import TitleView from "./TitleView";
import Buttons from "./Buttons";

function App() {
  const TITLE = "Stretches";

  const DATA = [
    {
      area: "upper-back",
      video: "https://www.youtube.com/watch?v=bTn89EBKJdM",
    },

    {
      area: "mid-back",
      video: "https://www.youtube.com/watch?v=VnDuWC40egg",
    },

    {
      area: "lower-back",
      video: "https://www.youtube.com/watch?v=N-xqKx8oshs",
    },

    {
      area: "hips",
      video: "https://www.youtube.com/watch?v=nLuvQCTPrcY",
    },

    {
      area: "calves",
      video: "https://www.youtube.com/watch?v=37GHTaoknfw",
    },

    {
      area: "chest",
      video: "https://www.youtube.com/watch?v=NePr1XKRTLU",
    },

    {
      area: "glute",
      video: "https://www.youtube.com/watch?v=eRCpceBhcm0",
    },

    {
      area: "foot",
      video: "https://www.youtube.com/watch?v=AXSj_5pBAKw",
    },

    {
      area: "forearm",
      video: "https://www.youtube.com/watch?v=Ayhu7TzNGSQ",
    },

    {
      area: "it band",
      video: "https://www.youtube.com/watch?v=i6Psvd81Hyc",
    },

    {
      area: "hamstring",
      video: "https://www.youtube.com/watch?v=pJUwEBgxWoE",
    },

    {
      area: "tricep",
      video: "https://www.youtube.com/watch?v=SaZK9vlSmHI",
    },

    {
      area: "lat",
      video: "https://www.youtube.com/watch?v=6V5tSn9oEJg",
    },
  ];

  const BUTTONDATA = [
    {
      title: "back",
      embed: "https://www.youtube.com/watch?v=buF1v8aiTvM",
    },

    {
      title: "legs",
      embed: "https://www.youtube.com/watch?v=UIRTPXj1Q1U",
    },

    {
      title: "upper-body",
      embed: "https://www.youtube.com/watch?v=Kpd9ik93Sxk",
    },

    {
      title: "hips",
      embed: "https://www.youtube.com/watch?v=j42sLnoMkrA",
    },
  ];

  return (
    <div className="App">
      <TitleView headline={TITLE} />
      <Search placeholder="What hurts..." data={DATA} />
      <Buttons content={BUTTONDATA} />
    </div>
  );
}

export default App;

I am passing through props to my Buttons.jsx file. Each button would drop down a video. My problem is that when I click one button, all of the videos appear. I want it so that only that certain video that was clicked would show.

here is the link to repo

https://noahfarinas.github.io/stretching/

Thanks in advance!

Buttons.jsx

import { useState } from "react";
import ReactPlayer from "react-player";

export const Buttons = (props) => {
  const { content } = props;
    const [showVideo,setShowVideo] = useState(false);
    
    const handleOnClick = () =>  setShowVideo(true); 

   

  return (
    <div className="buttonContainer">
      {content.map((item) => (
        <div className="buttonSpace">
        
            <button id="btn" onClick={handleOnClick}>{item.title}</button>
            {showVideo ? <ReactPlayer url={item.embed} /> : null}    
         
        </div>
      ))}
    </div>
  );
};

export default Buttons;



**App.js**


import "./App.css";
import Search from "./Search";
import TitleView from "./TitleView";
import Buttons from "./Buttons";

function App() {
  const TITLE = "Stretches";

  const DATA = [
    {
      area: "upper-back",
      video: "https://www.youtube.com/watch?v=bTn89EBKJdM",
    },

    {
      area: "mid-back",
      video: "https://www.youtube.com/watch?v=VnDuWC40egg",
    },

    {
      area: "lower-back",
      video: "https://www.youtube.com/watch?v=N-xqKx8oshs",
    },

    {
      area: "hips",
      video: "https://www.youtube.com/watch?v=nLuvQCTPrcY",
    },

    {
      area: "calves",
      video: "https://www.youtube.com/watch?v=37GHTaoknfw",
    },

    {
      area: "chest",
      video: "https://www.youtube.com/watch?v=NePr1XKRTLU",
    },

    {
      area: "glute",
      video: "https://www.youtube.com/watch?v=eRCpceBhcm0",
    },

    {
      area: "foot",
      video: "https://www.youtube.com/watch?v=AXSj_5pBAKw",
    },

    {
      area: "forearm",
      video: "https://www.youtube.com/watch?v=Ayhu7TzNGSQ",
    },

    {
      area: "it band",
      video: "https://www.youtube.com/watch?v=i6Psvd81Hyc",
    },

    {
      area: "hamstring",
      video: "https://www.youtube.com/watch?v=pJUwEBgxWoE",
    },

    {
      area: "tricep",
      video: "https://www.youtube.com/watch?v=SaZK9vlSmHI",
    },

    {
      area: "lat",
      video: "https://www.youtube.com/watch?v=6V5tSn9oEJg",
    },
  ];

  const BUTTONDATA = [
    {
      title: "back",
      embed: "https://www.youtube.com/watch?v=buF1v8aiTvM",
    },

    {
      title: "legs",
      embed: "https://www.youtube.com/watch?v=UIRTPXj1Q1U",
    },

    {
      title: "upper-body",
      embed: "https://www.youtube.com/watch?v=Kpd9ik93Sxk",
    },

    {
      title: "hips",
      embed: "https://www.youtube.com/watch?v=j42sLnoMkrA",
    },
  ];

  return (
    <div className="App">
      <TitleView headline={TITLE} />
      <Search placeholder="What hurts..." data={DATA} />
      <Buttons content={BUTTONDATA} />
    </div>
  );
}

export default App;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一向肩并 2025-01-17 09:29:12

这个问题可以通过两种方式解决。一种方法是在 app.js 中的 BUTTONDATA 上有一个 map,并为数组的每个项目呈现单独的 组件。这样,每个 Button 组件将拥有自己的状态,并且在单击按钮时仅显示自己的内容。
另一种方法是为 BUTTONDATA 中的每个数组项都有一个布尔属性。然后您可以通过以下方式修改您的 组件:

Button.js

import { useEffect, useState } from "react";
import ReactPlayer from "react-player";

const Buttons = (props) => {
  const { content } = props;
  const [visibilityChanged, setVisibiltyChanged] = useState(false);

  useEffect(() => {}, [visibilityChanged]);

  const handleOnClick = (index) => () => {
    content[index].isVisible = !content[index].isVisible;
    setVisibiltyChanged(!visibilityChanged);
  };

  return (
    <div className="buttonContainer">
      {content.map((item, index) => (
        <div className="buttonSpace" key={index}>
          <button id="btn" onClick={handleOnClick(index)}>
            {item.title}
          </button>
          {item.isVisible ? <ReactPlayer url={item.embed} /> : null}
        </div>
      ))}
    </div>
  );
};

export default Buttons;

和 app.js 中将有以下更改:
App.js

const BUTTONDATA = [
    {
      title: "back",
      embed: "https://www.youtube.com/watch?v=buF1v8aiTvM",
      isVisible: false
    },

    {
      title: "legs",
      embed: "https://www.youtube.com/watch?v=UIRTPXj1Q1U",
      isVisible: false
    },

    {
      title: "upper-body",
      embed: "https://www.youtube.com/watch?v=Kpd9ik93Sxk",
      isVisible: false
    },

    {
      title: "hips",
      embed: "https://www.youtube.com/watch?v=j42sLnoMkrA",
      isVisible: false
    }
  ];

这里是我创建的代码沙箱,用于在单击按钮时切换视频。

The problem can be solved in two ways. One way is to have a map in app.js on the BUTTONDATA and render separate component for each item of the array. In this way each Button Component will have its own state and will show only its own contents upon button click.
The other way is to have a boolean property for each of array item in the BUTTONDATA. and then you can modify your component in the following way:

Button.js

import { useEffect, useState } from "react";
import ReactPlayer from "react-player";

const Buttons = (props) => {
  const { content } = props;
  const [visibilityChanged, setVisibiltyChanged] = useState(false);

  useEffect(() => {}, [visibilityChanged]);

  const handleOnClick = (index) => () => {
    content[index].isVisible = !content[index].isVisible;
    setVisibiltyChanged(!visibilityChanged);
  };

  return (
    <div className="buttonContainer">
      {content.map((item, index) => (
        <div className="buttonSpace" key={index}>
          <button id="btn" onClick={handleOnClick(index)}>
            {item.title}
          </button>
          {item.isVisible ? <ReactPlayer url={item.embed} /> : null}
        </div>
      ))}
    </div>
  );
};

export default Buttons;

and in app.js will have the following changes:
App.js

const BUTTONDATA = [
    {
      title: "back",
      embed: "https://www.youtube.com/watch?v=buF1v8aiTvM",
      isVisible: false
    },

    {
      title: "legs",
      embed: "https://www.youtube.com/watch?v=UIRTPXj1Q1U",
      isVisible: false
    },

    {
      title: "upper-body",
      embed: "https://www.youtube.com/watch?v=Kpd9ik93Sxk",
      isVisible: false
    },

    {
      title: "hips",
      embed: "https://www.youtube.com/watch?v=j42sLnoMkrA",
      isVisible: false
    }
  ];

Here is the code sandbox I created to toggle videos on button click.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文