我无法在<Router>组件外使用NavLink。

huangapple go评论74阅读模式
英文:

I can't use NavLink outside <Router> component

问题

我正在构建一个React应用程序,使用react-router-dom。为了为我的页面添加更多动画,我使用framer-motion在应用程序的页面之间创建过渡效果,但我不希望导航栏在每次切换路由时都进行动画和重新生成。因此,我确保通过不将其包含在路由器中,使导航栏不进行动画。

问题是它会呈现一个空白页面,当我查看控制台时,会出现许多错误:

> 未捕获错误:仅在<Router>组件的上下文中可以使用useLocation()。

> 上述错误出现在<NavLink>组件中:

我无法在<Router>组件外使用NavLink。

这是我的代码:

App.tsx

import React from "react";
import { createBrowserRouter, RouterProvider, Route } from "react-router-dom";
import Navbar from "./components/Navbar";
import { AnimatePresence } from "framer-motion";

import Home from "./pages/Home/Home.tsx";
import About from "./pages/About/About.tsx";

const router = createBrowserRouter([
  {
    path: "/",
    element: <Home />,
  },
  {
    path: "/about",
    element: <About /> 
  }
]);

function App() {
  return(
    <>
      <Navbar />
      
      <AnimatePresence mode="wait">
        <RouterProvider router={router} />
      </AnimatePresence>
    </>
  )
}

export default App;

Navbar.tsx

import React from "react";
import styled from "@emotion/styled";
import { NavLink } from "react-router-dom";

export default function Navbar(props: React.AllHTMLAttributes<HTMLElement>) {
  return (
    <NavbarDiv>
      <NavLink to="/" className={({ isActive }) => isActive ? "active" : ""}>
        Home
      </NavLink>
      <NavLink to="/" className={({ isActive }) => isActive ? "active" : ""}>
        About
      </NavLink>
    </NavbarDiv>
  )
}

const NavbarDiv = styled.div`
  width: 100%;
  height: 85px;
  background-color: #242424;
  color: var(--background-color);
  box-shadow: 0 2px 4px 0 rgba(0,0,0,.25);
`;
英文:

I'm building a React application with react-router-dom. To add more animations to my pages, I use framer-motion to create transitions between the pages of the application but I don't want the navbar to be animated and regenerated every times I navigate between my routes. So to I made sure that the navbar not animate by not including it in my router.

The problem is that it renders a blank page and when I go in the console, there are many errors like :

> Uncaught Error: useLocation() may be used only in the context of a
> <Router> component.

>
> The above error occurred in the <NavLink> component:

我无法在<Router>组件外使用NavLink。

Here is my code:

App.tsx

import React from &quot;react&quot;;
import { createBrowserRouter, RouterProvider, Route } from &quot;react-router-dom&quot;;
import Navbar from &quot;./components/Navbar&quot;;
import { AnimatePresence } from &quot;framer-motion&quot;;

import Home from &quot;./pages/Home/Home.tsx&quot;;
import About from &quot;./pages/About/About.tsx&quot;;

const router = createBrowserRouter([
  {
    path: &quot;/&quot;,
    element: &lt;Home /&gt;,
  },
  {
    path: &quot;/about&quot;,
    element: &lt;About /&gt; 
  }
]);

function App() {
  return(
    &lt;&gt;
      &lt;Navbar /&gt;
      
      &lt;AnimatePresence mode=&quot;wait&quot;&gt;
        &lt;RouterProvider router={router} /&gt;
      &lt;/AnimatePresence&gt;
    &lt;/&gt;
  )
}

export default App;

Navbar.tsx

import React from &quot;react&quot;;
import styled from &quot;@emotion/styled&quot;;
import { NavLink } from &quot;react-router-dom&quot;;

export default function Navbar(props: React.AllHTMLAttributes&lt;HTMLElement&gt;) {
  return (
    &lt;NavbarDiv&gt;
      &lt;NavLink to=&quot;/&quot; className={({ isActive }) =&gt; isActive ? &quot;active&quot; : &quot;&quot;}&gt;
        Home
      &lt;/NavLink&gt;
      &lt;NavLink to=&quot;/&quot; className={({ isActive }) =&gt; isActive ? &quot;active&quot; : &quot;&quot;}&gt;
        About
      &lt;/NavLink&gt;
    &lt;/NavbarDiv&gt;
  )
}

const NavbarDiv = styled.div`
  width: 100%;
  height: 85px;
  background-color: #242424;
  color: var(--background-color);
  box-shadow: 0 2px 4px 0 rgba(0,0,0,.25);
`;

答案1

得分: 1

Navbar 必须在路由上下文中渲染。稍微重构代码以创建 布局路由,用于渲染 Navbar 和嵌套路由的 Outlet 组件。

示例:

import React from "react";
import {
  createBrowserRouter,
  RouterProvider,
  Route,
  Outlet,
} from "react-router-dom";
import { AnimatePresence } from "framer-motion";
import Home from "./pages/Home/Home.tsx";
import About from "./pages/About/About.tsx";
import Navbar from "./components/Navbar";

const AppLayout = () => (
  <>
    <Navbar />
    <AnimatePresence mode="wait">
      <Outlet />
    </AnimatePresence>
  </>
);

const router = createBrowserRouter([
  {
    element: <AppLayout />,
    children: [
      {
        path: "/",
        element: <Home />,
      },
      {
        path: "/about",
        element: <About /> 
      }
    ]
  },
]);

function App() {
  return <RouterProvider router={router} />;
}

export default App;
英文:

The links in the Navbar must be rendered within a routing context. Refactor the code a bit to create a layout route that renders Navbar and an Outlet component for the nested routes the layout route renders.

Example:

import React from &quot;react&quot;;
import {
  createBrowserRouter,
  RouterProvider,
  Route,
  Outlet,
} from &quot;react-router-dom&quot;;
import { AnimatePresence } from &quot;framer-motion&quot;;
import Home from &quot;./pages/Home/Home.tsx&quot;;
import About from &quot;./pages/About/About.tsx&quot;;
import Navbar from &quot;./components/Navbar&quot;;

const AppLayout = () =&gt; (
  &lt;&gt;
    &lt;Navbar /&gt;
    &lt;AnimatePresence mode=&quot;wait&quot;&gt;
      &lt;Outlet /&gt;
    &lt;/AnimatePresence&gt;
  &lt;/&gt;
);

const router = createBrowserRouter([
  {
    element: &lt;AppLayout /&gt;,
    children: [
      {
        path: &quot;/&quot;,
        element: &lt;Home /&gt;,
      },
      {
        path: &quot;/about&quot;,
        element: &lt;About /&gt; 
      }
    ]
  },
]);

function App() {
  return &lt;RouterProvider router={router} /&gt;;
}

export default App;

huangapple
  • 本文由 发表于 2023年4月19日 22:37:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定