DEV Community

_Khojiakbar_
_Khojiakbar_

Posted on

2

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.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay