Lädt...


🔧 Using React Router v6 with Client-Side Routing


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

The differences between Router v5 (version 5) and v6 (version 6) are many. In this blog post, I'll be covering what client-side routing is, and how to use React Router to your benefit when working with this type of routing.

Client-side routing is a mechanism through which a user interacts with a webpage. It doesn't require any refreshes or reloads to print new data onto the page; it doesn't require any new GET requests to the server. All of the routing is done with JavaScript. This means: the client's side of the server does ALL of the routing, fetching, and rendering of the information on the DOM for the client to see. The URL should reflect what the user wants to see (ie twitter.com/home, twitter.com/user), which is more intuitive.

Additionally, it's faster overall, which the user always appreciates (not initially, as more must be loaded right off the bat)!

React Router is a client-side router (there are others, such as Hash Router, TanStack, or Hook Router. React Router allows certain components to be displayed based on which URL is called (ie twitter.com/home will render the Home component, twitter.com/user will render the User component).

When naming your endpoints, it is helpful to use the 7 RESTful Routes:

Image description
Image source

Do you want a user to be able to create something new on the page? View other items on the page? Those questions are helpful to ask yourself when naming your URL endpoints.

const routes = [
    {
        path: "/",
        element: <App />,
        errorElement: <ErrorPage />,
        children: [
            {
                path: "/home",
                element: <Home />
            },
            {
                path: "/about",
                element: <About />
            },
     //and so on

^ Your routes variable should be stored in routes.js, with each component imported at the top of the file. Each route is an object, and 'routes' is an array of these objects. "path: "/home"" means that your URL will read yoururl.com/Home. Makes sense! You can also render more than one element on a specific path as well.

This example renders App as the parent component, and Home and About as its nested children. App will act as a mechanism through which info is passed down to all its children; anything rendered in App will render in its children as well. The ErrorPage component will render to any of its children that run into an error, letting the user know that something went wrong.

Import your routes into your index.js file:

import React from "react";
import ReactDOM from "react-dom/client";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import routes from "./routes.js";
import "./index.css"

const router = createBrowserRouter(routes);

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<RouterProvider router={router} />);

This links your file with the routes listed to your index.js.
createBrowserRouter takes the routes array imported from routes.js and creates the router that your website will use for navigation; RouterProvider is the component that takes the prop of 'router' and defines it using the routes in the routes.js file. It will then be available throughout your entire application.

It took me some time to understand this, especially because there are so many resources for the syntax of v5 versus v6! Take your time - you've got this! xo

Additional note: To pass props from App to children components, you can use useOutletContext. Here are two additional resources about useOutletContext: 1 and 2. This allows you, the developer, to pass props from parent to child while simultaneously using Router (since you aren't explicitly rendering the children components in your parent component anymore, enabling the passing of props).

...

🔧 Understanding React Routing Using React Router


📈 39.39 Punkte
🔧 Programmierung

🔧 Routing in React with React Router Dom


📈 34.65 Punkte
🔧 Programmierung

🔧 React Router v6: A Comprehensive Guide to Page Routing in React


📈 34.65 Punkte
🔧 Programmierung

🔧 Mastering React Router: The Ultimate Guide to Navigation and Routing in React Apps!


📈 34.65 Punkte
🔧 Programmierung

🔧 Relative Vs Dynamic Routing in React – Different Routing Methods with Examples


📈 33.69 Punkte
🔧 Programmierung

🔧 React Router | React Router DOM


📈 28.57 Punkte
🔧 Programmierung

🔧 This Week In React #185: React Conf, React Query, refs, Next.js after, mini-react...


📈 28.2 Punkte
🔧 Programmierung

🔧 This Week In React #185: React Conf, React Query, refs, Next.js after, mini-react...


📈 28.2 Punkte
🔧 Programmierung

📰 How to Install Winscp FTP Client/SFTP Client/SCP Client (WinSCP Client) In Ubuntu


📈 27.76 Punkte
📰 Alle Kategorien

🔧 Junior level: Routing with React Router


📈 27.61 Punkte
🔧 Programmierung

🔧 Mid level: Routing with React Router


📈 27.61 Punkte
🔧 Programmierung

🔧 Senior level: Routing with React Router


📈 27.61 Punkte
🔧 Programmierung

🔧 Lead level: Routing with React Router


📈 27.61 Punkte
🔧 Programmierung

🔧 Architect level: Routing with React Router


📈 27.61 Punkte
🔧 Programmierung

🔧 Intern level: Routing with React Router


📈 27.61 Punkte
🔧 Programmierung

🔧 I used react-router but not for routing


📈 27.61 Punkte
🔧 Programmierung

🔧 How to create routing with React Router Dom from big scale projects?


📈 27.61 Punkte
🔧 Programmierung

🔧 Routing to Ruin: Navigating React Router Like a Villain


📈 27.61 Punkte
🔧 Programmierung

🔧 TanStack Router: Setup & Routing in React


📈 27.61 Punkte
🔧 Programmierung

🔧 Learn React Router v6: Building a Feature-Rich Blog Application with Advanced Routing Techniques


📈 27.61 Punkte
🔧 Programmierung

🔧 Understanding Next.js Page Routing vs App Routing and SSR Changes in Next.js 14


📈 26.64 Punkte
🔧 Programmierung

🔧 Kotlin Routing - routing everything


📈 26.64 Punkte
🔧 Programmierung

📰 Advanced Routing: Homematic IP bekommt intelligentes Routing


📈 26.64 Punkte
📰 IT Nachrichten

📰 Open Routing: Facebook gibt interne Plattform für Backbone-Routing frei


📈 26.64 Punkte
📰 IT Nachrichten

🔧 Handling React OTP Input Auth Web | React Native using react-otp-kit package


📈 25.88 Punkte
🔧 Programmierung

🔧 Handling React OTP Input Auth Web | React Native using react-otp-kit package


📈 25.88 Punkte
🔧 Programmierung

🔧 Using Node File Router for file-based routing in Node.js


📈 25.29 Punkte
🔧 Programmierung

🔧 Problem with Hash Router in react-router-dom V6.22.3


📈 21.52 Punkte
🔧 Programmierung

🔧 React Router has merged with Remix, should you use a different router?


📈 21.52 Punkte
🔧 Programmierung

🔧 Como usar o React Router Dom versão 6.26.0 em seu projeto com react js.


📈 21.33 Punkte
🔧 Programmierung

🔧 Navigating the React-TypeScript Landscape with React Router


📈 21.33 Punkte
🔧 Programmierung

🔧 React Router not working after "React app" published through GitHub actions


📈 21.33 Punkte
🔧 Programmierung

🔧 React Rooting (react-router-dom)


📈 21.33 Punkte
🔧 Programmierung

matomo