0

I have some React routes that when I nest one route inside another, I need to repeat the route path.

To explain, for example:

<Route
        path="admin">
        <Switch>
            <Route
                path="admin/specific/:id"
                component={SpecificAdmin} />
            <Route
                exact
                path="admin"
                component={AdminPage}>
            </Route>
            <Route
                exact
                path="admin/edit/new"
                component={EditSpecificAdmin} />
        </Switch>
    </Route>

I want a page where I can see the list of items, one for adding a new one and another for looking, editing a specific item. So I thought about the paths edit/new and specific/1. So the routes do not detect when I write specific/1 (the specific id) and not either the admin nesting, so I need to write the admin in each one...

5
  • can you share full code so anyone was able to recreate your issue? Commented Nov 15, 2019 at 15:51
  • Basically, what you want is to pass to your nested route '/specific/:id', '/', '/edit/new'. is that so ? Commented Nov 15, 2019 at 15:53
  • @ThomasBihan-Poudec yes Commented Nov 15, 2019 at 15:55
  • 1
    as @tareqAziz answer, you can get some of those intel in props, in one of my project, I created another router for the nested route. ie: in your project, i would create a AdminRouter component and pass the pass as path={${match.path}/specific/:id}, etc Commented Nov 15, 2019 at 16:00
  • I think match.path will return /something/:id which is actually a format, not a partial url of current page. Rather match.url will return /something/1 or /something/anything which can be used as current page's url Commented Nov 15, 2019 at 16:23

2 Answers 2

1

As Tareq aziz said, you can easily have intel in props.

You can create another router to pass easily new value:

// your original component
import AdminRouter from './Admin/Router';

export default () => {
  return (
    <Route path="admin">
     <AdminRouter />     
    </Route>
  );
}

// in ./Admin/Router.js
export default (props) => {
  return (
    <Switch>
        <Route 
            exact
            path={`${props.match.path}/specific/:id`}
            component={SpecificAdmin}
        />
        <Route 
            exact
            path={`${props.match.path}`}
            component={AdminPage}
        />
        <Route 
            exact
            path={`${props.match.path}/edit/new`}
            component={EditSpecificAdmin}
        />
    </Switch>
  );
}

I'm not sure though if the order of the routes are correct.

Sign up to request clarification or add additional context in comments.

2 Comments

That's nice but I'm trying to understand why the route does not do this by itself
@PichiWuana Because it keeps open to configure by user
1

I think you can get your current page's url from props using location.pathname or match.url. You can see my image. Then you may add your nested route after that. Hope it will help you

enter image description here

You may code your path like this way

path=`${this.props.location.pathname}/edit/new`

path=`${this.props.location.pathname}/specific/:id`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.