How to render the first tab link component of a child component in react nested routes?












1















I'm very much new to react and i'm testing a tabs layout here: https://codesandbox.io/s/lpjlqo7n4z where i have 2 navigations, a Home and About which renders different contents. Inside the Homepage there's 2 more inner navigation links with different routes. Now my question is how to automatically render the component of the first tablink inside the homepage just below the inner navigation?



Assuming everything is imported properly, here's my Index.js code:



  <Router>
<div className="App">
<header>
<div className="logo">
<figure>
<p>Logo</p>
</figure>
</div>
<nav>
<ul>
<li>
<Link to="/" exact>Home</Link>
</li>
<li>
<Link to="/about" exact>About</Link>
</li>
</ul>
</nav>
</header>
<main>
<Switch>
<Route exact path="/" render={props => <Home {...props}/>} />
<Route exact path="/about" render={props => <About {...props} />} />
</Switch>
</main>
</div>
</Router>


Home component:



const Home = (props) => {
return (
<div className="page">
<div className="page-header">
<h1>Homepage</h1>
</div>
<div className="page-nav">
<ul>
<li>
<Link to={`/graphics-design`}>Graphics Design</Link>
</li>
<li>
<Link to={`/development`}>Development</Link>
</li>
</ul>
</div>
<div className="page-content">
<Switch>
<Route path={`${match}/:topicId`} render={props => <DesignTab />} />
<Route
path={`${match}/:topicId`}
render={props => <DevelopmentTab />}
/>
</Switch>
</div>
</div>
);
};

export default Home;


And the first tab component:



const DesignTab = props => {
return (
<div className="tab-content">
<div className="tab-header">
<h1>Design Works</h1>
</div>
<div className="tab-details">
<p>
<span>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tenetur
dignissimos corrupti eius vero maxime architecto similique odio
maiores nihil accusantium iure error et voluptate placeat excepturi
blanditiis, ad numquam itaque.
</span>
</p>
</div>
</div>
);
};

export default DesignTab;


How do i automatically render the tab content just below the inner navigation of the homepage?



Please see the layout here: https://codesandbox.io/s/lpjlqo7n4z










share|improve this question





























    1















    I'm very much new to react and i'm testing a tabs layout here: https://codesandbox.io/s/lpjlqo7n4z where i have 2 navigations, a Home and About which renders different contents. Inside the Homepage there's 2 more inner navigation links with different routes. Now my question is how to automatically render the component of the first tablink inside the homepage just below the inner navigation?



    Assuming everything is imported properly, here's my Index.js code:



      <Router>
    <div className="App">
    <header>
    <div className="logo">
    <figure>
    <p>Logo</p>
    </figure>
    </div>
    <nav>
    <ul>
    <li>
    <Link to="/" exact>Home</Link>
    </li>
    <li>
    <Link to="/about" exact>About</Link>
    </li>
    </ul>
    </nav>
    </header>
    <main>
    <Switch>
    <Route exact path="/" render={props => <Home {...props}/>} />
    <Route exact path="/about" render={props => <About {...props} />} />
    </Switch>
    </main>
    </div>
    </Router>


    Home component:



    const Home = (props) => {
    return (
    <div className="page">
    <div className="page-header">
    <h1>Homepage</h1>
    </div>
    <div className="page-nav">
    <ul>
    <li>
    <Link to={`/graphics-design`}>Graphics Design</Link>
    </li>
    <li>
    <Link to={`/development`}>Development</Link>
    </li>
    </ul>
    </div>
    <div className="page-content">
    <Switch>
    <Route path={`${match}/:topicId`} render={props => <DesignTab />} />
    <Route
    path={`${match}/:topicId`}
    render={props => <DevelopmentTab />}
    />
    </Switch>
    </div>
    </div>
    );
    };

    export default Home;


    And the first tab component:



    const DesignTab = props => {
    return (
    <div className="tab-content">
    <div className="tab-header">
    <h1>Design Works</h1>
    </div>
    <div className="tab-details">
    <p>
    <span>
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Tenetur
    dignissimos corrupti eius vero maxime architecto similique odio
    maiores nihil accusantium iure error et voluptate placeat excepturi
    blanditiis, ad numquam itaque.
    </span>
    </p>
    </div>
    </div>
    );
    };

    export default DesignTab;


    How do i automatically render the tab content just below the inner navigation of the homepage?



    Please see the layout here: https://codesandbox.io/s/lpjlqo7n4z










    share|improve this question



























      1












      1








      1








      I'm very much new to react and i'm testing a tabs layout here: https://codesandbox.io/s/lpjlqo7n4z where i have 2 navigations, a Home and About which renders different contents. Inside the Homepage there's 2 more inner navigation links with different routes. Now my question is how to automatically render the component of the first tablink inside the homepage just below the inner navigation?



      Assuming everything is imported properly, here's my Index.js code:



        <Router>
      <div className="App">
      <header>
      <div className="logo">
      <figure>
      <p>Logo</p>
      </figure>
      </div>
      <nav>
      <ul>
      <li>
      <Link to="/" exact>Home</Link>
      </li>
      <li>
      <Link to="/about" exact>About</Link>
      </li>
      </ul>
      </nav>
      </header>
      <main>
      <Switch>
      <Route exact path="/" render={props => <Home {...props}/>} />
      <Route exact path="/about" render={props => <About {...props} />} />
      </Switch>
      </main>
      </div>
      </Router>


      Home component:



      const Home = (props) => {
      return (
      <div className="page">
      <div className="page-header">
      <h1>Homepage</h1>
      </div>
      <div className="page-nav">
      <ul>
      <li>
      <Link to={`/graphics-design`}>Graphics Design</Link>
      </li>
      <li>
      <Link to={`/development`}>Development</Link>
      </li>
      </ul>
      </div>
      <div className="page-content">
      <Switch>
      <Route path={`${match}/:topicId`} render={props => <DesignTab />} />
      <Route
      path={`${match}/:topicId`}
      render={props => <DevelopmentTab />}
      />
      </Switch>
      </div>
      </div>
      );
      };

      export default Home;


      And the first tab component:



      const DesignTab = props => {
      return (
      <div className="tab-content">
      <div className="tab-header">
      <h1>Design Works</h1>
      </div>
      <div className="tab-details">
      <p>
      <span>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Tenetur
      dignissimos corrupti eius vero maxime architecto similique odio
      maiores nihil accusantium iure error et voluptate placeat excepturi
      blanditiis, ad numquam itaque.
      </span>
      </p>
      </div>
      </div>
      );
      };

      export default DesignTab;


      How do i automatically render the tab content just below the inner navigation of the homepage?



      Please see the layout here: https://codesandbox.io/s/lpjlqo7n4z










      share|improve this question
















      I'm very much new to react and i'm testing a tabs layout here: https://codesandbox.io/s/lpjlqo7n4z where i have 2 navigations, a Home and About which renders different contents. Inside the Homepage there's 2 more inner navigation links with different routes. Now my question is how to automatically render the component of the first tablink inside the homepage just below the inner navigation?



      Assuming everything is imported properly, here's my Index.js code:



        <Router>
      <div className="App">
      <header>
      <div className="logo">
      <figure>
      <p>Logo</p>
      </figure>
      </div>
      <nav>
      <ul>
      <li>
      <Link to="/" exact>Home</Link>
      </li>
      <li>
      <Link to="/about" exact>About</Link>
      </li>
      </ul>
      </nav>
      </header>
      <main>
      <Switch>
      <Route exact path="/" render={props => <Home {...props}/>} />
      <Route exact path="/about" render={props => <About {...props} />} />
      </Switch>
      </main>
      </div>
      </Router>


      Home component:



      const Home = (props) => {
      return (
      <div className="page">
      <div className="page-header">
      <h1>Homepage</h1>
      </div>
      <div className="page-nav">
      <ul>
      <li>
      <Link to={`/graphics-design`}>Graphics Design</Link>
      </li>
      <li>
      <Link to={`/development`}>Development</Link>
      </li>
      </ul>
      </div>
      <div className="page-content">
      <Switch>
      <Route path={`${match}/:topicId`} render={props => <DesignTab />} />
      <Route
      path={`${match}/:topicId`}
      render={props => <DevelopmentTab />}
      />
      </Switch>
      </div>
      </div>
      );
      };

      export default Home;


      And the first tab component:



      const DesignTab = props => {
      return (
      <div className="tab-content">
      <div className="tab-header">
      <h1>Design Works</h1>
      </div>
      <div className="tab-details">
      <p>
      <span>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Tenetur
      dignissimos corrupti eius vero maxime architecto similique odio
      maiores nihil accusantium iure error et voluptate placeat excepturi
      blanditiis, ad numquam itaque.
      </span>
      </p>
      </div>
      </div>
      );
      };

      export default DesignTab;


      How do i automatically render the tab content just below the inner navigation of the homepage?



      Please see the layout here: https://codesandbox.io/s/lpjlqo7n4z







      javascript reactjs tabs react-router-dom web-frontend






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 7:50







      Twirlman

















      asked Nov 27 '18 at 5:05









      TwirlmanTwirlman

      175111




      175111
























          1 Answer
          1






          active

          oldest

          votes


















          0














          There are issues with your program you need to address before starting your problem:



          The tab content just below the inner navigation of the homepage is impossible to render because the path is impossible to reach. You basically saying "Once im in the / home path, i want to match /gaming/:topicId paths" but this isn't possible because the only way to get to your Home component is to exactly match / in your index. So you can't exactly match / as well as /gaming/topicId.



          I would recommend moving these routes to your index.js file:



          <Switch>
          <Route path={`${match}/:topicId`} render={props => <DesignTab />} />
          <Route
          path={`${match}/:topicId`}
          render={props => <DevelopmentTab />}
          />
          </Switch>


          I don't understand what you want this Route to match to because the match object doesn't make sense in this context.



          When you make statements like render={props => <Home />, the props are not being sent to your Home component. You must tell the component you want to send it props via render={props => <Home {...props} /> instead.



          Also const Home = (props, { match }) is not the correct syntax and match will always be undefined here. Change to const Home = (props) then you can access match via props.match.



          I hope my advice helps :)






          share|improve this answer
























          • Hey thanks for your reply, yes i didn't included the props in the home and about component, that's typo but what i want to achieve is, the Home component should have 2 tab links that is already rendering the first tab component. If you check the link i provided: codesandbox.io/s/lpjlqo7n4z , i manage to render it all except for the tab component below the tablink is not showing.

            – Twirlman
            Nov 27 '18 at 6:55













          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%2f53493072%2fhow-to-render-the-first-tab-link-component-of-a-child-component-in-react-nested%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









          0














          There are issues with your program you need to address before starting your problem:



          The tab content just below the inner navigation of the homepage is impossible to render because the path is impossible to reach. You basically saying "Once im in the / home path, i want to match /gaming/:topicId paths" but this isn't possible because the only way to get to your Home component is to exactly match / in your index. So you can't exactly match / as well as /gaming/topicId.



          I would recommend moving these routes to your index.js file:



          <Switch>
          <Route path={`${match}/:topicId`} render={props => <DesignTab />} />
          <Route
          path={`${match}/:topicId`}
          render={props => <DevelopmentTab />}
          />
          </Switch>


          I don't understand what you want this Route to match to because the match object doesn't make sense in this context.



          When you make statements like render={props => <Home />, the props are not being sent to your Home component. You must tell the component you want to send it props via render={props => <Home {...props} /> instead.



          Also const Home = (props, { match }) is not the correct syntax and match will always be undefined here. Change to const Home = (props) then you can access match via props.match.



          I hope my advice helps :)






          share|improve this answer
























          • Hey thanks for your reply, yes i didn't included the props in the home and about component, that's typo but what i want to achieve is, the Home component should have 2 tab links that is already rendering the first tab component. If you check the link i provided: codesandbox.io/s/lpjlqo7n4z , i manage to render it all except for the tab component below the tablink is not showing.

            – Twirlman
            Nov 27 '18 at 6:55


















          0














          There are issues with your program you need to address before starting your problem:



          The tab content just below the inner navigation of the homepage is impossible to render because the path is impossible to reach. You basically saying "Once im in the / home path, i want to match /gaming/:topicId paths" but this isn't possible because the only way to get to your Home component is to exactly match / in your index. So you can't exactly match / as well as /gaming/topicId.



          I would recommend moving these routes to your index.js file:



          <Switch>
          <Route path={`${match}/:topicId`} render={props => <DesignTab />} />
          <Route
          path={`${match}/:topicId`}
          render={props => <DevelopmentTab />}
          />
          </Switch>


          I don't understand what you want this Route to match to because the match object doesn't make sense in this context.



          When you make statements like render={props => <Home />, the props are not being sent to your Home component. You must tell the component you want to send it props via render={props => <Home {...props} /> instead.



          Also const Home = (props, { match }) is not the correct syntax and match will always be undefined here. Change to const Home = (props) then you can access match via props.match.



          I hope my advice helps :)






          share|improve this answer
























          • Hey thanks for your reply, yes i didn't included the props in the home and about component, that's typo but what i want to achieve is, the Home component should have 2 tab links that is already rendering the first tab component. If you check the link i provided: codesandbox.io/s/lpjlqo7n4z , i manage to render it all except for the tab component below the tablink is not showing.

            – Twirlman
            Nov 27 '18 at 6:55
















          0












          0








          0







          There are issues with your program you need to address before starting your problem:



          The tab content just below the inner navigation of the homepage is impossible to render because the path is impossible to reach. You basically saying "Once im in the / home path, i want to match /gaming/:topicId paths" but this isn't possible because the only way to get to your Home component is to exactly match / in your index. So you can't exactly match / as well as /gaming/topicId.



          I would recommend moving these routes to your index.js file:



          <Switch>
          <Route path={`${match}/:topicId`} render={props => <DesignTab />} />
          <Route
          path={`${match}/:topicId`}
          render={props => <DevelopmentTab />}
          />
          </Switch>


          I don't understand what you want this Route to match to because the match object doesn't make sense in this context.



          When you make statements like render={props => <Home />, the props are not being sent to your Home component. You must tell the component you want to send it props via render={props => <Home {...props} /> instead.



          Also const Home = (props, { match }) is not the correct syntax and match will always be undefined here. Change to const Home = (props) then you can access match via props.match.



          I hope my advice helps :)






          share|improve this answer













          There are issues with your program you need to address before starting your problem:



          The tab content just below the inner navigation of the homepage is impossible to render because the path is impossible to reach. You basically saying "Once im in the / home path, i want to match /gaming/:topicId paths" but this isn't possible because the only way to get to your Home component is to exactly match / in your index. So you can't exactly match / as well as /gaming/topicId.



          I would recommend moving these routes to your index.js file:



          <Switch>
          <Route path={`${match}/:topicId`} render={props => <DesignTab />} />
          <Route
          path={`${match}/:topicId`}
          render={props => <DevelopmentTab />}
          />
          </Switch>


          I don't understand what you want this Route to match to because the match object doesn't make sense in this context.



          When you make statements like render={props => <Home />, the props are not being sent to your Home component. You must tell the component you want to send it props via render={props => <Home {...props} /> instead.



          Also const Home = (props, { match }) is not the correct syntax and match will always be undefined here. Change to const Home = (props) then you can access match via props.match.



          I hope my advice helps :)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 27 '18 at 6:39









          Shawn AndrewsShawn Andrews

          965617




          965617













          • Hey thanks for your reply, yes i didn't included the props in the home and about component, that's typo but what i want to achieve is, the Home component should have 2 tab links that is already rendering the first tab component. If you check the link i provided: codesandbox.io/s/lpjlqo7n4z , i manage to render it all except for the tab component below the tablink is not showing.

            – Twirlman
            Nov 27 '18 at 6:55





















          • Hey thanks for your reply, yes i didn't included the props in the home and about component, that's typo but what i want to achieve is, the Home component should have 2 tab links that is already rendering the first tab component. If you check the link i provided: codesandbox.io/s/lpjlqo7n4z , i manage to render it all except for the tab component below the tablink is not showing.

            – Twirlman
            Nov 27 '18 at 6:55



















          Hey thanks for your reply, yes i didn't included the props in the home and about component, that's typo but what i want to achieve is, the Home component should have 2 tab links that is already rendering the first tab component. If you check the link i provided: codesandbox.io/s/lpjlqo7n4z , i manage to render it all except for the tab component below the tablink is not showing.

          – Twirlman
          Nov 27 '18 at 6:55







          Hey thanks for your reply, yes i didn't included the props in the home and about component, that's typo but what i want to achieve is, the Home component should have 2 tab links that is already rendering the first tab component. If you check the link i provided: codesandbox.io/s/lpjlqo7n4z , i manage to render it all except for the tab component below the tablink is not showing.

          – Twirlman
          Nov 27 '18 at 6:55






















          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%2f53493072%2fhow-to-render-the-first-tab-link-component-of-a-child-component-in-react-nested%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

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)