Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push to History in React Router v5?

Is it possible to create a global history file to manage the createBrowserHistory() on react-router-dom v5? I know the V5 has the useHistory() as a way to get the history. But is it possible to retrieve the history from anywhere, like for cases where I am not using a function component?

On V4 I could create a file history.js:

import { createBrowserHistory } from 'history';
export default createBrowserHistory();

It works on V4 https://codesandbox.io/s/react-router-v4-nfwr0

It doesn't work on V5 - It updates the URL but redirects to not-found https://codesandbox.io/s/react-router-v5-not-working-jlrep

like image 950
Mark Nunes Avatar asked Jan 25 '23 22:01

Mark Nunes


1 Answers

As the doc says you should use the v4 of history to work on react-router v5.

https://github.com/ReactTraining/history

Documentation for version 4 can be found on the v4 branch. Version 4 is used in React Router versions 4 and 5.

like image 190
Filipe Marins Avatar answered Jan 27 '23 11:01

Filipe Marins