英文:
I can't use NavLink outside <Router> component
问题
我正在构建一个React应用程序,使用react-router-dom
。为了为我的页面添加更多动画,我使用framer-motion
在应用程序的页面之间创建过渡效果,但我不希望导航栏在每次切换路由时都进行动画和重新生成。因此,我确保通过不将其包含在路由器中,使导航栏不进行动画。
问题是它会呈现一个空白页面,当我查看控制台时,会出现许多错误:
> 未捕获错误:仅在<Router>组件的上下文中可以使用useLocation()。
> 上述错误出现在<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:
Here is my code:
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);
`;
答案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 "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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论