javascript - 在 react-router-dom v6 中使用历史

标签 javascript reactjs react-router-dom

我用 react-router-dom version 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);
          }
        })
      }
我的 routes.js 文件:
    import React from 'react';
    import { Navigate } from 'react-router-dom';
    import DashboardLayout from './Pages/DashboardLayout';
    import AccountView from './Pages/views/account/AccountView';
    import CustomerListView from './Pages/views/customer/CustomerListView';
    import DashboardView from './Pages/views/reports/DashboardView';
    import ProductListView from './Pages/views/product/ProductListView';
    import SettingsView from './Pages/views/settings/SettingsView';
    import Home from './Pages/home';
    import About from './Pages/About';
    import Partners from './Pages/Partners';
    import Services from './Pages/services';
    import Login from './Pages/Login';
    import RD from './Pages/RD';
    import ContactUs from './Pages/contactus';
    import Apply from './Pages/apply';
    import PartnerShip from './Pages/partnership';
    import News from './Pages/News';
    const routes = [
     {
     path: 'users',
     element: <DashboardLayout />,
     children: [
      { path: 'account', element: <AccountView /> },
      { path: 'customers', element: <CustomerListView /> },
      { path: 'dashboard', element: <DashboardView /> },
      { path: 'products', element: <ProductListView /> },
      { path: 'settings', element: <SettingsView /> }
      ]
     },
    {
    path: '/',
    element: <Home />,
    },
    {
    path: 'about',
    element: <About />
    },
     {path: 'partners',
     element: <Partners />,
    
    },
    {
    path: 'services',
    element: <Services />,
    
    },
    {
    path: 'contactus',
    element: <ContactUs />,
    
    },
    {
    path: 'login',
    element: <Login />,
    
     },{
    path: 'RD',
    element: <RD />,
    
    },
    {
    path: 'apply',
    element: <Apply />,
    
     },
     {
    path: 'partnership',
    element: <PartnerShip />,
    
     },
     {
    path: 'News',
    element: <News />,
    
     }
    ];

    export default routes;

最佳答案

在 react-router-dom v6 中,需要使用 使用导航 而不是使用历史。
参见 https://reacttraining.com/blog/react-router-v6-pre/ 中的示例

import React from 'react';
import { useNavigate } from 'react-router-dom';

function App() {
  let navigate = useNavigate();
  let [error, setError] = React.useState(null);

  async function handleSubmit(event) {
    event.preventDefault();
    let result = await submitForm(event.target);
    if (result.error) {
      setError(result.error);
    } else {
      navigate('success');
    }
  }

  return (
    <form onSubmit={handleSubmit}>
      // ...
    </form>
  );
}

关于javascript - 在 react-router-dom v6 中使用历史,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63471931/

相关文章:

reactjs - 如何在react中不刷新页面的情况下向url添加新参数?

javascript - 展开和折叠元素以及从 JavaScript 中删除静态文本

javascript - 多个 HTML 相同的 id 元素,如果只有一个在 DOM 中呈现

javascript - react-webcam(npm 包)视频大小

reactjs - 如何在 Material UI 选择中显示多个空白区域?

javascript - 使用 nextjs 放大时,React Image Magnifiers 不起作用

reactjs - 如何在 react-router-dom v6 中使用 React Routing Navigate 而不是重定向

javascript - 刷新页面后如何留在 protected 路线上。 react 路由器 5.2.0

javascript - 开发nodeJS web应用是否需要express.js框架

javascript - Vuetify : checkbox shows status is checked when it is unchecked, 反之亦然