k10a
k10a

Reputation: 1087

React onClick Event Not Working in Component

Thanks so much for your help in advance. My application where users can select some videos and then saved works in React + Youtube API. I've been successfully fetched videos from Youtube right now. But I have two matters for completely building it done.

1) onClick() is not working in CardAction Container
2) isSelected is not working in 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;

Upvotes: 1

Views: 231

Answers (1)

FX2000
FX2000

Reputation: 141

Can you use ES6?

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

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

Upvotes: 2

Related Questions