英文:
Using createBrowserRouter and createRoutesFromElements to create routes and navigate between to components
问题
在你的代码中,出现了一个错误。错误信息是:TypeError: Cannot destructure property 'basename' of 'react__WEBPACK_IMPORTED_MODULE_0__.useContext(...)' as it is null.
这个错误通常发生在使用react-router-dom
库时,出现了路由提供器(RouterProvider)的问题。要解决这个问题,你可以尝试以下几步:
- 确保你的项目已经正确安装了
react-router-dom
库。你可以使用以下命令来安装它:
npm install react-router-dom
-
确保你的React版本与
react-router-dom
库的版本兼容。有时候,不同版本的React和react-router-dom
之间可能会出现兼容性问题。你可以查看它们的文档,以确保它们的版本兼容。 -
确保你的路由提供器(RouterProvider)的位置正确。根据你的代码,路由提供器应该包裹整个应用程序,而不仅仅是
RouterProvider
标签内的内容。请确保RouterProvider
在return
语句外部。
如果你仍然遇到问题,可能需要更详细地检查你的项目配置和依赖项版本,以找出导致这个错误的根本原因。希望这些步骤能帮助你解决问题。
英文:
I have two components, the Homepage
and AboutMe
components that I would like to navigate to using their respective links About Me
and Home
.
Homepage.js
function HomePage(){
return(
<h1>Welcome to my site</h1>
)
}
export default HomePage;
AboutMe.js
function AboutMe(){
return(
<h1>About Me</h1>
)
}
export default AboutMe;
With the code below, without the navigation links, I'm able to navigate to the Homepage
component using http://localhost:3001/
and the AboutMe
component using http://localhost:3001/about-me
import React from "react";
import AboutMe from "./AboutMe";
import HomePage from "./Homepage";
import {
createBrowserRouter,
createRoutesFromElements,
Route,
RouterProvider,
Link
} from 'react-router-dom';
// router and routes
const router = createBrowserRouter(
createRoutesFromElements(
<Route >
<Route index path="/" element={<HomePage />} />
<Route path="about-me" element={<AboutMe />} />
</Route>
)
)
function App() {
return (
<RouterProvider router={router} />
)
}
export default App;
But when I try to add the corresponding links as below, I get an error that reads
TypeError: Cannot destructure property 'basename' of 'react__WEBPACK_IMPORTED_MODULE_0__.useContext(...)' as it is null.
App.js
import React from "react";
import AboutMe from "./AboutMe";
import HomePage from "./Homepage";
import {
createBrowserRouter,
createRoutesFromElements,
Route,
RouterProvider,
Link
} from 'react-router-dom';
// router and routes
const router = createBrowserRouter(
createRoutesFromElements(
<Route >
<Route index path="/" element={<HomePage />} />
<Route path="about-me" element={<AboutMe />} />
</Route>
)
)
function App() {
return (
<div>
<nav className="nam">
<Link to="/" className="nav-item">Home</Link>
<Link to="/about-me" className="nav-item">About Me</Link>
</nav>
<RouterProvider router={router} />
</div>
)
}
export default App;
What am I doing wrong?
答案1
得分: 0
You probably need to wrap the Route provider around the app component.
function App() {
return (
<RouterProvider router={router}>
<div>
<nav className="nam">
<Link to="/" className="nav-item">Home</Link>
<Link to="/about-me" className="nav-item">About Me</Link>
</nav>
</div>
</RouterProvider>
)
}
英文:
You probably need to wrap the Route provider around the app component.
function App() {
return (
<RouterProvider router={router}>
<div>
<nav className="nam">
<Link to="/" className="nav-item">Home</Link>
<Link to="/about-me" className="nav-item">About Me</Link>
</nav>
</div>
</RouterProvider>
)
}
Please give it a shot.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论