Search code examples
reactjsreact-routerreact-link

How we can link sidebar menu item with app body?


I have a react.js project, I want to link sidebar menu as I drawn in image below. When any users click the side menu items, it can be linked theirs body. I was search so many materials on internet, they do easy, but I do not know how to apply this projects, any idea will be appreciated.

Screenshot:

enter image description here

App.js:

function App() {
 return (
<div className="App">
 
 <Router>
  <>
  <Header/>
  <AppBody>
    <Sidebar/>
  <Switch>
      <Route path="/" exact>
     <Chat/>
      </Route>
    </Switch>
      </AppBody>
    
    </>
   </Router>  
   </div>
   );}

Chat.js:

 function Chat() {
 return <ChatContainer>
    <h1>Chat screen will be here</h1>

    <ChatInput/>
   </ChatContainer>
   }
 export default Chat;

Sidebar.js:

function Sidebar() {
return (
   <SidebarContainer>
 <SidebarHeader>
  <SidebarInfo>
  <h2>React</h2>

  <h3>
      <FiberManualRecordIcon/>
      soft
  </h3>
  </SidebarInfo>
  <CreateIcon/>
  </SidebarHeader>
  <SidebarOption Icon={InsertCommentIcon} title="Threads"/>
  <SidebarOption Icon={InboxIcon} title="Mentions & reactions"/>
  <SidebarOption Icon={DraftsIcon} title="Saved items"/>
  <SidebarOption Icon={BookmarkBorderIcon} title="Channel browser"/>
  <SidebarOption Icon={PeopleAltIcon} title="People & user groups"/>
  <SidebarOption Icon={AppsIcon} title="Apps"/>
  <SidebarOption Icon={FileCopyIcon} title="File browser"/>
  <SidebarOption Icon={ExpandLessIcon} title="Show less"/>
  <hr/>
   <SidebarOption Icon={ExpandMoreIcon} title="Channels"/>
   <hr/>
   <SidebarOption Icon={AddIcon} addChannelOption title="Add Channels"/>

   </SidebarContainer>
   )}
   export default Sidebar;

Solution

  • You need to wrap the SideBar Option with the Link Component. And have the MainContent to be rendered via Route . So that when you click on the item in the SideBar you will update the route and the Route component renders its component if the path matches .

    SideBar Example