简体   繁体   English

React 项目中的 NavLink 组件未显示在屏幕上

[英]The NavLink components in React project not showing up on screen

I'm new to React and I'm trying to use react-router to build a Single Page Application.我是 React 的新手,我正在尝试使用react-router来构建单页应用程序。 What I mean is to link all pages ( Home , About , Login , etc) in the navigation bar by using <NavLink> component from react-router-dom .我的意思是使用react-router-dom中的<NavLink>组件链接导航栏中的所有页面( HomeAboutLogin等)。 The result turns out is a blank screen with nothing showing up.结果是一个空白屏幕,什么都没有显示。 Please help me to fix this issue, I have attached my source code below (in JavaScript and TailwindCSS):请帮我解决这个问题,我在下面附上了我的源代码(在 JavaScript 和 TailwindCSS 中):

App.js应用程序.js

import React from "react";
import NavBar from "./components/NavBar";
import About from "./components/About";
import { BrowserRouter, Route, Routes } from "react-router-dom";

function App() {
  return (
    <>
      <BrowserRouter>
        <NavBar />
        <div className="container mt-2" style={{ marginTop: 40 }}>
          <Routes>
            <Route path="/about">
              <About />
            </Route>
            ...
          </Routes>
        </div>
      </BrowserRouter>
    </>
  );
}

export default App;

NavBar.js导航栏.js

import React, { useState } from "react";
import { NavLink } from "react-router-dom";


const Navbar = () => {
  const [isOpen, setOpen] = useState(false);
  return (
    <nav
      className="relative flex flex-wrap items-center justify-between px-2 py-2 bg-white-500 mb-3 border-b-2 drop-shadow-md"
      role="navigation"
      aria-label="main navigation"
    >
      <div className="container px-4 mx-auto flex flex-wrap items-center justify-between">
        <NavLink
          className="navbar-item"
          activeClassName="is-active"
          to="/"
          exact
        >
          BrandNAME
        </NavLink>
        
        <div className={`navbar-menu ${isOpen && "is-active"}`}>
          <div className="flex flex-col lg:flex-row list-none lg:ml-auto">
            <NavLink
              className="navbar-item px-3 py-2 flex items-center text-md leading-snug text-black cursor-pointer hover:opacity-75"
              activeClassName="is-active"
              to="/about"
            >
              About
            </NavLink>

            <NavLink
              className="navbar-item px-3 py-2 flex items-center text-md leading-snug text-black cursor-pointer hover:opacity-75"
              activeClassName="is-active"
              to="/socials"
            >
              Socials
            </NavLink>
          </div>

          <div className="navbar-end">
            <div className="navbar-item">
              <div className="buttons">
                <a className="button is-white">Log in</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </nav>
  );
};
export default Navbar;

and an example of About.jsAbout.js的一个例子

import React from "react";

const About = () => (
  <div>
    <h1 className="title is-1">This is the About Page</h1>
    <p>
      Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
      inceptos himenaeos. Vestibulum ante ipsum primis in faucibus orci luctus
      et ultrices posuere cubilia curae; Duis consequat nulla ac ex consequat,
      in efficitur arcu congue. Nam fermentum commodo egestas.
    </p>
  </div>
);

export default About;

In react-router-dom@6 the Route component API changed significantly.react-router-dom@6中, Route组件 API 发生了显着变化。 Only another Route component is a valid child component, the routed content should be rendered on the Route component's element prop.只有另一个Route组件是有效的子组件,路由的内容应该在Route组件的element属性上呈现。 The element prop takes a ReactNode , aka JSX. element属性需要一个ReactNode ,也就是 JSX。

Route路线

declare function Route( props: RouteProps ): React.ReactElement | null; interface RouteProps { caseSensitive?: boolean; children?: React.ReactNode; element?: React.ReactNode | null; index?: boolean; path?: string; }

Example:例子:

<Routes>
  <Route path="/about" element={<About />} />
  ...
</Routes>

If you are using react-router-dom version 6 you should import like this:如果您使用的是 react-router-dom 版本 6,您应该像这样导入:

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

And use like this:并像这样使用:

    <>
      <Router>
        <NavBar />
        <div className="container mt-2" style={{ marginTop: 40 }}>
          <Routes>
            <Route path="/about" exact element={<About/>}>
            ...
          </Routes>
        </div>
       </Router>
    </>

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

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