React:'Redirect'不是从'react-router-dom'导出的

我在终端中运行时收到以下错误。npm run start

尝试导入错误:“重定向”未从“react-router-dom”导出。

我已重新安装 、 、 。已重新启动终端和计算机,但问题仍然存在。node_modulesreact-router-domreact-router

我的代码:

import React from 'react';
import { Switch, Redirect } from 'react-router-dom';

import { RouteWithLayout } from './components';
import { Minimal as MinimalLayout } from './layouts';

import {
  Login as LoginView,
  Dashboard as DashboardView,
  NotFound as NotFoundView
} from './views';

const Routes = () => {
  return (
    <Switch>
      <Redirect
        exact
        from="/"
        to="/dashboard"
      />
      <RouteWithLayout
        component={routeProps => <LoginView {...routeProps} data={data} />}
        exact
        layout={MinimalLayout}
        path="/login"
      />
      <Redirect to="/not-found" />
    </Switch>
  );
};

export default Routes;

这是我的进口:package.json

"react-router": "^6.0.0-beta.0",
"react-router-dom": "^6.0.0-beta.0",

答案 1

对于 v6,只需替换为react-router-domRedirectNavigate

import { Navigate } from 'react-router-dom';
.
.
.
{ component: () => <Navigate to="/404" /> }

答案 2

重定向已从 v6 中删除。您可以将“重定向”替换为“导航”。

import {
  BrowserRouter as Router,
  Routes,
  Route,
  Navigate,
} from 'react-router-dom';
import Home from '../home/Home';

export default function App() {
  return (
    <Router>
      <Routes>
        <Route path="/home" element={<Home />} />
        <Route path="/" element={<Navigate replace to="/home" />} />
      </Routes>
    </Router>
  );
}