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'));
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.
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.
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)
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
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") );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With