M
M
Marat Ivanov2019-08-15 11:38:37
React
Marat Ivanov, 2019-08-15 11:38:37

How in the route, if one url path is given, add an additional path to it?

The bottom line is, is there a <Route path="/first" component={first} />way to make it so that when switching to /first , /item1 is automatically added, that is, the general path automatically becomes /first/item1? All options are interesting.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Suntsev, 2019-08-15
@GreyCrew

There are several options, but as I see it, it is best to think through the architecture correctly.
When you go to /first, you probably open some kind of container in which other routes are located (/first/item1, /first/item2 etc)
An automatic redirect can be done in the First component, where you describe nested routes

<Switch>
     <Redirect
            from={"/first"}
            exact
            to={"/first/item1"}
            render={() => <Item {...props}/>}
      />
      <Route
            path={"/first/item1"}
            render={() => <Item {...props}/>}
      />
     <Route
            path={ '/first/item2'}
            render={() => <List {...props}/>}
     />
</Switch>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question