Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BrowserRouter vs Router with history.push()

I am trying to understand the difference between BrowserRouter and Router of the react-router-dom (v5) package and what difference it makes for my example below.

The documentation says:

BrowserRouter A that uses the HTML5 history API (pushState, replaceState and the popstate event) to keep your UI in sync with the URL.

Source: https://reacttraining.com/react-router/web/api/BrowserRouter

Router The common low-level interface for all router components. Typically apps will use one of the high-level routers instead: BrowserRouter, HashRouter, MemoryRouter, NativeRouter, StaticRouter

Source: https://reacttraining.com/react-router/web/api/Router

From what I understand is that I should be using BrowserRouter for my HTML5 browser apps and I have been doing this so far.

history.push(...) example:

I am trying to perform a history.push('/myNewRoute') within a thunk:

import history as './history';  ...  export function someAsyncAction(input) {   return dispatch => {     fetch(`${API_URL}/someUrl`, {       method: 'POST',       headers: {         'Accept': 'application/json',         'Content-Type': 'application/json'       },       body: JSON.stringify({ input }),     }).then(() => {       history.push('/myNewRoute');     }).catch((err) => {       dispatch(setError(err));     })   }; }; 

history is defined as this module:

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

and the history is also passed to my router:

import { BrowserRouter as Router } from 'react-router-dom'; import history as './history';  ...  const App = () => (   <Router history={history}>      ...   </Router> ); 

Problem: history.push() will update the URL in the browser bar but not render the component behind the route.

If I import Router instead of BrowserRouter, it works:

// Does not work: import { BrowserRouter as Router } from 'react-router-dom';  // Does work: import { Router } from 'react-router-dom'; 
like image 342
mitchkman Avatar asked Jun 21 '19 17:06

mitchkman


People also ask

What is difference between BrowserRouter and Router?

The main difference between the two is the way they store the URL and communicate with your web server. A <BrowserRouter> uses regular URL paths. These are generally the best-looking URLs, but they require your server to be configured correctly.

When should I use BrowserRouter and Router?

This router uses the HTML 5 History API to keep the UI in sync with the the path. BrowserRouter is used for doing client side routing with URL segments. You can load a top level component for each route. This helps separate concerns in your app and makes the logic/data flow more clear.

What is history PUSH IN react Router?

The history. push() function belongs to react-router-dom and used to move from the current page to another one. It takes the first argument as a destination path and a second argument as the state. Note: You can only use this.

What is BrowserRouter and Router in react?

React Router is a standard library for routing in React. It enables navigation between views from different components in a React application, allows the browser URL to be changed, and keeps the UI in sync with the URL.


1 Answers

BrowserRouter ignores the history prop as it handles the history automatically for you. If you need access to the history outside of a react component, then using Router should be fine.

like image 66
Juanso87 Avatar answered Sep 24 '22 04:09

Juanso87