title: React Router with - Ant Design Sider: how to populate content section with components for relevant menu item
body: <p>I'm trying to use AntD menu sider like a tab panel.</p>
  
  <p>I want to put components inside the content so that the content panel renders the related component when a menu item is clicked.</p>
  
  <p>How do I get this structure to take components as the content for each menu item?</p>
  
  <pre><code>import React from 'react';
  import { compose } from 'recompose';
  import { Divider, Layout, Card, Tabs, Typography, Menu, Breadcrumb, Icon } from 'antd';
  import { PasswordForgetForm } from '../Auth/Password/Forgot';
  
  
  const { Title } = Typography
  const { TabPane } = Tabs;
  const { Header, Content, Footer, Sider } = Layout;
  const { SubMenu } = Menu;
  
  
  class Dashboard extends React.Component {
    state = {
      collapsed: false,
    };
  
    onCollapse = collapsed =&gt; {
      console.log(collapsed);
      this.setState({ collapsed });
    };
  
    render() {
      return (
        &lt;Layout style={{ minHeight: '100vh' }}&gt;
          &lt;Sider collapsible collapsed={this.state.collapsed} onCollapse={this.onCollapse}&gt;
            &lt;div  /&gt;
            &lt;Menu theme="light" defaultSelectedKeys={['1']} mode="inline" &gt;
  
              &lt;Menu.Item key="1"&gt;
                &lt;Icon type="fire" /&gt;
                &lt;span&gt;Next item&lt;/span&gt;
                &lt;PasswordForgetForm /&gt;
              &lt;/Menu.Item&gt;  
              &lt;Menu.Item key="2"&gt;
                &lt;Icon type="fire" /&gt;
                &lt;span&gt;Next item&lt;/span&gt;
                &lt;Another component to render in the content div /&gt;
              &lt;/Menu.Item&gt;
  
            &lt;/Menu&gt;
          &lt;/Sider&gt;
          &lt;Layout&gt;
            &lt;Header style={{ background: '#fff', padding: 0 }} /&gt;
            &lt;Content style={{ margin: '0 16px', background: '#fff' }}&gt;
  
              &lt;div style={{ padding: 24, background: '#fff', minHeight: 360 }}&gt;
              RENDER RELEVANT COMPONENT HERE BASED ON MENU ITEM SELECTED
              &lt;/div&gt;
            &lt;/Content&gt;
            &lt;/Layout&gt;
        &lt;/Layout&gt;
      );
    }
  }
  
  export default Dashboard;
  </code></pre>
  
  <p>When I try to render this, no errors are thrown, but the content div does not update with the PasswordForgetForm that I specified as the content for the menu item.</p>
  
  <p>I tried Chris' suggestion below - which works fine to render the component for each of the different menu item content divs in the layout segment, however - the downside of this approach is that with every page refresh, the path goes to the content component for that particular menu item, instead of the test page which has the menu on it -- and the content component with that relevant content. If this approach is endorsed as sensible, is there a way to keep the page refresh on the original page, rather than trying to go to a subset menu item url?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I found this <a href="https://reacttraining.com/react-router/web/api/Route/render-func" rel="noreferrer">literature</a>. I think this might be what I need to know, but the language is too technical for me to understand. Can anyone help with a plain english version of this subject matter?</p>
  
  <p>I also found <a href="https://popmotion.io/pose/learn/route-transitions-react-router/" rel="noreferrer">this tutorial</a> which uses Pose to help with rendering. While this structure is what I'm trying to achieve, it looks like Pose has been deprecated. Does anyone know if its necessary to go beyond react-router to get this outcome, or is there a solution within react that I can look to implement?</p>
  
  <p>This <a href="https://reacttraining.com/react-router/web/example/sidebar" rel="noreferrer">example</a> is similar to what I want, but I can't find anything that defines sidebar (which seems to be an argument that is necessary to make this work). </p>
  
  <p>I have also seen <a href="https://stackoverflow.com/questions/33062830/using-react-router-with-a-layout-page-or-multiple-components-per-page">this post</a>. While some of the answers are a copy and paste of the docs, I wonder if the answer by Adam Gering is closer to what I need. I'm trying to keep the Sider menu on the /dash route at all times. That url should not change - regardless of which menu item in the sider the user clicks BUT the content div in the /dash component should be updated to render the component at the route path I've specified. </p>
  
  <p><strong>APPLYING CHRIS' SUGGESTION</strong></p>
  
  <p>Chris has kindly offered a suggestion below. I tried it, but the circumstance in which it does not perform as desired is when the menu item is clicked (and the relevant component correctly loads in the content div, but if I refresh the page, the refresh tries to load a page with a url that is for the component that is supposed to be inside the content div on the dashboard page.</p>
  
  <pre><code>import React from 'react';
  import {
      BrowserRouter as Router,
      Route,
      Link,
      Switch,
  
    } from 'react-router-dom';
  import * as ROUTES from '../../constants/Routes';
  import { compose } from 'recompose';
  import { Divider, Layout, Card, Tabs, Typography, Menu, Breadcrumb, Icon } from 'antd';
  import { withFirebase } from '../Firebase/Index';
  import { AuthUserContext, withAuthorization, withEmailVerification } from '../Session/Index';
  
  // import UserName from '../Users/UserName';
  import Account from '../Account/Index';
  import Test from '../Test/Index';
  
  
  
  const { Title } = Typography
  const { TabPane } = Tabs;
  const { Header, Content, Footer, Sider } = Layout;
  const { SubMenu } = Menu;
  
  
  class Dashboard extends React.Component {
    state = {
      collapsed: false,
    };
  
    onCollapse = collapsed =&gt; {
      console.log(collapsed);
      this.setState({ collapsed });
    };
  
    render() {
      const {  loading } = this.state;
      // const dbUser = this.props.firebase.app.snapshot.data();
      // const user = Firebase.auth().currentUser;
      return (
      &lt;AuthUserContext.Consumer&gt;
        {authUser =&gt; (  
  
          &lt;div&gt;    
           {authUser.email}
            &lt;Router&gt;  
              &lt;Layout style={{ minHeight: '100vh' }}&gt;
                &lt;Sider collapsible collapsed={this.state.collapsed} onCollapse={this.onCollapse}&gt;
                  &lt;div  /&gt;
                  &lt;Menu theme="light" defaultSelectedKeys={['1']} mode="inline" &gt;
                    &lt;SubMenu
                      key="sub1"
                      title={
                        &lt;span&gt;
                        &lt;Icon type="user" /&gt;
                        &lt;span&gt;Profile&lt;/span&gt;
                        &lt;/span&gt;
                      }
                    &gt;
  
                        &lt;Menu.Item key="2"&gt;&lt;Link to={ROUTES.ACCOUNT}&gt;Account Settings&lt;/Link&gt;&lt;/Menu.Item&gt;
  
  
                        &lt;Menu.Item key="3"&gt;&lt;Link to={ROUTES.TEST}&gt;2nd content component&lt;/Link&gt;&lt;/Menu.Item&gt;
  &lt;/SubMenu&gt;
  
                  &lt;/Menu&gt;
                &lt;/Sider&gt;
                &lt;Layout&gt;
  
                  &lt;Header&gt;                &lt;/Header&gt;      
                  &lt;Content style={{ margin: '0 16px', background: '#fff' }}&gt;
  
                    &lt;div style={{ padding: 24, background: '#fff', minHeight: 360 }}&gt;
                        &lt;Switch&gt;
                            &lt;Route path={ROUTES.ACCOUNT}&gt;
                            &lt;Account /&gt;
                            &lt;/Route&gt;
                            &lt;Route path={ROUTES.TEST}&gt;
                            &lt; Test /&gt;
                            &lt;/Route&gt;
  
                        &lt;/Switch&gt;    
                    &lt;/div&gt;
                  &lt;/Content&gt;
                  &lt;Footer style={{ textAlign: 'center' }}&gt;
  
  
                   test footer
                  &lt;/Footer&gt;
                &lt;/Layout&gt;
              &lt;/Layout&gt;
            &lt;/Router&gt;  
          &lt;/div&gt;
        )}
      &lt;/AuthUserContext.Consumer&gt;  
      );
    }
  }
  
  export default Dashboard;
  </code></pre>
  
  <p>In my routes file, I have:</p>
  
  <pre><code>export const ACCOUNT = '/account';
  </code></pre>
  
  <p>I've also found <a href="https://scotch.io/courses/using-react-router-4/using-switch?autoplay=true" rel="noreferrer">this tutorial</a> - which uses the same approach outlined above - and doesn't get redirected in the same way that my code does on page refresh. The tutorial uses Route instead of BrowserRouter - but otherwise I can't see any differences.</p>
  
  <p><strong>NEXT ATTEMPT</strong></p>
  
  <p>I saw this <a href="http://%3CRoute%20path=%7B%60$%7Bthis.props.match.path%7D/quote%60%7D%20component=%7BQuote%7D/%3E" rel="noreferrer">post</a> (thanks Matt). I have tried to follow the suggestions in that post.</p>
  
  <p>I removed the outer Router wrapper from the Dashboard page (there is a Router around App.js, which is where the route to Dashboard is setup).</p>
  
  <p>Then, in my Dashboard, I changed the Content div to:</p>
  
  <p></p>
  
  <p>I added:</p>
  
  <pre><code>let match = useRouteMatch();
  </code></pre>
  
  <p>to the render method inside my Dashboard component (as shown <a href="https://reacttraining.com/react-router/web/guides/quick-start" rel="noreferrer">here</a>).</p>
  
  <p>I added useRouteMatch to my import statement from react-router-dom.</p>
  
  <p>This produces an error that says:</p>
  
  <blockquote>
    <p>Error: Invalid hook call. Hooks can only be called inside of the body
    of a function component. This could happen for one of the following
    reasons:
    1. You might have mismatching versions of React and the renderer (such as React DOM)
    2. You might be breaking the Rules of Hooks</p>
  </blockquote>
  
  <p>There is more message in the error above but stack overflow won't allow me to post it because it has a short link</p>
  
  <p>I don't know what I've done wrong in these steps, but if I comment out the let statement, the page loads.</p>
  
  <p>When I go to /Dashboard and click "account", Im expecting the account component to render inside the content div I have in the layout on the Dashboard page. Instead, it redirects directly to a page at localhost3000/account (for which there is not page reference - it's just a component to render inside the Dashboard page). </p>
  
  <p>So this is worse than the problem I started with - because at least at the start, the redirect only happened on page refresh. Now it happens immediately.</p>
  
  <p>I found <a href="https://github.com/SandipNirmal/react-router-examples/tree/master/src" rel="noreferrer">this repo</a> that has examples of each kind of route. I can't see what it is doing that I am not. </p>
  
  <p>This <a href="https://forums.meteor.com/t/react-router-how-to-render-component-within-a-layout-vs-stand-alone/45767/2" rel="noreferrer">post</a> seems to have had the same problem as me, and resolved it using the same approach as I have tried. It's a post from 2018 so may the passage of time makes the approach outdated - but I can't see any difference between what that post solution implements and my original attempt.</p>
  
  <p><strong>NEXT ATTEMPT</strong></p>
  
  <p>I had thought I had found an approach that works in Lyubomir's answer on <a href="https://stackoverflow.com/questions/41474134/nested-routes-with-react-router-v4-v5?rq=1">this post</a>. </p>
  
  <p>I have:</p>
  
  <pre><code>&lt;Menu.Item key="2"&gt;
                        &lt;Link to=
  </code></pre>
  
  <p>{<code>${this.props.match.url}/account</code>}>
                      Account Settings
                    
                  </p>
  
  <p>Then in the div on the dash component where I want to display this component I have:</p>
  
  <pre><code>&lt;div&gt;
                        &lt;Switch&gt;
                            &lt;Route exact path={`${this.props.match.url}/:account`} component={Account} /&gt;
  </code></pre>
  
  <p>This works. On page refresh, I can keep things working as they should.</p>
  
  <p>HOWEVER, when I add a second menu item with:</p>
  
  <p></p>
  
  <pre><code>&lt;Menu.Item key="16"&gt;
                      &lt;Link to={`${this.props.match.url}/learn`}&gt;
                        &lt;Icon type="solution" /&gt;
                        &lt;span&gt;Lern&lt;/span&gt;
                      &lt;/Link&gt;  
                    &lt;/Menu.Item&gt;
  </code></pre>
  
  <p>Instead of rendering the Learning component when menu item for /learn is clicked (and the url bar changes to learn), the Account component is rendered. If i delete the account display from the switch statement, then I can have the correct Learning component displayed.                    </p>
  
  <p>With this attempt, I have the menu items working to match a Dashboard url extension (ie localhost/dash/account or localhost/dash/learn) for <strong>ONE</strong> menu item only. If I add a second menu item, the only way I can correctly render the 2nd component, is by deleting the first menu item. I am using switch with exact paths.</p>
  
  <p>I want this solution to work with multiple menu items.</p>
  
  <p>I have tried alternating path for url (eg:</p>
  
  <pre><code>&lt;Link to={`${this.props.match.url}/learn`}&gt;
  </code></pre>
  
  <p>is the same as:</p>
  
  <pre><code>&lt;Link to={`${this.props.match.path}/learn`}&gt;
  </code></pre>
  
  <p>I have read the explanation in <a href="https://css-tricks.com/react-router-4/" rel="noreferrer">this blog</a> and while I don't entirely understand these options. I have <a href="https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/Route.md#component" rel="noreferrer">read this</a>. It suggests the match statement is now a legacy method for rendering components (now that hooks are available). I can't find any training materials that show how to use hooks to achieve the expected outcome.</p>