React Router失败道具'历史',未定义

时间:2017-03-16 21:42:47

标签: reactjs react-router

我一直在跟随Tyler Mcginnis的教程,并对反应路由器,特别是与历史记录有关。我最后逐字复制他的代码只是为了看看它是否只有我,但我还是得到了

var React = require('react');
var ReactRouter = require('react-router');
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var hashHistory = ReactRouter.hashHistory;
var IndexRoute = ReactRouter.IndexRoute;
var Main = require('../components/Main');
var Home = require("../components/Home");
var PromptContainer = require('../containers/PromptContainer');

var routes = (
  <Router history={hashHistory}>
    <Route path='/' component={Main}>
      <IndexRoute component={Home} />
      <Route path='playerOne' header='Player One' component={PromptContainer} />
      <Route path='playerTwo/:playerOne' header='Player Two' component={PromptContainer} />
    </Route>
  </Router>
);

module.exports = routes;

实施是:

hashHistory

我注意到react-router模块中不存在createBrowserHistory,而history模块中有var BrowserHistory = require('history/createBrowserHistory); const history = createBrowserHistory(); 。按照我发现的API文档,我想我必须通过那里调用它:

createBrowserHistory is not a function

这样做会产生undefined错误。删除paranthesis,导致上述历史记录相同的错误为ReactRouter.hashHistory

当我记录历史记录时,它肯定是未定义的,这让我相信这个问题与import语句有关,但是控制台不会告诉我 constrain(tabBarController.view, containerView) { (tabBarControllerView, containerView) in tabBarControllerView.height == containerView.height tabBarControllerView.width == containerView.width } 无法找到?

我理解这个教程已经有一年了,我可能没有注意到API的变化,这就是我的问题所在。任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:33)

路由器v4有点不同

origin

HashHistory

import { HashRouter as Router, Route } from 'react-router-dom'; import App from './components/App'; render(( <Router> <Route exact path="/" component={App} /> </Router> ), document.getElementById('root'));

BrowserHistory

答案 1 :(得分:0)

var BrowserHistory = require('history/createBrowserHistory');
const history = createBrowserHistory();

你的意思是var createBrowserHistory = require...吗?

此外,现在createBrowserHistory位于v3位于history/lib/createBrowserHistory

如果这不起作用 - 您使用的是什么版本的react-router