TypeError:道具未定义

时间:2018-10-25 03:31:07

标签: javascript reactjs

我正在开发一个React Video Player应用,该应用对youtube进行api调用,并向用户显示5个视频的列表,并让他们播放VideoDetail组件中返回的第一个Video。但是,我被上述错误困扰,但我仍然觉得自己将构造函数适当地添加到了基于类的组件中。任何有关如何解决此错误的帮助将不胜感激。 这是我的index.js文件的代码。

此文件包含应用程序(父)组件。

import React,{Component} from 'react';
import ReactDOM from 'react-dom';
import SearchBar from './components/search_bar';
import YTSearch from 'youtube-api-search';
import VideoList from './components/video_list';
import VideoDetail from './components/video_detail';

const API_KEY = 'MyApiKey';

class App extends Component(){
  constructor(props){
    super(props);

    this.state = {videos: []};

    YTSearch({key: API_KEY, term: 'internet of things'}, (videos) => {
      this.SetState({videos});
    });
  }
  render(){
    return(
          <div>
          <SearchBar />
          <VideoDetail video={this.state.videos[0]} />
          <VideoList videos = {this.state.videos} />
          </div>
    );
  }
}

ReactDOM.render(<App />, document.querySelector('.container'));

video_detail.js文件具有VideoDetail组件的代码

import React from 'react';

const VideoDetail = ({video}) =>{
  if (!video){
    return(
      <div>Loading...</div>
    )
  }

  const videoId = video.id.videoId;
  const url = `www.youtube.com/embed/${videoId}`;

  return(
    <div className="video-detail col-md-8">
      <div className="embed-responsive embed-responsive-16by9">
        <iframe className="embed-responsive-item" src = {url}></iframe>
      </div>
      <div className="details">
      <div>{video.snippet.title}</div>
      <div>{video.snippet.description}</div>
      </div>
    </div>
  )
}

export default VideoDetail;

video_list_item.js包含VideoListItem组件的代码

import React from 'react';

const VideoListItem = ({video}) =>{

const imgUrl = video.snippet.thumbnails.default.url;

  return(
    <li className = "list-group-item">
    <div className ="video-list media" >
        <div className ="media-left">
          <img className = "media-object" src = {imgUrl}/>
        </div>

        <div className = "media-body">
          <div className = "media-heading">{video.snippet.title}</div>
        </div>
      </div>
    </li>
  )
}

export default VideoListItem;

video_list.js包含VideoList组件的代码,如下所示

import React from 'react';
import VideoListItem from './video_list_item';

const VideoList = (props) => {
  const videoItems = props.videos.map((video) => {
    return <VideoListItem video = {video} key = {video.etag} />;
  });
  return(
    <ul className="col-md-4 list-group">
      {videoItems}
    </ul>
  );
}

export default VideoList;

search_bar.js包含我尚未接线的SearchBar组件的代码。

import React, {Component} from 'react';

class SearchBar extends Component{
  constructor(props) {
    super(props);

    this.state = {term: ''};
  }
  render(){
    return(
      <div>
      <input />
      </div>
    );
  }
};

export default SearchBar;

1 个答案:

答案 0 :(得分:1)

在您的App文件中,将第17行的class App extends Component() { 更改为class App extends Component {,将this.SetState更改为this.setState