InterviewHubPRO
Section Overview
Q87

Question 87 of 445

Part 2: React Router & Navigation (78 - 88)

20%
React & Modern Frontend (445 Qs)•Part 2: React Router & Navigation (78 - 88)

How to get history on React Router v4?

Below are the list of steps to get history object on React Router v4,

  1. Create a module that exports a history object and import this module across the project.

For example, create history.js file:

javascript
 import { createBrowserHistory } from "history";

 export default createBrowserHistory({
   /* pass a configuration object here if needed */
 });
  1. You should use the <Router> component instead of built-in routers. Import the above history.js inside index.js file:
jsx
 import { Router } from "react-router-dom";
 import history from "./history";
 import App from "./App";

 ReactDOM.render(
   <Router history={history}>
     <App />
   </Router>,
   holder
 );
  1. You can also use push method of history object similar to built-in history object:
javascript
 // some-other-file.js
 import history from "./history";

 history.push("/go-here");