Day 5: React Layouts, Outlets, and Links

On the fifth day of learning react, we grabbed on concepts like layouts, outlets, and links in React. They are essential concepts for structuring and navigating through a React application.

Layouts

These provide a consistent structure and design across multiple pages or views, enhancing user experience and maintaining visual coherence throughout the application. Layouts structure or design our application. They often include common components such as headers, footers, navigation menus, and sidebars that remain consistent across multiple pages or views of your application.

//MyRoute.js
import { BrowserRouter as Router, Routes, Route} from "react-router-dom"

import HomePage from "./pages/HomePage"
import AboutPage from "./pages/AboutPage"
import MainLayout from "./layouts/MainLayout"
import { ServicePage } from "./pages/ServicesPage"
import { ContactPage } from "./pages/ContactPage"



const MyRoutes = () => {
    return(
        <Router>
            <Routes>
                <Route path="/" element={<MainLayout/>} >
                    <Route path="/" element={<HomePage/>} />
                    <Route path="/about" element={<AboutPage/>}/>
                    <Route path="/services" element={<ServicePage/>}/>
                    <Route path="/contact" element={<ContactPage/>}/>
                </Route>                
            </Routes>
        </Router>
    )
}

export default MyRoutes        

Outlets

These offer a dynamic way to inject content into specific areas of the layout based on the current route or view, enabling flexible and modular application architecture. They provide a placeholder or target for dynamically injecting content based on the current route or view.

//MainLayout.js
import { Outlet } from "react-router-dom"
import { Footer } from "../components/footer/Footer"
import { Navbar } from "../components/navbar/Navbar"
const MainLayout = () =>{
    
    return(
        <>
            <Navbar/>
            <Outlet/>
            <Footer/>
        
        </>
    )
}
export default MainLayout        

Links

They facilitate seamless navigation within the application without the need for full page reloads, resulting in faster and smoother user interactions, thereby enhancing overall usability and user satisfaction. React provides the <Link> component from the react-router-dom package for handling client-side routing without a page reload.

import { Link } from 'react-router-dom';
import './Navbar.css'
export function Navbar() {
  return (
    <>
    <nav className="navbar">
      <div className="brand">Logo</div>
      <ul className="items">
        <li>
          <Link to="/" className='nav-links'>Home</Link>
        </li>
        <li>
          <Link to="/about" className='nav-links'>About</Link>
        </li>
        <li>
          <Link to="/services" className='nav-links'>Services</Link>
        </li>
        <li>
          <Link to="/contact" className='nav-links'>Contact</Link>
        </li>
         
      </ul>
    </nav>7
    
    </>
  );
}        

I am extending my gratitude to Bikash Karki for his insightful lectures.

要查看或添加评论,请登录

Aastha Sharma的更多文章

  • ExpressJS

    ExpressJS

    What is ExpressJS? Express.js is a Node.

  • Day 13: Forms in React (Formik and Yup)

    Day 13: Forms in React (Formik and Yup)

    On the thirteenth day, I learned about forms in React, the Fragment tag, and react-icons in the react application…

  • Day 12: localStorage

    Day 12: localStorage

    It can be infuriating to close a webpage while filling out a form accidentally. We lose all the data already filled and…

  • Day 11: Layout, Offset, and the useParams Hook

    Day 11: Layout, Offset, and the useParams Hook

    Layout and Offset In React, "offset" and "limit" are commonly used when dealing with paginated data. These terms are…

  • Day 10: Using API data and Pagination in React

    Day 10: Using API data and Pagination in React

    APIs in React APIs (Application Programming Interfaces), in React, are used to fetch data from external sources, such…

  • Day 9: Environment Variables and Toastify in React

    Day 9: Environment Variables and Toastify in React

    What are Environment Variables? Environment variables in React serve as dynamic configurations for our application…

  • Day 8:React Hooks (Part 2)

    Day 8:React Hooks (Part 2)

    After a thorough introduction to React Hooks and the useState hook on day 7, we learned about the useEffect hook in…

  • Day 7: React Hooks

    Day 7: React Hooks

    On the seventh day of learning React, we learned about React hooks, their use, and their applications. React Components…

  • Day 6: Dynamic React Components using Props

    Day 6: Dynamic React Components using Props

    React utilizes a component-based architecture that enables developers to create reusable and modular UI elements. A…

  • Day 4: React-Router-DOM and BootStrap

    Day 4: React-Router-DOM and BootStrap

    On the fourth day of learning React, we learned about installing Bootstrap and React router DOM, all thanks to our…

社区洞察