元素类型无效:期望一个字符串或一个类/函数(对于复合组件)但得到:object

时间:2017-09-11 11:48:16

标签: reactjs react-router-v4

我是新来的反应,并从复数视频中学到同样的东西。我在实施react-router时遇到了这个错误:

  

元素类型无效:期望一个字符串(用于内置组件)或一个类/函数(用于复合组件)但得到:object。

这是我的代码 -

1)main.js

"use strict";
var React = require('react');
var createReactClass = require('create-react-class');
var ReactDOM = require('react-dom');
var Router = require('react-router'); 
var routes = require('./routes');
ReactDOM.render(<Router routes={routes}></Router>,document.getElementById('app'))

2)routes.js

var React = require('react');
var Router = require('react-router');
var DefaultRoute = Router.DefaultRoute;
var Route = Router.Route;

var routes = (
        <Route name="app" path="/" handler={require('./components/app')}>
            <DefaultRoute handler={require('./components/homePage')} />
            <Route name="authors" handler={require('./components/authors/authorPage')}/>
            <Route name="about" handler={require('./components/about/aboutPage')}/>
        </Route>
    );

module.exports = routes;

3)app.js

var React = require('react');
var ReactDOM = require('react-dom');
var createReactClass = require('create-react-class');
var Header = require('./common/header');
var RouteHandler = require('react-router').RouteHandler;

var App = createReactClass({
    render:function(){
        return (
            <div>
                <Header/>
                <RouteHandler/> 
            </div>
        );
    }
});

module.exports = App;

版本 - &#34; react-router&#34;:&#34; ^ 4.2.0&#34;

1 个答案:

答案 0 :(得分:0)

自react-router v1.0起,DefaultRoute消失了。而是引入了IndexRoute。而且自从v4 IndexRoute也消失了。所以我猜你使用的是v4 React路由器,但语法是0.1x版本。您应该使用旧版本的路由器或更新的语法。