BrowserHistory undefined with React Router 2.00 release candidates

With React Router 2.0.0rc1-5 I get browserHistory as undefined after import:

import { browserHistory } from 'react-router'

The package seems to be installed correctly, but regardless of the version and on the server or client, I got the same result.

Maybe this is a known mistake?

+5
source share
2 answers

See useRouterHistory: https://github.com/rackt/react-router/blob/master/upgrade-guides/v2.0.0.md#using-custom-histories

I use this on the server side:

 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
source

Install reactive router version 3.0

npm install --save react-router@3.0 or

yarn add react-router@3.0

Then both methods work:

Method 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') ); 

Method 2

 import { Router, browserHistory } from 'react-router'; import routes from './routes'; ReactDOM.render( // or hashHistory <Router history={browserHistory} routes={routes}/>, document.getElementById('root') ); 
0
source

Source: https://habr.com/ru/post/1240736/


All Articles