ReactJS - 元素类型无效错误

时间:2016-04-28 02:52:33

标签: javascript reactjs

我在下面的代码中出现以下错误,但我无法确定我的组件有什么问题。任何帮助表示赞赏!

  

错误:元素类型无效:期望一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:object。检查App的呈现方法。

index.js

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import YTSearch from 'youtube-api-search'; // Used to interact with Youtube API
import SearchBar from './components/search_bar'; // custom search bar component
import VideoList from './components/video_list'; // custom youtube video list component

const API_KEY = "key not shown here for privacy purposes";

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

        this.state = {videos: []};

        YTSearch({key: API_KEY, term: 'sufboards'}, (videos) => { 
            this.setState({videos});
        });
    }
    render(){
        return (
            <div>
                <SearchBar />
                <VideoList videos={this.state.videos} />
            </div>
        );
    }
}
ReactDOM.render(<App />, document.querySelector(".container"));

webpack.config.js

根据 JordanHendrix 的要求。

module.exports = {
  entry: [
    './src/index.js'
  ],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {
    loaders: [{
      exclude: /node_modules/,
      loader: 'babel'
    }]
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  devServer: {
    historyApiFallback: true,
    contentBase: './'
  }
};

video_detail.js

根据 bhargavponnapalli

的要求
import React from 'react';

const VideoList = (props) => {
    return (
        <ul className="col-md-4 list-group">
            {props.videos.length}
        </ul>
    );
};

export default VideoList;

search_bar.js

根据 bhargavponnapalli

的要求
import React, {Component} from 'react';

class SearchBar extends Component {
    constructor(props){
        super(props);
        this.state = {term: ""};
    }

    render(){
        return (
            <input
                onChange={
                    event => this.setState({
                        term: event.target.value
                    })
                }
                value = {this.state.term} 
            />
        );
    }
}

export default SearchBar;

我的其他组件所需的任何额外代码都将根据要求添加到此处。

1 个答案:

答案 0 :(得分:1)

简单修复:

import VideoList from './components/video_list';

该文件为空,我的代码位于video_detail.js,因此我必须将其移至正确的文件,video_list.js