使用React Router 2.0.0rc1-5我在导入后将browserHistory视为未定义:
import { browserHistory } from 'react-router'
软件包似乎安装正确,但无论版本如何,无论是在服务器还是客户端,我都得到了相同的结果。
也许这是一个已知的错误?
答案 0 :(得分:1)
请参阅useRouterHistory: https://github.com/rackt/react-router/blob/master/upgrade-guides/v2.0.0.md#using-custom-histories
我在服务器端使用它:
import {Router, RouterContext, match, useRouterHistory} from 'react-router';
import {createMemoryHistory} from 'history';
// ...
const appHistory = useRouterHistory(createMemoryHistory)({});
const component = (
<Provider store={store} key="provider">
<Router routes={routes} history={appHistory} />
</Provider>
);
答案 1 :(得分:0)
安装react-router 3.0版
npm install --save react-router@3.0
或
yarn add react-router@3.0
然后,两种方法都有效:
方法1
import { Router, useRouterHistory } from 'react-router';
import {createMemoryHistory} from 'history';
import routes from './routes';
const appHistory = useRouterHistory(createMemoryHistory)({});
ReactDOM.render(
<Router history={appHistory} routes={routes}/>,
document.getElementById('root')
);
方法2
import { Router, browserHistory } from 'react-router';
import routes from './routes';
ReactDOM.render(
// or hashHistory
<Router history={browserHistory} routes={routes}/>,
document.getElementById('root')
);