反应onClick事件在组件中不起作用

时间:2019-12-05 09:56:35

标签: reactjs

非常感谢您的提前帮助。我的应用程序,用户可以选择一些视频,然后将其保存在React + Youtube API中。我已经成功地从YouTube上获取了视频。但是要完全完成它,我有两件事。

1)onClick()在CardAction容器中不起作用
2)isSelected在Youtube.js中不起作用

src / components / common / Youtube.js

const Youtube = ({ video, handleSelect, classes, selectedVideos }) => {
  const url = "https://www.youtube.com/embed/" + video.id.videoId;
  const videoInfo = video.snippet;
  const isSelected = selectedVideos.find(v => v.uid === video.id.videoId);

  return (
    <div>
      {isSelected ? (
        <Card className={classes.card}>
          <iframe
            id="ytplayer"
            type="ytplayer"
            width="100%"
            height="400"
            src={url}
            frameborder="0"
          />
          <CardActionArea
            onClick={handleSelect(video.id.videoId)}
          >
            <h2>{videoInfo.title}</h2>
            <p>{videoInfo.description}</p>
          </CardActionArea>
        </Card>
      ) : (
        <Card className={classes.card}>
          <iframe
            id="ytplayer"
            type="ytplayer"
            width="100%"
            height="270"
            src={url}
            frameborder="0"
          />
          <CardActionArea
            onClick={handleSelect(video.id.videoId)}
          >
            <h2>{videoInfo.title}</h2>
            <p>{videoInfo.description}</p>
          </CardActionArea>
        </Card>
      )}
    </div>
  );
};

export default Youtube;

src / components / common / YoutubeList.js

const YoutubeList = ({ videos, handleSelect, selectedVideos }) =>
  videos.map(video => (
    <Youtube
      video={video}
      handleSelect={handleSelect}
      selectedVideos={selectedVideos}
    />
  ));

export default YoutubeList;

src / components / Home.js

const YOUTUBE_API_KEY = process.env.REACT_APP_YOUTUBE_API_KEY;

class Home extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      videos: [],
      selectedVideos: []
    };
  }

  onSerchYoutube = keyword => {
    const url = `https://www.googleapis.com/youtube/v3/search?type=video&part=snippet&q=${keyword}&maxResults=3&key=${YOUTUBE_API_KEY}`;

    axios
      .get(url)
      .then(response => {
        this.setState({
          videos: response.data.items
        });
      })
      .catch(() => {
        console.log("Failed to fetch videos :(");
      });
  };

  _handleSelect = id => {
    const { selectedVideos } = this.state;
    selectedVideos.push({ uid: id });
    console.log("selected!" + id);
    console.log("selected", selectedVideos);
  };

  render() {
    return (
      <div>
        <div>
          <Header title={"Home"} />
          <SearchForm onSerchYoutube={this.onSerchYoutube} />
          <YoutubeList
            videos={this.state.videos}
            handleSelect={this._handleSelect}
            selectedVideos={this.state.selectedVideos}
          />
        </div>
      </div>
    );
  }
}

export default Home;

1 个答案:

答案 0 :(得分:2)

可以使用ES6吗?

更改
onClick={handleSelect(video.id.videoId)}


onClick={() => handleSelect(video.id.videoId)}