0

I am new in reactJs, i was trying to do nested route for one component and it's not working following way i was trying to do nested and error occur like this

Warning: You should not use and in the same route; will be ignored

render(
<Provider store={store}>
    <ConnectedRouter history={history}>
        <div>
            <Route exact path="/" component={App} />
            <Route exact path="/test" component={App}/>
            <Route  path="/register" component={Registration}>
                <Route path="/register/pet/more" component={App}/>
            </Route>
        </div>
    </ConnectedRouter>
</Provider>,
document.getElementById('root'))

So what should i have to do for nested route for one component. Thanks

2 Answers 2

1

You can avoid nested routes and instead use a combination of Switch and exact in react-router-dom. Here's how it can be done.

<Switch>
    <Route exact path="/" component={App} />
    <Route exact path="/test" component={App}/>
    <Route exact path="/register" component={Registration} />
    <Route exact path="/register/pet/more" component={App} />
</Switch>

The idea is that Switch selects only the matching route, and exact does an exact match, so this should solve your problem.

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

3 Comments

<Switch> <Route exact path="/" component={App} /> <Route exact path="/test" component={App}/> <Route exact path="/register" component={Registration} /> <Route exact path="/register/pet/more" component={Registration} /> </Switch> Can i route one component with two path as like above code?
Yes, you can. Switch will choose the route that exactly matches the path defined.
It's a good practice to upvote answers that solve your problem, as it motivates better answers.
1

what version of react-router are you using? If you are using React-router version 4 you put your nested route in another component.see the link below Nested routes with react router v4

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.