Introduction

we will walk through setting up a basic React application with routing using react-router-dom. We'll cover creating a simple folder structure, defining routes, and integrating them into the main application component.

Folder Structure

We'll start by organizing our files in a logical structure.

Setting up Routes in Routing/Main.js

First, we'll define the routes for our application.

import React from "react";
import { Routes, Route } from "react-router-dom";
import Home from "../pages/Home";
import Contactus from "../pages/Contactus";
function Main() {
    return (
        <Routes>
            <Route path="/" element={<Home />} />
            <Route path="/Contactus" element={<Contactus />} />
        </Routes>
    );
}
export default Main;

Explanation

Creating the Main Application Component in App.js

Next, we'll set up the main application component, including the header, footer, and routing components.

import React from 'react';
import Header from './pages/Header';
import Footer from './pages/Footer';
import Main from './Routing/Main';
import { BrowserRouter } from 'react-router-dom';
import './App.css';
function App() {
    return (
        <div className="App">
            <BrowserRouter>
                <Header />
                <Main />
                <Footer />
            </BrowserRouter>
        </div>
    );
}
export default App;

Explanation

Entry Point in index.js

Finally, we'll set up the entry point of the React application.

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);
reportWebVitals();

Explanation

Summary

In this article, we've set up a basic React application with routing. We organized our files, defined routes in Main.js, created the main application component in App.js, and set up the entry point in index.js. This structure provides a clean and scalable foundation for building a React application with routing capabilities.