React:“重定向"不是从“react-router-dom"导出的 [英] React: 'Redirect' is not exported from 'react-router-dom'

查看:116
本文介绍了React:“重定向"不是从“react-router-dom"导出的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到 尝试导入错误:重定向"不是从react-router-dom"导出的. 在终端中运行 npm run start 时.

I am getting Attempted import error: 'Redirect' is not exported from 'react-router-dom'. when running npm run start in terminal.

我重新安装了node_modules,重新安装了react-router-domreact-router,重新启动了终端和我的电脑,但问题仍然存在.

I have reinstalled node_modules, reinstalled react-router-dom, react-router, restarted terminal and my computer, but the issue persists.

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",

感谢任何帮助,谢谢.

推荐答案

Redirect 组件已从 react-router 版本 6 中删除.

Redirect component has been removed from the react-router version 6.

来自 react-router 的 github:

From the the github of react-router:

重定向(以及 useRoutes 中的重定向)已被移除.

Redirect (and redirectTo in useRoutes) was removed.

我们不再支持在初始渲染时重定向,因为未来版本的 React React 的兼容性问题不会让我们在初始渲染时更改祖先组件中的状态 w/out警告,所以我们必须删除组件,以及能够在初始渲染上执行导航().你仍然可以渲染a ,但它实际上不会更新页面直到下一个渲染.

We no longer support redirecting on the initial render, due to compatibility issues with future versions of React React won't let us change the state in an ancestor component on the initial render w/out warning, so we had to remove the component, as well as the ability to do a navigate() on the initial render. You can still render a , but it won't actually update the page until the next render.

更多详情,请参见:react-router v6.0.0-alpha.3

For more details, see: react-router v6.0.0-alpha.3

如果你想使用 Redirect 组件,你必须使用 react router 版本 5.

If you want to use Redirect component, you will have to use react router version 5.

这篇关于React:“重定向"不是从“react-router-dom"导出的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆