Ways to implement various types of navigation bar elements across multiple webpages

I have developed two different navbar components for separate subsystems within our project.

App.js

function App() {
  return (
    <>
      <Router>
        <Routes>
          <Route path="/" element={<CirclePage />} />
          <Route
            path = "*"
            element={
              <>
                <Navbar/>
                <Routes>
                  <Route path="/homepage" element={<Homepage />} />
                  <Route path="/events" element={<Events />} />
                  <Route path="/clubs" element={<Clubs />} />
                  <Route path="/notifications" element={<Notifications />} />
                  <Route path="/profile" element={<Profile />} />
                  <Route path="/contact" element={<Contact/>}/>
                </Routes>
              </>
            }
          />

          <Route 
            path = "*"
            element={
              <>
                <NavbarClub/>
                <Routes>
                  <Route path="/homepage-club" element={<HomepageClubs />} />
                  <Route path="/events-club" element={<EventManagement />} />
                  <Route path="/contact-clubs" element={<ContactClubs />} />
                  <Route path="/notifications-club" element={<NotificationsClub />} />
                  <Route path="/profile-club" element={<ClubProfile />} />
              </Routes>
              </>
            } 
            />

        </Routes>
      </Router>
    </>
  );
}

export default App;

The Navbar is set to appear on all screens except the main screen CirclePage. It should be visible on pages like homepage, events, clubs, notifications, profile and contact, while NavbarClub should only appear on pages with -club suffix. How can I achieve this setup?

Answer №1

Develop unique layout components for each section in order to display the appropriate navbar and an Outlet for nested Route components.

For instance:

import { Outlet } from 'react-router-dom';

const NavbarLayout = () => (
  <>
    <Navbar />
    <Outlet />
  </>
);

const NavbarClubLayout = () => (
  <>
    <NavbarClub />
    <Outlet />
  </>
);

Application

function App() {
  return (
    <Router>
      <Routes>
        <Route path="/" element={<CirclePage />} />
        <Route path="*" element={<NavbarLayout />}>
          <Route path="homepage" element={<Homepage />} />
          <Route path="events" element={<Events />} />
          <Route path="clubs" element={<Clubs />} />
          <Route path="notifications" element={<Notifications />} />
          <Route path="profile" element={<Profile />} />
          <Route path="contact" element={<Contact />} />
        </Route>

        <Route path="*" element={<NavbarClubLayout />}>
          <Route path="homepage-club" element={<HomepageClubs />} />
          <Route path="events-club" element={<EventManagement />} />
          <Route path="contact-clubs" element={<ContactClubs />} />
          <Route path="notifications-club" element={<NotificationsClub />} />
          <Route path="profile-club" element={<ClubProfile />} />
        </Route>
      </Routes>
    </Router>
  );
}

https://codesandbox.io/s/how-to-use-different-kinds-of-navbar-components-on-different-pages-glq85?fontsize=14&hidenavigation=1&module=%2Fsrc%2FApp.js&theme=dark

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Nextjs Image was preloaded using link preload, but it remained unused after a short period following the window's load event

I've encountered an issue while working with Next.js, a React-based framework. I am attempting to display the logo.png image using the Image component provided by Next.js. My image is stored in this folder: public/img Here is the code I'm using ...

What is the best strategy for managing pagination when dealing with a large number of pages?

Up until now, I have understood that pagination only links different pages together. This works fine when dealing with a limited number of pages or posts to display. However, what if I have 30 or more pages to paginate? Wouldn't it be impractical to c ...

Obtain JSON information from a Javascript response using Puppeteer:

While developing a test with Puppeteer and Node, I encountered the need to extract an access token from a response after logging in. Here is the current code snippet: //Click Login Button const loginButton = await page.$(".click-button.dark.ng-star-i ...

The tab key seems to be malfunctioning when triggered during a jquery change event

I encountered an unusual issue with my jQuery events, and I'm not entirely sure if it's a problem related to jQuery. In hopes that some jQuery experts can shed some light on this. The code snippet below was included in my HTML page to automatica ...

Exploring the testing capabilities of Angular JS in conjunction with third-party libraries such as

When testing an angular controller that utilizes an external library like Google Analytics event tracking, how can you approach it? For instance: $scope.showVolumn = function() { ga('send', { 'hitType': 'event', ...

When React object state remains unchanged, the page does not update automatically

i have a state object with checkboxes: const [checkboxarray_final, setCheckboxarray_final] = useState({ 2: ",4,,5,", 9: ",1,", }); i'm working on enabling check/uncheck functionality for multiple checkboxes: these are ...

Choose multiple table cells as a unit

In this scenario, I am looking to group select multiple cells within a table. The desired functionality includes being able to click on a target cell to select it, as well as the ability to achieve selection by clicking and dragging to cover multiple cells ...

TestingCompilerFactory is not available as a provider

Currently troubleshooting my test file to identify the issue that is hindering a successful test run: import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { Component, Directive, Input, OnInit } from '@angula ...

Struggling to position elements at the edges in Bootstrap

My goal is to move the elements to the corners of the screen. Despite trying the code below, it doesn't seem to be working as expected. <!-- Page Footer --> <footer> <div class="navbar mt-3 py-3 bg-dark"> <div class=&q ...

Creating Sleek Tables - comparing the benefits of using table-layout:fixed versus setting individual td-width to

Several weeks ago I posted a query (Table overflows parent div when td content is too wide) related to table design. The response I received seemed to resolve the issue perfectly. Now, as I attempted to redesign the table yesterday, I encountered a challen ...

Exploring the Power of GraphQL Args in Mutation Operations

Currently, I am in the process of developing a blog service using express and apollo-express in conjunction with mongodb (mongoose). While implementing mutation queries, I have encountered difficulties in accessing the arguments of a mutation query. I am ...

Option list malfunctioning in Safari, Mozilla, as well as Chrome browsers

I encountered some issues while working on the front-end of my web app. Here are a few problems: I need to truncate text if it's too long, so I use the following CSS: .suggestion-box-text { white-space: nowrap; overflow: hidden; text-overfl ...

Using Jquery to toggle the visibility of divs based on radio button selections

I am struggling to hide the divs with the radio buttons until their title is clicked on. However, I am facing issues as they are not showing up when their title is clicked on. Any assistance would be greatly appreciated. SPIKE <!DOCTYPE html> &l ...

Interested in learning how to filter nested tables?

After successfully integrating a filter code snippet, I encountered an issue with the filter not recognizing data tables that can only be inserted as nested tables. Due to my limited knowledge of JavaScript/CSS, I'm unsure if this problem can be resol ...

"Uncaught ReferenceError: $ is not defined - $function()" error in JavaScript/jQuery

When attempting to execute a JavaScript/jQuery function, an error is encountered when using Firebug: $ is not defined $(function()". The issue arises from the placement of the JavaScript code within a file named core.js that is referenced by index.php. W ...

React - 'classProperties' is not currently activated in this setting

Recently, I incorporated Truncate-react into my project. Subsequently, React prompted me to install @babel/plugin-proposal-class-properties, which I promptly added to the package.json file as follows: "babel": { "presets": ...

Vue.js - Implementing multiple values (array) for a component through a property

I am currently working with vue.js components that receive their data from external sources. For example: <vue-button icon="fa-arrow-right" text="mytext"></vue-button> Initially, this setup is effective. However, I encountered a challenge wh ...

Can someone explain the significance of this in an HTML form?

Can anyone explain the following code snippet found in a web form? I found this button code on a webpage, but I'm having trouble understanding it. Could someone provide some clarity? <input type="submit" name="ctl00$ContentPlaceHolder1$Button8" v ...

A method of binding data from an array of objects in Vue using v-bind

I am tasked with rendering a board that is 20x15 and placing creatures on it. The information on where to place the creatures is stored in this.creaturesOnBoard within the gameEngine. My plan is to take X and y coordinates, then check if a creature exists ...

Is it possible to integrate my Python code into a React Native application?

For my semester project, I successfully completed a Software Architecture Recovery Desktop App using Python and PyQt5. Now, I have been tasked with creating a web app and a mobile app for the same project. I plan to utilize Python's Flask framewo ...