
反应路由问题。我安装了,但它不起作用--如何修复?

本教程将阅读页面。但我的电脑不会舔这个
发布于 2022-11-13 19:37:22
您使用的代码是react-router-dom@5代码(即Redirect__、Switch__等),但错误表明您已经安装了最新的主要版本react-router-dom@6。
要么用下面的CLI命令恢复回v5:
npm uninstall --save react-router-dom
npm install --save react-router-dom@5或者继续安装RRDv6,并按照v5迁移指南更新/替换RRDv6中从v5替换的组件和钩子。
发布于 2022-11-13 19:17:26
useHistory在react路由器dom v6中被删除。请改用useNavigate。下面是useNavigate的示例代码。
在这个例子中,我使用上下文API (一种在整个应用程序中共享状态值的方式)来获得一个函数来注销用户。然后将它们导航到登录页面。
import React, { useContext, useEffect, useState } from "react";
import {useNavigate} from "react-router-dom";
import AuthContext from "../../context/AuthContext";
export default function Logout(){
// in order to navigate, you need to first use the useNavigate hook.
const navigate = useNavigate();
//this is a function that logs out the user
let {logoutUser} = useContext(AuthContext);
//at the start, log out the user
useEffect(()=>{
logoutUser();
//then -- and this is the part you want -- navigate them to the login page.
navigate('/auth/login');
},[])
}https://stackoverflow.com/questions/74423883
复制相似问题