小编jon*_*nny的帖子

在 react-router-dom v6 中使用历史记录

**我使用 react-router-dom 版本 6,当我使用 this.props.history.push('/UserDashboard') 它不起作用,我用 const history = createBrowserHistory(); history.push('/UserDashboard') 但我仍然有一个问题,当我想重定向到 '/UserDashboard' 只是链接发生变化而页面仍然是第一个??

有什么帮助吗??**

    handleSubmit(event){
   

    event.preventDefault();
    const history = createBrowserHistory();
    axios({
      method: "POST", 
      url:"http://localhost:3001/users/login", 
      data:  this.state
    }).then((response)=>{
      console.log(response.data.user.admin)
      if (response.data.success === true && response.data.user.admin === false){
       
              const history = createBrowserHistory();
              history.push({
               pathname:"/users",
               state:{
               Key : response.data.user }
 });

    
       
      }else if(response.statusCode === 401 ){
        alert("Invalid username or password");
       window.location.reload(false);
      }
    })
  }
Run Code Online (Sandbox Code Playgroud)

我的 routes.js 文件:

import React from 'react';
import { Navigate } from 'react-router-dom';
import DashboardLayout from './Pages/DashboardLayout';
import …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-router-dom

2
推荐指数
7
解决办法
4316
查看次数

标签 统计

javascript ×1

react-router-dom ×1

reactjs ×1