React-router: useHistory() return history object different for <Router history={history}>

Created on 27 Jun 2020  路  2Comments  路  Source: ReactTraining/react-router

Version

v5.2.0

Test Case

https://codesandbox.io/s/react-router-689yc

Steps to reproduce

Instead of BrowserRouter, use Router with customized history
then use the hook _useHistory()_ and see the different object return by history

Expected Behavior

to return history object with properties

  1. length
  2. action
  3. location
  4. createHref
  5. push
  6. replace
  7. go
  8. goBack
  9. goForward
  10. block
  11. listen

Actual Behavior

return properties from history object

  1. action
  2. location
  3. createHref
  4. push
  5. replace
  6. go
  7. back
  8. forward
  9. listen
  10. block

TLDR:
_goBack_ and _goForward_ methods are undefined
for history object when using custom history with Router component

Most helpful comment

Only history 4.x works with Router 5.x.

All 2 comments

Remove 'history' dependency from package.json file and that will do the trick (at least on your test case). @pankaj-ch

Only history 4.x works with Router 5.x.

Was this page helpful?
0 / 5 - 0 ratings