0

I am currently using react-router-dom to create navigation within my web app.

My index.js and App.js look like:

Index.js

ReactDOM.render(
    <BrowserRouter>
        <Provider store={store}>
            <App />
        </Provider>
    </BrowserRouter>,
    document.getElementById('root')
);

App.js

class App extends React.Component {
  render() {
    return (
      <BrowserRouter>
        <div>
          <Route exact path ='/' component={Home} />
          <Route path ='/container' component={Container} />
          <Route path ='/profile' component={ProfilePage} />
        </div>
      </BrowserRouter>
    );
  }
}

I idea was that if Home contains the header and the sidebar, it would also keep it for other components like Container and ProfilePage.

My Home's render looks like:

  render() {
    return (
      <div>
        <Header />
        <div className="App">
          <Sidebar />
          <Container className="container-comp" />
          {this.renderLoggingOutput()}

        </div>
      </div>
    );
  }

But when I Link to /profile, it just shows me the ProfilePage component without the header and the sidebar.

Any help?

2 Answers 2

4

Put your header outside the routes:

class App extends React.Component {
  render() {
    return (
      <BrowserRouter>
       <Header /> <-------- place here or outside the routes at a minimum
        <div>
          <Route exact path ='/' component={Home} />
          <Route path ='/container' component={Container} />
          <Route path ='/profile' component={ProfilePage} />
        </div>
      </BrowserRouter>
    );
  }
}

I have a similar structure in one of my apps. the routes look like this:

class App extends Component {
  render() {
    return (
      <BrowserRouter>
        <div className="App">
          <Navbar />
          <Switch>
            <Route exact path='/' component={Dashboard}/>
            <Route path='/character/:id' component={CharacterDetails}/>
            <Route path='/encounter/:id' component={Encounter}/>
.........
          </Switch>
        </div>
      </BrowserRouter>
    );
  }
}

export default App;
Sign up to request clarification or add additional context in comments.

Comments

2

The only job of a route is to mount a component. If you want omnipresent components across all routes, treat the routes as children to a base component.

class App extends React.Component {
  render() {
    <div>
      <Header />
      <Sidebar />
      {this.props.children}
    </div>
  }
}

const Routes = () => (
  <App>
    {/* your routes here */}
  </App>
)

2 Comments

Do we put {this.Routes} or this.props.children? Cuz the this.props.children doesn't actually use Routes
You would put this.props.children. this.props.children is a special prop in react. With this.props.children, whatever you put in between <App> in the Routes will show up where this.props.children is in your App component. You can read more about it in the docs.

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.