Logo

dev-resources.site

for different kinds of informations.

Header in React

Published at
8/14/2024
Categories
header
react
Author
__khojiakbar__
Categories
2 categories in total
header
open
react
open
Author
14 person written this
__khojiakbar__
open
Header in React
import { About, Location, Careers, Error } from "../views"; // Importing components from the 'views' directory
import { Routes, Route } from 'react-router-dom'; // Importing React Router DOM components
import Header from './Header'; // Importing the Header component
import Footer from './Footer'; // Importing the Footer component
import MainSection from './MainSection'; // Importing the MainSection component

// Defining the routes and their corresponding components
export const routerLinks = [
    {
        id: 1,
        path: '/about',
        component: <About/>,
        title: "About"
    },
    {
        id: 2,
        path: '/location',
        component: <Location/>,
        title: "Location"
    },
    {
        id: 3,
        path: '/careers',
        component: <Careers/>,
        title: "Careers"
    },
    {
        id: 4,
        path: '*', // This acts as a catch-all for undefined routes
        component: <Error/>
    }
];

// Main App component
const App = () => {
  return (
    <>
      <MainSection>
        <Header/>  {/* Header component */}
        <Routes>  {/* React Router DOM Routes component */}
          {
            routerLinks.length && 
            routerLinks.map((item) => 
            <Route key={item.id} path={item.path} element={item.component}/>)
          }
        </Routes>
        <Footer/>  {/* Footer component */}
      </MainSection>
    </>
  );
};

export default App;
Enter fullscreen mode Exit fullscreen mode

Explanation:

Import Statements: The code imports necessary components like About, Location, Careers, Error, and other UI components like Header, Footer, and MainSection.

routerLinks Array: This array contains objects, each representing a route in the application. It includes:

id: A unique identifier for each route.
path: The URL path that triggers the route.
component: The component that will be rendered when the route is matched.
title: A title for the route (optional).

App Component:

The App component wraps the application content inside the MainSection.
It includes a Header at the top, followed by the Routes component, which renders the routes based on the routerLinks array.
Each route is mapped to a Route component using the map() function.
The Footer is placed at the bottom of the page.
This setup allows you to easily manage and add new routes in the routerLinks array.

header Article's
30 articles in total
Favicon
Header in React
Favicon
Header in React
Favicon
Empty Excel Folders for Python programming
Favicon
Using cURL Custom Headers: A Simple Guide
Favicon
Comment ajouter un en-tΓͺte et un pied de page Γ  Word avec Python
Favicon
Solve the error "header files not found. no such file or directory".
Favicon
Understanding Word Header and Footer: Professional Advice for java developers
Favicon
How to Create a Resume Header [7+ Examples]
Favicon
HTML table sticky header with borders
Favicon
Java-How to Add or Remove Header and Footer in Word documents
Favicon
Remember to set Cache-Control headers on your secured pages
Favicon
CSS Shopping cart Icon with number of items.
Favicon
How to make a dynamic Twitter Header with Switchboard Canvas
Favicon
How to create header/footer (using HTML) in each page
Favicon
backdrop-filter: blur / header sticky
Favicon
Manage License Headers with Ease
Favicon
HTML tags | header
Favicon
[Java] Add Different Headers/Footers for Odd and Even Pages in Word
Favicon
SpringBoot RabbitMq Headers Exchange
Favicon
Insert Header and Footer to Word using Java
Favicon
Answer: How to specify column names while reading an Excel file using Pandas?
Favicon
Make a Material table header fix
Favicon
Sticky navbar from scratch using react
Favicon
So hard to make table header sticky
Favicon
How to remove IIS server information from the response header?
Favicon
Global Header in Swagger-Ui Spring-Boot
Favicon
HTTP Headers Explained
Favicon
Java add image and text header/footer to Excel
Favicon
Add Header and Footer to an Existing PDF Document in Java
Favicon
Security Headers to use on your webserver

Featured ones: