Home  >  Article  >  Web Front-end  >  How to implement react-router-dom route jump

How to implement react-router-dom route jump

藏色散人
藏色散人Original
2022-12-28 14:02:412816browse

react-router-dom Implementation method of route jump: 1. Open the corresponding js file; 2. Manually operate the route jump through the useNavigate method; 3. Pass the route value through the state attribute and use useLocation Method gets parameters.

How to implement react-router-dom route jump

The operating environment of this tutorial: Windows 10 system, react18.0.0 version, Dell G3 computer.

react-router-dom How to implement routing jump?

React-Router-dom route jump

useNavigate

The useNavigate method can be manually operated for route jump and can switch between different pages

import { FunctionComponent } from "react";
import { useNavigate } from "react-router-dom";
import { Button } from "antd";
export const Login: FunctionComponent = () => {
  const navigate = useNavigate();
  const login = () => {
    navigate("/"); // 向 navigate 方法中传入要跳转的 path 路径
  };
  return (
    
Login
); };
attribute Description
replace Routing history, when When the value is true, no historical entries are created
state The route passes the value and the params parameter

Try

on CodeSandBox replace property default value is false, use {replace:true} so that we don't create another entry in the login page history stack. This means that when they reach the protected page and click the "Back" button, they will not be taken back to the login page. The

state attribute is passed in as a params parameter and will not be displayed in the url. Later, the page refresh parameters will be lost. On other pages, we use the useLocation method to obtain parameters

Recommended learning: "react video tutorial"

The above is the detailed content of How to implement react-router-dom route jump. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn