Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the hash from the url in react-router

I'm using react-router for my routing and I use the hashHistory option so that I could refresh the page from the browser or specify a url of one of my existing routes and land on the right page. It works fine but I see the hash in the url like this: http://localhost/#/login?_k=ya6z6i

This is my routing configuration:

ReactDOM.render((  <Router history={hashHistory}>     <Route path='/' component={MasterPage}>       <IndexRoute component={LoginPage} />       <Route path='/search' component={SearchPage} />       <Route path='/login' component={LoginPage} />       <Route path='/payment' component={PaymentPage} />     </Route>   </Router>),     document.getElementById('app-container')); 
like image 448
Dennis Nerush Avatar asked Feb 03 '16 16:02

Dennis Nerush


People also ask

How do I get the URL hash in React?

To get query parameters from a hash fragment with React Router v5, we can use the useLocation hook to return the location object and use the location. hash property to get the hash part of the URL. We have the Foo component that calls the useLocation and assign the returned object to the location variable.

How do I remove trailing slash from URL React?

This is a super quick example of how to remove trailing slashes from URLs in React apps that use React Router. The solution is to add the following react router <Redirect ... /> that matches any URL with a trailing slash and automatically redirects it to the same URL without the trailing slash.

What is hash routing in React?

A Router that uses the hash portion of the URL (i.e. window.location.hash) to keep your UI in sync with the URL. ( source: React Router)


2 Answers

Did you try the browserHistory option ? You will be able also to refresh the page from the browser or specify a url of one of existing routes and land on the right page.

import { Router, Route, browserHistory } from 'react-router';  ReactDOM.render((  <Router history={browserHistory}>     <Route path='/' component={MasterPage}>       <IndexRoute component={LoginPage} />       <Route path='/search' component={SearchPage} />       <Route path='/login' component={LoginPage} />       <Route path='/payment' component={PaymentPage} />     </Route>   </Router>),     document.getElementById('app-container')); 

Moreover hashHistory is not for production use considering the react-router github doc.

https://github.com/ReactTraining/react-router/blob/master/docs/guides/Histories.md#browserhistory

Should I use hashHistory?

Hash history works without configuring your server, so if you're just getting started, go ahead and use it. But, we don't recommend using it in production, every web app should aspire to use browserHistory

like image 107
Aaleks Avatar answered Sep 23 '22 19:09

Aaleks


i am new to react but i have used BrowserRouter and it works fine :-

    import React from "react";     import ReactDOM from "react-dom";     import { BrowserRouter, Route, Switch } from "react-router-dom";  ReactDOM.render(   <BrowserRouter >     <Switch>       {indexRoutes.map((prop, key) => {         return <Route to={prop.path} component={prop.component} key={key} />;       })}     </Switch>   </BrowserRouter>,   document.getElementById("root") ); 
like image 24
Eassa Nassar Avatar answered Sep 21 '22 19:09

Eassa Nassar