简体   繁体   English

来自 React-router-dom 的 NavLink/Link 不起作用(空白屏幕)

[英]NavLink/Link from React-router-dom not working (blank screen)

Hi guys I was starting out on React and came across this problem, this is my Navbar component:大家好,我刚开始使用 React 时遇到了这个问题,这是我的 Navbar 组件:

import { BrowserRouter as NavLink } from "react-router-dom";

    const Navbar = () => {
      return (
        <>
          <NavLink as={NavLink} exact to="/">
            Hello
          </NavLink>
        </>
      );
    };

    export default Navbar;

and this is my App.js:这是我的 App.js:

    import "./App.css";
    import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
    import Navbar from "./Components/Navbar";
    
    const App = () => {
      return <Navbar />;
    };

    export default App;

when I run the code this is what happens:当我运行代码时,会发生以下情况:

React App running on chrome在 chrome 上运行的 React 应用程序

Also the link is not clickable nor has an underline like when using a simple tag And what's even worse is that if I change the import statement in my Navbar.js from:此外,该链接不可点击,也没有像使用简单标签时那样的下划线更糟糕的是,如果我将 Navbar.js 中的导入语句更改为:

import { BrowserRouter as NavLink } from "react-router-dom";

to

import { NavLink } from "react-router-dom";

everything goes blank:一切都变成空白:

React app running on Chrome在 Chrome 上运行的 React 应用程序

my react-router-dom version is "^6.3.0"我的 react-router-dom 版本是“^6.3.0”

please help I have seen different articles and I couldn't come up with a solution请帮助我看过不同的文章,但我无法提出解决方案

in v6+ keywords "exact" and "strict" don't work anymore on NavLink, instead put "end"在 v6+ 中,关键字“exact”和“strict”不再适用于 NavLink,而是放置“end”

you don't use BrowserRouter as a navlink but您不使用 BrowserRouter 作为导航链接,但

import {BrowserRouter as Router, Routes, Route} from 'react-router-dom
<Router>
    <Routes>
       <Route path="/home" element={<HomePage />} />
       <Route path="/page1" element={<PageOne />} />
    </Routes>
</Router>

or或者

import { NavLink } from 'react-router-dom'
<NavLink className={({ isActive }) => "nav-link" + (isActive ? " activated" : "")} end to="/home">
    home
</NavLink>

https://v5.reactrouter.com/web/api/BrowserRouter https://v5.reactrouter.com/web/api/BrowserRouter

Let me explain to you a bit how react-router works.让我向您解释一下 react-router 的工作原理。 There are 2 components you need: the router that will show the component you want, depending on the path you have, and the navbar, which will change that path.您需要 2 个组件:将根据您拥有的路径显示所需组件的路由器和将更改该路径的导航栏。

Let's start with the router component, BrowserRouter.让我们从路由器组件 BrowserRouter 开始。 This is an example from docs.这是来自文档的示例。 In here, you define what components the router to show on your page, depending on the path:在这里,您可以根据路径定义路由器要在页面上显示的组件:

<BrowserRouter>
    <Routes>
      <Route path="/" element={<App />}>
        <Route path="teams" element={<Teams />}>
          <Route path=":teamId" element={<Team />} />
        </Route>
      </Route>
    </Routes>
  </BrowserRouter>

Now, you need a way to navigate to these routes.现在,您需要一种导航到这些路线的方法。 For that, you can use Link (should work with NavLink as well):为此,您可以使用 Link(也应该与 NavLink 一起使用):

  <nav>
    <Link to="/">Home</Link>
    <Link to="/teams">Teams</Link>
  </nav>

Basically, your mistake is that you either have one or the other, not both at the same time.基本上,您的错误是您要么拥有一个,要么拥有另一个,而不是同时拥有两者。 You need both to work.你需要两者都工作。

Hello everyone I have found a solution to my problem.大家好,我找到了解决问题的方法。

I simply updated node and npm to the latest version and that seemed to resolve the blank screen issue.我只是将节点和 npm 更新到最新版本,这似乎解决了黑屏问题。

for npm I just typed in the command:对于 npm 我刚刚输入了命令:

npm update -g

Next I downloaded and installed the LTS version of node.js from:接下来我从以下位置下载并安装了 node.js 的 LTS 版本:

https://nodejs.org/en/

I thank everyone for their contribution.我感谢大家的贡献。 Bye再见

The following day the issue arose again and I was greeted with the blank page yet again.第二天,问题再次出现,我再次受到空白页的欢迎。

Here is what I did to get it working for good this time.这是我这次为了让它正常工作而做的事情。

after a lot of researching articles and trial and error, I found a simple solution.经过大量研究文章和反复试验,我找到了一个简单的解决方案。 And I think this will help others as well.我认为这也会对其他人有所帮助。

I just changed the import statement in my app.js from:我刚刚将我的 app.js 中的导入语句从:

import { Router, Routes, Route } from "react-router-dom";

to

import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

This fixed the issue of the blank screen.这解决了空白屏幕的问题。

App.js:应用程序.js:

import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Navbar from "./components/Navbar";
import Home from "./components/pages/Home";
import About from "./components/pages/About";
import Contact from "./components/pages/Contact";

function App() {
  return (
    <>
      <Router>
        <Navbar />
        <Routes>
          <Route path="/" element={<Home />} />
          <Route path="/about" element={<About />} />
          <Route path="/contact" element={<Contact />} />
        </Routes>
      </Router>
    </>
  );
}

also for the texts from other components to appear I used element :为了让其他组件的文本出现,我使用了element

<Route path="/" element={<Home />} />

instead of component :而不是组件

<Route path="/" component={<Home />} />

For others having this issue the real fix is that your NavBar component should be nested within your BrowserRouter like so:对于其他有此问题的人,真正的解决方法是您的 NavBar 组件应该嵌套在 BrowserRouter 中,如下所示:

import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Navbar from "./components/Navbar";
import Home from "./components/pages/Home";
import About from "./components/pages/About";
import Contact from "./components/pages/Contact";

function App() {
  return (
    <>
      <Router>
        <Navbar /> {/* Nested in Router */}
        <Routes>
          <Route path="/" element={<Home />} />
          <Route path="/about" element={<About />} />
          <Route path="/contact" element={<Contact />} />
        </Routes>
      </Router>
    </>
  );
}

import React from "react";从“反应”导入反应; Add the above line to your file Navbar component then this issue resolved将上述行添加到您的文件导航栏组件中,然后此问题已解决

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

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