question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react-router's <Router> history prop expect properties missing from history's BrowserHistory

See original GitHub issue

I’m running into a problem using react-routers low level <Router> component with history’s BrowserHistory. The history prop for <Router> is expected to be a RouterHistory which includes canGo, entries and index not present in BrowserHistory.

import { Router } from "react-router-dom"
import createBrowserHistory from "history/createBrowserHistory"

const history = createBrowserHistory()
const Provider = ({ children }) =>  <Router history={history}>{children}</Router>

This yields the following:

Cannot create Router element because:
 • property canGo is missing in BrowserHistory [1] but exists in RouterHistory [2] in property history.
 • property entries is missing in BrowserHistory [1] but exists in RouterHistory [2] in property history.
 • property index is missing in BrowserHistory [1] but exists in RouterHistory [2] in property history.
 • string [3] is incompatible with undefined [4] in property key of the first argument of the first argument of
   property history.block.
 • string [5] is incompatible with undefined [6] in property state of the first argument of the first argument of
   property history.block.
 • string [7] is incompatible with boolean [8] in the return value of the first argument of property history.block.
 • string [3] is incompatible with undefined [4] in property history.location.key.
 • string [5] is incompatible with undefined [6] in property history.location.state.
 • string [9] is incompatible with LocationShape [10] in the first argument of property history.push.
 • string [11] is incompatible with LocationShape [12] in the first argument of property history.replace.
 • array type [13] is incompatible with undefined [14] in the second argument of property history.replace.

          flow-typed/npm/history_v4.x.x.js
      [5]  14│     state: string,
      [3]  15│     key: string,
             :
 [11][13]  23│     replace: (path: string, Array<mixed>) => void,
             :
      [7]  29│     block: ((location: BrowserLocation, action: Action) => string) => Unblock,
      [9]  30│     push: (path: string) => void,
             :
      [1]  42│   declare export default (opts?: HistoryOpts) => BrowserHistory;

          flow-typed/npm/react-router-dom_v4.x.x.js
      [6]  44│     state?: any,
      [4]  45│     key?: string
             :
     [10]  64│     push(path: string | LocationShape, state?: any): void,
 [12][14]  65│     replace(path: string | LocationShape, state?: any): void,
             :
      [8]  71│       callback: (location: Location, action: HistoryAction) => boolean
             :
      [2] 116│     history: RouterHistory,

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
Ashoatcommented, Jan 30, 2019

I just put up #3110, which should at least make this work without erroring:

<Router history={history.getHistoryObject()}>
  ...
</Router>
5reactions
daratacommented, Aug 20, 2018

Would something like this fix? At the top of the file import from history module

import type {
  BrowserHistory,
  HashHistory,
  MemoryHistory
} from 'history'

Then to declare the RouterHistory Type:

declare export type RouterHistory = BrowserHistory | HashHistory | MemoryHistory
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Router DOM's Router doesn't accept history correctly
The problem should be the missing length , goBack , and goForward properties from the memory history. Is it a bug with react-router...
Read more >
History - React Router: Declarative Routing for React.js
“browser history” - A DOM-specific implementation, useful in web browsers that ... history objects typically have the following properties and methods:.
Read more >
React Router breaking with TS? - The freeCodeCamp Forum
Hey guys, I have an issue accessing the history prop in my Header component that is in the <BrowserRouter> and I get this...
Read more >
React Router v6 in depth guide - YouTube
Welcome to react - router version 6 in-depth guide. In this video, we will learn about react - router v6 but creating a...
Read more >
[Solved]-React Router failed prop 'history', is undefined-Reactjs
or BrowserHistory import { BrowserRouter as Router, Route } from 'react-router-dom'; import App from './components/App'; render(( <Router> <Route exact ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found