type
status
date
slug
summary
tags
category
icon
password

React Router

Frontend Router: A path corresponds to a component. When we access a path in the browser, the component corresponding to that path will be rendered on the page.
Creating a Routing Development Environment
CRA
  1. Create a project and install all dependencies:
    1. Install the latest React Router package:
      1. Start the project:
        Requirement: Create a routing system that can switch between a login page and an article page.
        • /login -> Login Page
        • /article -> Article Page
        Use createBrowserRouter to configure path and element (component/jsx), and RouterProvider to bind the routes.
        notion image
         

        React Router Abstract Routing Module

        Router Configuration in Actual Development
        1. Page components: login & article
        1. Import router components and configure path - component mapping
        1. Render RouterProvider in the application entry file (inject the router instance)
        notion image
         
        notion image
         
        notion image
         

        Route Navigation

        In a routing system, multiple routes need to navigate between each other, and during navigation, parameters might need to be passed for communication.
        1. Declarative Navigation Declarative navigation refers to describing where to navigate to using the <Link /> component in the template. For example, the left-side menu in a backend management system typically uses this approach.
          1. Syntax Explanation: By specifying the target route path in the to attribute of the component, the component will be rendered as an anchor link (<a>) supported by the browser. If parameters need to be passed, they can be concatenated directly in the string.
        notion image
         
        1. Programmatic Navigation
        Programmatic navigation refers to obtaining the navigation method through the useNavigate hook and then using the method to perform routing jumps in a command-like manner. For example, if you want to navigate after a login request is completed, this approach is more flexible.
        Syntax Explanation: Use the navigate method to pass the address path and achieve navigation.
        notion image
         

        Passing Parameters in Navigation

        Using searchParams for Parameters
        notion image
         
        notion image
         

        Route Navigation with Parameters

        notion image
         
        notion image
         
        notion image
         

        Nested Route Configuration

        When routes are embedded within other routes, this relationship is called nested routing. Routes nested within a top-level route are known as secondary routes.
        Implementation Steps:
        1. Use the children attribute to configure route nesting.
        1. Use the <Outlet /> component to specify the rendering location for secondary routes.
         
        Router:
        notion image
         
        top-level route:
        notion image
        Comments: <Outlet /> is the outlet for secondary routes.
         

        Default Secondary Route Configuration

        Scenario and Configuration:
        When accessing a top-level route, a default secondary route component can be rendered. To achieve this, simply omit the path for the secondary route and set the index attribute to true.
        notion image
         
        notion image
         

        404 Route Configuration

        Scenario: When the URL path entered in the browser does not match any route in the route configuration, a 404 fallback component can be rendered to improve user experience.
        Implementation Steps:
        1. Prepare a NotFound component.
        1. Add a route at the end of the route configuration array with a as the path to handle unmatched routes.
        notion image
         
        notion image
         

        Two Routing Modes

        Mainstream frameworks commonly use two routing modes: history mode and hash mode. In React Router, createBrowserRouter is used for history mode, and createHashRouter is used for hash mode.
        Choose based on whether backend support is needed.
        notion image
        notion image
        Switching Methods: Simply switch the API, and no other changes are needed.
        • Author:Blair Wang
        • URL://article/reactrouter
        • Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!
        YogaMate App About Me
        Loading...
        Blair Wang
        Blair Wang
        Love& Live
        Announcement
        🎉My Personal Website Published🎉
        -- Thanks for your Tour ---