简体   繁体   English

警告:请使用 `import { NavLink } from "react-router-dom"`

[英]Warning: Please use `import { NavLink } from "react-router-dom"`

I'm getting this error:我收到此错误:

index.js:1437 Warning: Please use `import { NavLink } from "react-router-dom"` instead of `import NavLink from "react-router-dom/es/undefined"`. Support for the latter will be removed in the next major release.

I recently updated my version of react-router and react-router-dom from version ^4.3.1 to version ^5.1.2 .我最近将我的 react-router 和 react-router-dom 版本从^4.3.1到了^5.1.2版本。 The first error after updating the those packages was:更新这些包后的第一个错误是:

You should not use <Switch> outside a <Router> . You should not use <Switch> outside a <Router> After a search, I solved that error in the following way:搜索后,我通过以下方式解决了该错误:

import React, { Component } from 'react'
import { BrowserRouter as Router, Route, Switch } from "react-router-dom"

import Loadable from 'react-loadable'

import { Container } from 'reactstrap'
import Loading from "../../components/loading"

const Example = Loadable({ loader: () => import('../example/index.js'), loading: Loading })


export default class App extends Component {
  render() {
    return (
      <Router>
        <Container>
          <Header/>
          <main>
            <Switch>
             <Route exact path="/example/:exampleName/example1/:exampleName" component={Example}/>
            </Switch>
          </main>
        </Container>
      </Router>
    )
  }
} 

The next error I'm seeing in the console didn't yield any search results when I Googled it.我在控制台中看到的下一个错误在我用谷歌搜索时没有产生任何搜索结果。 It is:这是:

index.js:1437 Warning: Please use `import { NavLink } from "react-router-dom"` instead of `import NavLink from "react-router-dom/es/undefined"`. Support for the latter will be removed in the next major release.

I'm seeing this error multiple times for various react-router-dom related components, such as NavLink , Prompt , and other things I'm not even using in my application.对于各种react-router-dom相关组件(例如NavLinkPrompt以及我什至没有在应用程序中使用的其他内容),我多次看到此错误。

The reason I upgraded the package is because I wanted to use some new features such as the useParams hooks.我升级包的原因是因为我想使用一些新功能,例如 useParams 钩子。

My question: what is the best way to solve the errors and be able to use the latest version of react-router-dom in my project?我的问题:解决错误并能够在我的项目中使用最新版本的react-router-dom的最佳方法是什么?

It turns out browsing the code of both react-router and react-router-dom yields some interesting results:事实证明,浏览 react-router 和 react-router-dom 的代码会产生一些有趣的结果:

It looks like the reason the line is being printed is because the modules were deprecated:打印该行的原因似乎是因为模块已被弃用:

react-router-dom]$ cat NavLink.js

"use strict";
require("./warnAboutDeprecatedCJSRequire")("NavLink");
module.exports = require("./index.js").NavLink;

This happens because these were forwarded modules;发生这种情况是因为这些是转发模块; the module actually printing the error is actually react-router, not react-router-dom:实际打印错误的模块实际上是 react-router,而不是 react-router-dom:

warnAboutDeprecatedESMImport:警告关于已弃用的 ESMI 导入:

cat react-router/es/warnAboutDeprecatedESMImport.js
"use strict";

// ...

export default function(member) {
  printWarning(
    'Please use `import { %s } from "react-router"` instead of `import %s from "react-router/es/%s"`. ' +
      "Support for the latter will be removed in the next major release.",
    [member, member]
  );
}

So even though you didn't import the modules directly, you imported the modules from a module that imports the modules, so to speak.因此,即使您没有直接导入模块,也可以从导入模块的模块中导入模块。

According to their code, react-app-dom may have moved Switch:根据他们的代码,react-app-dom 可能已经移动了 Switch:

react-router/es/Switch.js:反应路由器/es/Switch.js:

    1 "use strict";
    2
    3 import warnAboutDeprecatedESMImport from "./warnAboutDeprecatedESMImport.js";
    4 warnAboutDeprecatedESMImport("Switch");
    5
    6 import { Switch } from "../esm/react-router.js";
    7 export default Switch;

as per their documentation, they mention that there are 2 ways to load react-router-dom, one is the CommonJS way, and another is the ES (ES6?) way, these refer to different ways to run the code (there also appears to be UMD).根据他们的文档,他们提到有两种加载 react-router-dom 的方式,一种是 CommonJS 方式,另一种是 ES(ES6?)方式,这些是指运行代码的不同方式(也出现了成为 UMD)。

Copied from: https://www.npmjs.com/package/react-router-dom复制自: https : //www.npmjs.com/package/react-router-dom

// using ES6 modules
import { BrowserRouter, Route, Link } from "react-router-dom";

// using CommonJS modules
const BrowserRouter = require("react-router-dom").BrowserRouter;
const Route = require("react-router-dom").Route;
const Link = require("react-router-dom").Link;

This is already on their site.这已经在他们的网站上。

This means, that your code may either be running on CommonJS or on ES6.这意味着,您的代码可能运行在 CommonJS 或 ES6 上。 If you can figure out which mode you are running on, you'll be able to select the correct import statements from above.如果您能确定您正在运行的模式,您将能够从上面选择正确的导入语句。

The old way of running the code seems documented here: import { BrowserRouter, Route, Switch } from 'react-router-dom';运行代码的旧方法似乎记录在这里: import { BrowserRouter, Route, Switch } from 'react-router-dom';

The only documentation I found on using Switch, Route and these components is here, so I assume you bumped onto the same code:我找到的关于使用 Switch、Route 和这些组件的唯一文档在这里,所以我假设您遇到了相同的代码:

https://reacttraining.com/react-router/web/api/BrowserRouterhttps://reacttraining.com/react-router/web/api/BrowserRouter

Interestingly, when one follows the steps on that page, everything works like a charm, so I think the problem may be that you have a mix of "new components" or dependencies, with "old" (2016?) dependencies, and as a result, this is causing the problem, the solution may be to just upgrade even more components, instead of upgrading components selectively (which is what I assume you are doing).有趣的是,当人们按照该页面上的步骤进行操作时,一切都像一种魅力结果,这是导致问题的原因,解决方案可能是升级更多组件,而不是有选择地升级组件(我假设您正在这样做)。

Consider these steps:考虑以下步骤:

1. create a new app (in another folder) using create-react-app
2. diff your current package.json vs the newly created package.json
3. figure out which are the differences
4. upgrade additional modules (some times deleting the whole node_modules helps).
5. rebuild and re-test.

Maybe you have an incompatible combination of an old react-router and a new react-router-dom or something like that, and one is using CommonJS but the other one expects ES modules, idk.也许你有一个旧的 react-router 和一个新的 react-router-dom 或类似的不兼容的组合,一个使用 CommonJS,但另一个需要 ES 模块,idk。

I hope this information is useful!.我希望这些信息有用!

maybe removing the ^ from the package.json will help too, by being more explicit about the versions (as per):也许从 package.json 中删除 ^ 也会有所帮助,通过更明确地了解版本(根据):

"

Please try a fresh installation.请尝试全新安装。 There was a release (4.4) that caused a number of issues for people;有一个版本 (4.4) 给人们带来了许多问题; that was unpublished and re-published as React Router v5.0.0.未发布并重新发布为 React Router v5.0.0。 The warning message comes from 4.4/5, but if you're on 4.3.1, you should not see it.警告消息来自 4.4/5,但如果您使用的是 4.3.1,则不应看到它。

https://github.com/ReactTraining/react-router/issues/6650 https://github.com/ReactTraining/react-router/issues/6650

How to fix it:如何修复:

yarn list v1.15.2

warning Filtering by arguments is deprecated. Please use the pattern option instead.

├─ react-router-dom@4.3.1

│ └─ react-router@4.4.0 // it need to be 4.3.1

└─ react-router@4.3.1
Done in 0.98s.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM