Alex Ironside
Alex Ironside

Reputation: 5049

'react-router' does not contain an export named 'browserHistory'

So I'm trying to use history in React, but it seems to be moved around a lot. I found this:

https://github.com/ReactTraining/react-router/blob/ab4552d2ea0ec5c0cf3c534bca654a1af3ea0dec/docs/guides/NavigatingOutsideOfComponents.md

but it just says that 'react-router' does not contain an export named 'browserHistory'. How can I solve it? Where can I find history? All answers I found were around 1.5 years old.

Upvotes: 2

Views: 3338

Answers (3)

Henrik Andersson
Henrik Andersson

Reputation: 47172

You're looking for the BrowserRouter.

import { BrowserRouter } from 'react-router';

browserHistory is something that existed in v2/v3 but was rewritten into BrowserRouter in v4. There is a migration guide here that should help you.

Upvotes: 2

illiteratewriter
illiteratewriter

Reputation: 4333

You no longer have browserHistory apparently. So you can do this instead,

<Route>
 <Header />
</Route>

and you can do

function Header(props) {
 // props.history and props.location are available now inside this component
}

and then you can do props.history.push or whatever if that is what you want to do.

The link you have is for the older version of react-router so check out react-router 4

Upvotes: 0

Shubham Khatri
Shubham Khatri

Reputation: 281706

history Api has been moved to a separate history package

In order to create browserHistory, you install history like

npm install -S history

and write

import createHistory from "history/createBrowserHistory"

const browserHistory = createHistory()
export { browserHistory };

However in react-router-v4 an equivalent of Router with browserHistory is BrowserRouter

Upvotes: 0

Related Questions