React'isSelected'不适用于检查是否被选中

时间:2019-12-07 23:10:33

标签: javascript reactjs

我正在React应用程序上苏醒,用户可以选择从Youtube获取的随机视频中的视频。我希望它可以识别是否选择了那些。但是isSelected无法正常工作。

const isSelected = selectedVideos.find(v => v.uid === video.id.videoId);

实际上,它没有定义。我不应该为此使用filter吗?

我的代码:

const Youtube = ({ video, handleSelect, 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>
          <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>
      ) : (
        <p>Selected</p>
      )}
    </div>
  );
};

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

export default YoutubeList;
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={"Create"} />
          <SearchForm onSerchYoutube={this.onSerchYoutube} />
          <YoutubeList
            videos={this.state.videos}
            handleSelect={this._handleSelect}
            selectedVideos={this.state.selectedVideos}
          />
        </div>
      </div>
    );
  }
}

export default Home;

其他

样本-selectedVideo

0: {uid: "kY6bGqtYD3k"}
1: {uid: "kY6bGqtYD3k"}
length: 2
__proto__: Array(0)

样本-视频

etag: ""j6xRRd8dTPVVptg711_CSPADRfg/0lHCSlZJYu6Fcoet132S1tSh4G0""
id:
kind: "youtube#video"
videoId: "hBv1SHjqd1Q"
__proto__: Object
kind: "youtube#searchResult"
snippet:
channelId: "UCfCKvREB11-fxyotS1ONgww"
channelTitle: "Mark Felton Productions"
description: "Did the Germans really create a stay-behind guerrilla force known as 'Werewolf' to continue the fight behind Allied lines in 1945? Yes, they did, but here you will ..."
liveBroadcastContent: "none"
publishedAt: "2019-03-27T16:55:23.000Z"
thumbnails: {default: {…}, medium: {…}, high: {…}}
title: "SS Werewolves - The True Story"

1 个答案:

答案 0 :(得分:0)

使用some代替find

find将返回匹配对象的整个值,并且您只需要布尔信息(如果有或没有)。为此,您可以使用some,如果有或没有,则仅返回布尔信息。

出于好奇,someincludes类似,但是some接受谓词功能,而includes接受文字值。