1

So basically I have a navbar that switches between routes. When the page loads, it goes to /home by default but doesn't actually render the App component it's being given. I have to click on the button that brings you to /home in order to render this.

I'm using react-router-dom with Redux.

Here's my BrowserRouter:

<Provider store = {store}>
    <BrowserRouter>
      <div>
        <Header />
        <Route exact path = "/home" component={App}> </Route>
        <Switch>
          <Route  path = "/about" render = {() => <AboutUs />}></Route>
          <Route  path = "/contact" render = {() => <Contact />}></Route>
          <Route  path = "/services" render = {() => <Services />}></Route>
        </Switch>
      </div>
    </BrowserRouter>
  </Provider>

Any advice?

Terry
  • 11
  • 2

3 Answers3

0

The default route is / not /home. You need to setup a redirect.

<Route exact path="/" render={() => ( <Route exact path="/home" component={App} /> )} />

Dávid Szabó
  • 2,119
  • 1
  • 12
  • 24
0

Put your /home route in the Switch with all the other routes:

<Provider store = {store}>
    <BrowserRouter>
      <div>
        <Header />
        <Switch>
          <Route exact path = "/home" component={App}> </Route>
          <Route  path = "/about" render = {() => <AboutUs />}></Route>
          <Route  path = "/contact" render = {() => <Contact />}></Route>
          <Route  path = "/services" render = {() => <Services />}></Route>
        </Switch>
      </div>
    </BrowserRouter>
  </Provider>
Chase DeAnda
  • 13,160
  • 2
  • 26
  • 39
0

You need to put /home inside <Switch>

<Route exact path = "/home" component={App}> </Route>

Means put above line of code inside <Switch> ex:-

<Switch>
      <Route exact path = "/home" component={App}> </Route>
      <Route  path = "/about" render = {() => <AboutUs />}></Route>
      <Route  path = "/contact" render = {() => <Contact />}></Route>
      <Route  path = "/services" render = {() => <Services />}></Route>
</Switch>