Router: [Feature] Basename API

Created on 13 Oct 2018  路  4Comments  路  Source: reach/router

Is it possible to get an API for basename? So that we can do something like:

<Router basename="/admin">
  <Home path="/" />
  <Login path="/login" />
</Router>

and have Home at /admin, and Login at /admin/login.

Documentation for React Router's basename API

Most helpful comment

It appears that a prop exists called basepath which accomplishes this same thing - though it is specified in the documentation as normally used internally.

import './App.css';

import { Router } from '@reach/router';
import React, { Component } from 'react';

const Page1 = props => <div>page1</div>;
const Page2 = props => <div>page2</div>;
const Page3 = props => <div>page3</div>;

class App extends Component {
  render() {
    return (
      <Router basepath="test">
        <Page1 path="page1" />
        <Page2 path="page2" />
        <Page3 path="page3" />
      </Router>
    );
  }
}

export default App;

Page1, Page2, Page3 are all accessible at /test/page1 and so on.

All 4 comments

I did something similar by:

const RouteWrapper = ({ children }) => <React.Fragment>{children}</React.Fragment>;

<Router>
  <RouteWrapper path="/admin">
    <Home path="/" />
    <Login path="/login" />
  </RouteWrapper>
  <RouteWrapper path="/admin-two">
    <Home path="/" />
    <Login path="/login" />
  </RouteWrapper>
</Router>

This was so I could see an overview of all my routes via indentation. If there's a better way please let me know!

@px-tristan-davey I wasn't aware you could do that, neat! I guess that works fine, but I'll leave this issue open.

It appears that a prop exists called basepath which accomplishes this same thing - though it is specified in the documentation as normally used internally.

import './App.css';

import { Router } from '@reach/router';
import React, { Component } from 'react';

const Page1 = props => <div>page1</div>;
const Page2 = props => <div>page2</div>;
const Page3 = props => <div>page3</div>;

class App extends Component {
  render() {
    return (
      <Router basepath="test">
        <Page1 path="page1" />
        <Page2 path="page2" />
        <Page3 path="page3" />
      </Router>
    );
  }
}

export default App;

Page1, Page2, Page3 are all accessible at /test/page1 and so on.

Ah, that's perfect — not sure how I missed it. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

magnusarinell picture magnusarinell  路  3Comments

hardfist picture hardfist  路  3Comments

Kadrian picture Kadrian  路  5Comments

michaelwiktorek picture michaelwiktorek  路  4Comments

benwiley4000 picture benwiley4000  路  4Comments