react-router createElement和render失败

时间:2016-03-10 12:06:05

标签: reactjs react-router

我正在探索react-router并遇到了一个非常基本的问题。令人沮丧的部分是我在另一个应用程序中使用react-router,它工作正常!!。

我在控制台中收到此错误。我在这里缺少什么?

警告:React.createElement:type不应为null,undefined,boolean或number。它应该是一个字符串(对于DOM元素)或一个ReactClass(对于复合组件)。

未捕获的不变违规:元素类型无效:期望一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:object。

var React = require('react');
var ReactDOM = require('react-dom');
var Router = require('react-router');
var Route = require('react-router').Route;
var hashHistory = require('react-router').hashHistory;

var App = React.createClass({
  render: function() {
    return <div>App Rendered from React Router!</div>
  }
});

//this works - there is nothing wrong with App component
// ReactDOM.render((
//   <App/>
// ), document.getElementById('app'));


//This works - just so I understand the basics
// var Child = React.createElement('div',{className:'child'});
// var Parent = React.createElement('div',{className:'parent'},Child);
// ReactDOM.render(Parent, document.getElementById('app'));


//this doesn't work - copy pasted from tutorial
ReactDOM.render((
  <Router history={hashHistory}>
    <Route path="/" component={App}/>
  </Router>
), document.getElementById('app'));

//Babel transpiled code
// ReactDOM.render(React.createElement(
//    Router,
//    { history: hashHistory },
//    React.createElement(Route, { path: '/', component: App })
//  ), document.getElementById('app'));


//this doesn't work
// var route = React.createElement(Route, { path: '/', component: App });
// var router = React.createElement(Router,{ history: hashHistory });
//
// ReactDOM.render(router, document.getElementById('app'));

1 个答案:

答案 0 :(得分:3)

您需要导入Router,例如

var Router = require('react-router').Router