2

I'm setting up nested routes within React-router-dom (v5.0.1), but it (route /primary/:id) is not working whenever I try and access one of the nested routes.

Nested route looks like..

const id = ({match}) =>  (
    <div>
        with id {match.params.id}
    </div>
)

const primary = ({match}) => (
    <div>
        This is the Primary route
        <br/>
        <Link to='/primary/one'>Primary with id</Link>
        <Route path='/primary/:id' component={id} />
    </div>
)

I cannot access the id component. I call the primary component in Routes component.

const Routes = () => {
    return(
        <Router>
            <Route exact path='/' component={main} />
            <Route exact path='/primary' component={primary} />    
        </Router>
    )
}

And Routes are called in App component as follows.

function App() {
  return (
    <div className="App">
      <Routes />
    </div>
  );
}

2 Answers 2

2

Define two Routes for primary and id components as follows.


const id = () =>  (
    <div>
        with id {props.id}
    </div>
)

const primary = () => (
    <div>
        This is the Primary route
        <br/>
        {
          props.match.params.id ? 
            <id id={props.match.params.id}/>
         : ""
        }

        <Link to='/primary/one'>Primary with id</Link>
    </div>
)
const Routes = () => {
    return(
        <Router>
            <Route exact path='/' component={main} /> 
            <Route exact path='/primary' component={primary} />
            <Route exact path='/primary/:id' component={id} />
        </Router>
    )
}

EDIT The below is not tested. Try if this works.

const primary = ({match}) => (
    <div>
        This is the Primary route
        <br/>
        <Link to='/primary/one'>Primary with id</Link>
        {<Route path='/primary/:id' component={id} />}
    </div>
)
Sign up to request clarification or add additional context in comments.

4 Comments

But i want ``` <Route exact path='/primary/:id' component={id} /> ``` this route inside primary component.. isn't that possible..?
Did you mean primary component works as a layout for each and every id component?
Since id component is accessed from primary component , I want that route inside primary component rather than in Routes or App component.. It seems that was working on earlier version of react-router-dom. I don't know why this isn't working now.
That's not working.. I think I have to declare primary route without path.. <Route component={primary} /> ... Now its working..
0

You just put this code in your routes:

const Routes = () => {
    return(
        <Router>
            <Route exact path='/' component={main} />
            <Route exact path='/primary/:id' component={primary} />    
        </Router>
    )
}

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.