简体   繁体   中英

React - React Router Dom, hashbang routing solution

How can I make routing in React, using react-router-dom, using hashbang? Something like

http://somesite.com/#/home

http://somesite.com/#/about

is fine by me.

In Angular routing, I used { useHash: true } in RouterModule of angular/router to achieve that.

In Vue routing, I used history: createWebHashHistory() in createRouter method of vue-router to achieve that.

Is there a way I achieve that here?

If it's not - please suggest me some other solid routing libraries for React.

PS If you wonder why I need it, the answer is IIS. And I don't want to go through overcomplicated procedure of getting it to work on IIS.

You need to use a HashRouter : https://reactrouter.com/web/api/HashRouter

For example, you can do the following:

import { HashRouter as Router, Route, Switch } from 'react-router-dom';

const App = () => {
  return (
    <Router>
      <Switch>
        <Route exact path="/foo">
          <Component1/>
        </Route>
        <Route exact path="/bar">
          <Component2/>
        </Route>
      </Switch>
    </Router>);
}

Use <HashRouter> instead of one of the other routers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM