📜  react-router - Javascript (1)

📅  最后修改于: 2023-12-03 15:19:45.300000             🧑  作者: Mango

React-Router - Javascript

React-Router is a powerful routing library for building single page applications (SPAs) with React. It allows you to easily manage navigation and rendering of your components, and create a seamless user experience.

Installation

To install React-Router, you can use either npm or yarn:

npm install react-router-dom

or

yarn add react-router-dom
Usage

To use React-Router in your application, you need to import the necessary components from the library, such as BrowserRouter, Route, and Link. Here is an example of how to set up a simple router in your app:

import React from 'react';
import { BrowserRouter, Route, Link } from 'react-router-dom';

const Home = () => <h2>Home</h2>;
const About = () => <h2>About</h2>;
const Contact = () => <h2>Contact</h2>;

const App = () => {
  return (
    <BrowserRouter>
      <div>
        <nav>
          <ul>
            <li>
              <Link to="/">Home</Link>
            </li>
            <li>
              <Link to="/about">About</Link>
            </li>
            <li>
              <Link to="/contact">Contact</Link>
            </li>
          </ul>
        </nav>

        <Route exact path="/" component={Home} />
        <Route path="/about" component={About} />
        <Route path="/contact" component={Contact} />
      </div>
    </BrowserRouter>
  );
};

export default App;

In this example, we define three components for the three pages of our app: Home, About, and Contact. Then, we use the BrowserRouter component to wrap our app and provide the routing functionality. Inside the BrowserRouter, we have a nav element that displays the links to the different pages of our app, and Route components that match the current URL path to the appropriate component to render.

Route Matching

The Route component has several props that can be used to control how it matches the current URL path to the component to render. Here are some of the most common props:

  • path: the URL path to match against the current URL. This can include dynamic segments that start with a colon, such as /users/:id.
  • exact: a boolean that indicates whether the path should be matched exactly or not. If true, the path must match the entire URL; if false (or not present), the path only needs to match the beginning of the URL.
  • strict: a boolean that indicates whether to match the path strictly or not. If true, the path must have a trailing slash to match URLs with a trailing slash; if false (or not present), the path will match with or without a trailing slash.
Linking

To create links between the different pages of your app, you can use the Link component provided by React-Router. Here is an example of how to create a link to the About page:

<Link to="/about">About</Link>

By default, clicking on a Link component will navigate to the linked page without reloading the entire app, thanks to React's ability to update the DOM efficiently.

Conclusion

React-Router is a powerful routing library that makes it easy to create and manage the navigation of your single page applications. By combining it with React's component-based architecture, you can create a seamless user experience that feels like a traditional multi-page app.