📜  React-router (1)

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

React-Router

React-Router is a powerful routing library for React applications. It allows developers to manage the URLs of their applications and render different components based on those URLs. React-Router is a standalone library, which means it can be used with or without Redux.

Installation

To use React-Router in your application, you must first install it using npm:

npm install react-router-dom
How it Works

React-Router is a declarative library that allows developers to define routes and map them to components. It uses the React context to manage the state of the router and provides a set of higher-order components that can be used to create routes.

Route Components

The Route component is the core building block of React-Router. It can be used to declare a route in your application and define which component should be rendered when that route is accessed.

import { Route } from 'react-router-dom';

function App() {
  return (
    <div>
      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
      <Route path="/services" component={Services} />
    </div>
  );
}

In the example above, we're mapping three routes to three different components. The exact prop for the home route ensures that it only matches if the path is exactly /. The other two routes will match any path that starts with /about and /services.

If none of the routes match, React-Router will fallback to a special NotFound component.

Link Components

The Link component is used to create a hyperlink that can be used to navigate to another route in your application.

import { Link } from 'react-router-dom';

function Navigation() {
  return (
    <nav>
      <ul>
        <li>
          <Link to="/">Home</Link>
        </li>
        <li>
          <Link to="/about">About</Link>
        </li>
        <li>
          <Link to="/services">Services</Link>
        </li>
      </ul>
    </nav>
  );
}

In the example above, we're creating a simple navigation menu. When a user clicks on one of the links, React-Router will update the URL and render the appropriate component.

Programmatic Navigation

React-Router provides a set of functions that can be used to navigate programmatically. These functions can be called from within your components to redirect the user to another route.

import { useHistory } from 'react-router-dom';

function Login() {
  const history = useHistory();

  function handleLogin() {
    // Do some login logic
    history.push('/dashboard');
  }

  return (
    <div>
      <button onClick={handleLogin}>Login</button>
    </div>
  );
}

In the example above, we're using the useHistory hook to get access to the history object. We then use the push method to redirect the user to the dashboard route after they've successfully logged in.

Conclusion

React-Router is an essential library for building complex single-page applications. It provides a set of powerful components and functions that allow developers to manage the URLs of their applications and render different components based on those URLs. With React-Router, you can build robust and scalable applications that are easy to navigate and manage.