Fraser Price
Fraser Price

Reputation: 909

React Router nested routes not rendering

I am trying to render the Nav component along with any page content in this way because I want to be able to access this.props.location in Nav (to highlight active location on Nav bar), so have assigned it to "/" route and passing child routes rather than simply rendering it. However, only the Nav component renders; none of components from other routes render on any page as {this.props.children} seems to be undefined in Nav. I am new to React so any help would be great.

App.tsx:

const landingPage = () => {
  if (Auth.isUserAuthenticated()) {
    return (
      <UserProfile/>
    );
  } else {
    return (
      <Landing />
    );
  }
};

const routes = () => {
  return (
    <Route path="/" component={Nav}>
      <Route path="/" component={landingPage}/>
      <Route path="/login" component={LoginForm}/>
      <Route path="/register" component={RegistrationForm}/>
      <Route path="/logout" component={Logout}/>
      <Route path="/about" component={About}/>
    </Route>
  )
}

class App extends React.Component<{}, null> {
  render() {
    return (
      <BrowserRouter>
        {routes()}
      </BrowserRouter>
    );
  }
}

Nav.tsx

class Nav extends React.Component<any, any> {
  constructor() {
    super();
  };

  linkActive = (link) => {
    return this.props.location.pathname.includes(link);
  };

  logInOut = () => {
    if (!Auth.isUserAuthenticated()) {
      return (
        [
          <NavItem active={this.linkActive("login")} href="/login">Login</NavItem>,
          <NavItem active={this.linkActive("register")} href="/register">Register</NavItem>
        ]
      );
    } else {
      return (
        <NavItem eventKey={"logout"} href="/logout">Logout</NavItem>
      );
    }
  };

  render() {
    return (
      <div>
        <Navbar className="fluid collapseOnSelect">
          <Navbar.Collapse>
            <Navbar.Header>
              <Navbar.Brand>
                <a href="/">Home</a>
              </Navbar.Brand>
              <Navbar.Toggle />
            </Navbar.Header>
            <BootstrapNav>
              <NavItem active={this.linkActive("about")} href="/about">About</NavItem>
            </BootstrapNav>
            <BootstrapNav pullRight>
              {this.logInOut()}
            </BootstrapNav>
          </Navbar.Collapse>
        </Navbar>
        {this.props.children}
      </div>
    );
  }
}

Upvotes: 0

Views: 766

Answers (1)

loelsonk
loelsonk

Reputation: 3598

Nav.tsx Component

You are using react-router v4 if I am not mistaken.

I want to be able to access this.props.location in Nav (to highlight active location on Nav bar), so have assigned it to "/" route and passing child routes rather than simply rendering it.

To be able to access this.props.location, this.props.history or this.props.match use withRouter High Order Component.

First lets import withRouter HOC.

import { withRouter } from 'react-router-dom';

To use it wrap your Nav Component with withRouter.

// example code
export default withRouter(Nav);

App.tsx Component

Reorganize your routes. I highly suggest using Switch.

// We are still using BrowserRouter 
import {
  BrowserRouter as Router,
  Switch,
} from 'react-router-dom';

<Router>
  <Switch>
    <Route path="/" component={landingPage}/>
    <Route path="/login" component={LoginForm}/>
    <Route path="/register" component={RegistrationForm}/>
    <Route path="/logout" component={Logout}/>
    <Route path="/about" component={About}/>
  </Switch>
</Router>

In your App.tsx render method you can add nav at the top.

Let's pretend only authenticated user can see Nav Component. In your case you don't need to access this.props.children.

renderNav() {
  return (this.props.authenticated) ? <Nav /> : null;
}

render() {
  return (
    <div>
      {this.renderNav()}
      <Router>
        <Switch>
          <Route path="/" component={landingPage}/>
          <Route path="/login" component={LoginForm}/>
          <Route path="/register" component={RegistrationForm}/>
          <Route path="/logout" component={Logout}/>
          <Route path="/about" component={About}/>
        </Switch>
      </Router>
    </div>
  );
}

If you want to learn more about react-router v4 read these examples, guides and apis on react-training. Hope this helps you!

Upvotes: 2

Related Questions