Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between BrowserRouter and Router in react-router?

I’m trying to implement basic routing for my application but got stuck with the difference between the BrowserRouter and Router. In my case Router is working properly, BrowserRouter is not routing properly.

I’m using a history object. When the user clicks a button, it needs to be taken to the login page history.push('/login')


const Routing = () => 
    <Router history={history}>
        <Switch>
            <Route exact path="/" component={Home} />
            <Route path="/login" component={Login} />
        </Switch>
    </Router>

Above one work, but if I use BrowserRouter instead of Router it is not working properly.

like image 299
imThamizhselvan Avatar asked Jun 26 '19 03:06

imThamizhselvan


People also ask

What is BrowserRouter and router in react?

BrowserRouter: BrowserRouter is a router implementation that uses the HTML5 history API(pushState, replaceState and the popstate event) to keep your UI in sync with the URL. It is the parent component that is used to store all of the other components.

What is the difference between HashRouter and BrowserRouter in react?

Both BrowserRouter and HashRouter components were introduced in React Router ver. 4 as subclasses of Router class. Simply, BrowserRouter syncs the UI with the current URL in your browser, This is done by the means of HTML-5 History API. On the other hand, HashRouter uses the Hash part of your URL to sync.

Is react router and react router dom same?

They are technically three different packages: React Router, React Router DOM, and React Router Native. The primary difference between them lies in their usage. React Router DOM is for web applications and React Router Native is for mobile applications made with React Native.

How many types of routers are there in react?

On the basis of the part of URL that the router will use to track the content that the user is trying to view, React Router provides three different kinds of routers: Memory Router. Browser Router. Hash Router.


2 Answers

Simply, they are both to equal each other:

import { BrowserRouter } from 'react-router-dom'

<BrowserRouter>
  <App />
</BrowserRouter>

and

import { Router } from 'react-router-dom'
import { createBrowserHistory } from 'history'

const history = createBrowserHistory()

<Router history={history}>
  <App />
</Router>
like image 163
Jack Chen Avatar answered Oct 11 '22 18:10

Jack Chen


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

So your routes should be like this,

const Routing = () => <BrowserRouter> <Switch> <Route exact path="/" component={Home} /> <Route path="/login" component={Login} /> </Switch> </BrowserRouter>

For navigation on click of a button you can use Redirect from react-router-dom package.

import { Redirect } from 'react-router-dom'

on click of button,

<Redirect to="/login" />
like image 39
ravibagul91 Avatar answered Oct 11 '22 17:10

ravibagul91