Nested Routes not rendering with React Router v4












2















I am trying to group some of my routes together with React Router v4 to clean up some of my components. For now I just want to have my non logged in routes group together and my admin routes grouped together but the following doens't work.



main.js



const Main = () => {
return (
<main>
<Switch>
<Route exact path='/' component={Public} />
<Route path='/admin' component={Admin} />
</Switch>
</main>
);
};

export default Main;


public.js



const Public = () => {
return (
<Switch>
<Route exact path='/' component={Greeting} />
<Route path='/signup' component={SignupPage} />
<Route path='/login' component={LoginPage} />
</Switch>
);
};

export default Public;


The Greeting component shows at "localhost:3000/", but the SignupPage component does not show at "localhost:3000/signup" and the Login component doesn't show at "localhost:3000/signup". Looking at the React Dev Tools these two routes return Null.










share|improve this question





























    2















    I am trying to group some of my routes together with React Router v4 to clean up some of my components. For now I just want to have my non logged in routes group together and my admin routes grouped together but the following doens't work.



    main.js



    const Main = () => {
    return (
    <main>
    <Switch>
    <Route exact path='/' component={Public} />
    <Route path='/admin' component={Admin} />
    </Switch>
    </main>
    );
    };

    export default Main;


    public.js



    const Public = () => {
    return (
    <Switch>
    <Route exact path='/' component={Greeting} />
    <Route path='/signup' component={SignupPage} />
    <Route path='/login' component={LoginPage} />
    </Switch>
    );
    };

    export default Public;


    The Greeting component shows at "localhost:3000/", but the SignupPage component does not show at "localhost:3000/signup" and the Login component doesn't show at "localhost:3000/signup". Looking at the React Dev Tools these two routes return Null.










    share|improve this question



























      2












      2








      2


      2






      I am trying to group some of my routes together with React Router v4 to clean up some of my components. For now I just want to have my non logged in routes group together and my admin routes grouped together but the following doens't work.



      main.js



      const Main = () => {
      return (
      <main>
      <Switch>
      <Route exact path='/' component={Public} />
      <Route path='/admin' component={Admin} />
      </Switch>
      </main>
      );
      };

      export default Main;


      public.js



      const Public = () => {
      return (
      <Switch>
      <Route exact path='/' component={Greeting} />
      <Route path='/signup' component={SignupPage} />
      <Route path='/login' component={LoginPage} />
      </Switch>
      );
      };

      export default Public;


      The Greeting component shows at "localhost:3000/", but the SignupPage component does not show at "localhost:3000/signup" and the Login component doesn't show at "localhost:3000/signup". Looking at the React Dev Tools these two routes return Null.










      share|improve this question
















      I am trying to group some of my routes together with React Router v4 to clean up some of my components. For now I just want to have my non logged in routes group together and my admin routes grouped together but the following doens't work.



      main.js



      const Main = () => {
      return (
      <main>
      <Switch>
      <Route exact path='/' component={Public} />
      <Route path='/admin' component={Admin} />
      </Switch>
      </main>
      );
      };

      export default Main;


      public.js



      const Public = () => {
      return (
      <Switch>
      <Route exact path='/' component={Greeting} />
      <Route path='/signup' component={SignupPage} />
      <Route path='/login' component={LoginPage} />
      </Switch>
      );
      };

      export default Public;


      The Greeting component shows at "localhost:3000/", but the SignupPage component does not show at "localhost:3000/signup" and the Login component doesn't show at "localhost:3000/signup". Looking at the React Dev Tools these two routes return Null.







      reactjs react-router react-router-v4






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 5 '18 at 17:59









      Shubham Khatri

      82.6k15100140




      82.6k15100140










      asked Aug 11 '17 at 2:53









      Cody James TookerCody James Tooker

      52119




      52119
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The reason is very obvious. for your route in main.js, you have specified the Route path of Public component with exact exact path='/' and then in the Public component you are matching for the other Routes. So if the route path is /signup, at first the path is not exact so Public component is not rendered and hence no subRoutes will.



          Change your route configuration to the following



          main.js



          const Main = () => {
          return (
          <main>
          <Switch>
          <Route path='/' component={Public} />
          <Route path='/admin' component={Admin} />
          </Switch>
          </main>
          );
          };

          export default Main


          public.js



          const Public = () => {
          return (
          <Switch>
          <Route exact path='/' component={Greeting} />
          <Route path='/signup' component={SignupPage} />
          <Route path='/login' component={LoginPage} />
          </Switch>
          );
          };


          Also when you are specifying the nested routes these should be relative to the parent Route, for instance if the parent route is /home and then in the child Route you wish to write /dashboard . It should be written like



          <Route path="/home/dashboard" component={Dashboard}


          or even better



          <Route path={`${this.props.match.path}/dashboard`} component={Dashboard}





          share|improve this answer


























          • Thanks for the feedback. That does make sense. I guess I was trying to combine old React Router nesting with React Router v4 methods. Thanks for the help. It is obvious now that I look at it.

            – Cody James Tooker
            Aug 11 '17 at 15:38











          • Not a problem, glad to have helped

            – Shubham Khatri
            Aug 11 '17 at 17:13











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45626214%2fnested-routes-not-rendering-with-react-router-v4%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The reason is very obvious. for your route in main.js, you have specified the Route path of Public component with exact exact path='/' and then in the Public component you are matching for the other Routes. So if the route path is /signup, at first the path is not exact so Public component is not rendered and hence no subRoutes will.



          Change your route configuration to the following



          main.js



          const Main = () => {
          return (
          <main>
          <Switch>
          <Route path='/' component={Public} />
          <Route path='/admin' component={Admin} />
          </Switch>
          </main>
          );
          };

          export default Main


          public.js



          const Public = () => {
          return (
          <Switch>
          <Route exact path='/' component={Greeting} />
          <Route path='/signup' component={SignupPage} />
          <Route path='/login' component={LoginPage} />
          </Switch>
          );
          };


          Also when you are specifying the nested routes these should be relative to the parent Route, for instance if the parent route is /home and then in the child Route you wish to write /dashboard . It should be written like



          <Route path="/home/dashboard" component={Dashboard}


          or even better



          <Route path={`${this.props.match.path}/dashboard`} component={Dashboard}





          share|improve this answer


























          • Thanks for the feedback. That does make sense. I guess I was trying to combine old React Router nesting with React Router v4 methods. Thanks for the help. It is obvious now that I look at it.

            – Cody James Tooker
            Aug 11 '17 at 15:38











          • Not a problem, glad to have helped

            – Shubham Khatri
            Aug 11 '17 at 17:13
















          1














          The reason is very obvious. for your route in main.js, you have specified the Route path of Public component with exact exact path='/' and then in the Public component you are matching for the other Routes. So if the route path is /signup, at first the path is not exact so Public component is not rendered and hence no subRoutes will.



          Change your route configuration to the following



          main.js



          const Main = () => {
          return (
          <main>
          <Switch>
          <Route path='/' component={Public} />
          <Route path='/admin' component={Admin} />
          </Switch>
          </main>
          );
          };

          export default Main


          public.js



          const Public = () => {
          return (
          <Switch>
          <Route exact path='/' component={Greeting} />
          <Route path='/signup' component={SignupPage} />
          <Route path='/login' component={LoginPage} />
          </Switch>
          );
          };


          Also when you are specifying the nested routes these should be relative to the parent Route, for instance if the parent route is /home and then in the child Route you wish to write /dashboard . It should be written like



          <Route path="/home/dashboard" component={Dashboard}


          or even better



          <Route path={`${this.props.match.path}/dashboard`} component={Dashboard}





          share|improve this answer


























          • Thanks for the feedback. That does make sense. I guess I was trying to combine old React Router nesting with React Router v4 methods. Thanks for the help. It is obvious now that I look at it.

            – Cody James Tooker
            Aug 11 '17 at 15:38











          • Not a problem, glad to have helped

            – Shubham Khatri
            Aug 11 '17 at 17:13














          1












          1








          1







          The reason is very obvious. for your route in main.js, you have specified the Route path of Public component with exact exact path='/' and then in the Public component you are matching for the other Routes. So if the route path is /signup, at first the path is not exact so Public component is not rendered and hence no subRoutes will.



          Change your route configuration to the following



          main.js



          const Main = () => {
          return (
          <main>
          <Switch>
          <Route path='/' component={Public} />
          <Route path='/admin' component={Admin} />
          </Switch>
          </main>
          );
          };

          export default Main


          public.js



          const Public = () => {
          return (
          <Switch>
          <Route exact path='/' component={Greeting} />
          <Route path='/signup' component={SignupPage} />
          <Route path='/login' component={LoginPage} />
          </Switch>
          );
          };


          Also when you are specifying the nested routes these should be relative to the parent Route, for instance if the parent route is /home and then in the child Route you wish to write /dashboard . It should be written like



          <Route path="/home/dashboard" component={Dashboard}


          or even better



          <Route path={`${this.props.match.path}/dashboard`} component={Dashboard}





          share|improve this answer















          The reason is very obvious. for your route in main.js, you have specified the Route path of Public component with exact exact path='/' and then in the Public component you are matching for the other Routes. So if the route path is /signup, at first the path is not exact so Public component is not rendered and hence no subRoutes will.



          Change your route configuration to the following



          main.js



          const Main = () => {
          return (
          <main>
          <Switch>
          <Route path='/' component={Public} />
          <Route path='/admin' component={Admin} />
          </Switch>
          </main>
          );
          };

          export default Main


          public.js



          const Public = () => {
          return (
          <Switch>
          <Route exact path='/' component={Greeting} />
          <Route path='/signup' component={SignupPage} />
          <Route path='/login' component={LoginPage} />
          </Switch>
          );
          };


          Also when you are specifying the nested routes these should be relative to the parent Route, for instance if the parent route is /home and then in the child Route you wish to write /dashboard . It should be written like



          <Route path="/home/dashboard" component={Dashboard}


          or even better



          <Route path={`${this.props.match.path}/dashboard`} component={Dashboard}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 24 '17 at 12:40

























          answered Aug 11 '17 at 5:44









          Shubham KhatriShubham Khatri

          82.6k15100140




          82.6k15100140













          • Thanks for the feedback. That does make sense. I guess I was trying to combine old React Router nesting with React Router v4 methods. Thanks for the help. It is obvious now that I look at it.

            – Cody James Tooker
            Aug 11 '17 at 15:38











          • Not a problem, glad to have helped

            – Shubham Khatri
            Aug 11 '17 at 17:13



















          • Thanks for the feedback. That does make sense. I guess I was trying to combine old React Router nesting with React Router v4 methods. Thanks for the help. It is obvious now that I look at it.

            – Cody James Tooker
            Aug 11 '17 at 15:38











          • Not a problem, glad to have helped

            – Shubham Khatri
            Aug 11 '17 at 17:13

















          Thanks for the feedback. That does make sense. I guess I was trying to combine old React Router nesting with React Router v4 methods. Thanks for the help. It is obvious now that I look at it.

          – Cody James Tooker
          Aug 11 '17 at 15:38





          Thanks for the feedback. That does make sense. I guess I was trying to combine old React Router nesting with React Router v4 methods. Thanks for the help. It is obvious now that I look at it.

          – Cody James Tooker
          Aug 11 '17 at 15:38













          Not a problem, glad to have helped

          – Shubham Khatri
          Aug 11 '17 at 17:13





          Not a problem, glad to have helped

          – Shubham Khatri
          Aug 11 '17 at 17:13


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45626214%2fnested-routes-not-rendering-with-react-router-v4%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine