我开始学习反应,我想在一个简单的项目中集成反应路由器。代码是:
var React = require('react');
var ReactDOM = require('react-dom');
var Router = require('react-router');
var Route = Router.Route;
var hashHistory = Router.hashHistory;
var IndexRoute = Router.IndexRoute;
var APP = require('./components/APP');
var Map = require('./components/Maps');
var Graph = require('./components/Graph');
var routes = (
<Router history={hashHistory}>
<Route path="/" component={APP}></Route>
<Route path="maps" component={Maps}></Route>
<Route path="graph" component={Graph}></Route>
</Router>
)
ReactDOM.render(routes, document.getElementById('react-container'));
但是我收到以下错误: 警告:React.createElement:type无效 - 期望一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:object。
由于
答案 0 :(得分:1)
路由器组件不是'react-router'库的默认导出。代码中的Router
对象只是一个包装器对象,其中包含“react-router”库的各个部分,包括Router
和Route
组件。因此,访问Router
组件的方式与访问Route
组件的方式相同:
var router = require('react-router');
var Router = router.Router;
var Route = router.Route;