相关疑难解决方法(0)

在 React.js 中,如何在用户单击后退按钮时运行函数?

我环顾四周,试图用 React router 找到一个解决方案。通过 V5,您可以使用<Promt />.
我还尝试寻找一个普通的 JavaScript 解决方案,但没有任何效果。

我使用 React router v6 并histroy替换为const navigate = useNavigation()没有.listen属性的 router。

此外,v6 没有<Promt />组件。

尽管如此,最后我还是使用了useEffectclear函数。但这适用于组件的所有更改。前进的时候也是如此。

根据react.js文档“React在组件卸载时执行清理”。

 useEffect(() => {
    // If user clicks the back button run function
    return resetValues();;
  })
Run Code Online (Sandbox Code Playgroud)

reactjs react-router

5
推荐指数
1
解决办法
5078
查看次数

在 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
查看次数