Why is my route in React /#/?

Asked

Viewed 65 times

3

I created a route structure with Hashrouter in React and everything works fine, but my route always http://localhost:3000/#/ + a rota.

return (
    <div>
      <HashRouter>
        <Switch>
          <Route path="/login" component={Login} />
          <Route path="/" component={Dashboard} />
        </Switch>
      </HashRouter>
    </div>
  );

1 answer

4


Why are you using the HashRouter, using location.hash to determine the page the user will see. This part of the URL is called hash.

You can use the BrowserRouter to have a path "normal" managed by this library.


In some cases, when using BrowserRouter (which uses real paths in the URL), you will need to set the location where your React application is hosted to redirect all paths to the SPA location. A redirect like:

/* /index.html 200

Thus you will ensure that even if the user accesses a page from the browser address bar, the application will be accessed by it.

  • 1

    That’s right! Thank you very much for your reply Luiz, helped me a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.