Using createBrowserRouter and createRoutesFromElements to create routes and navigate between to components

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

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)的问题。要解决这个问题,你可以尝试以下几步:

  1. 确保你的项目已经正确安装了react-router-dom库。你可以使用以下命令来安装它:
npm install react-router-dom
  1. 确保你的React版本与react-router-dom库的版本兼容。有时候,不同版本的React和react-router-dom之间可能会出现兼容性问题。你可以查看它们的文档,以确保它们的版本兼容。

  2. 确保你的路由提供器(RouterProvider)的位置正确。根据你的代码,路由提供器应该包裹整个应用程序,而不仅仅是RouterProvider标签内的内容。请确保RouterProviderreturn语句外部。

如果你仍然遇到问题,可能需要更详细地检查你的项目配置和依赖项版本,以找出导致这个错误的根本原因。希望这些步骤能帮助你解决问题。

英文:

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 (
&lt;RouterProvider router={router}&gt;
    &lt;div&gt;
      &lt;nav className=&quot;nam&quot;&gt;
        &lt;Link to=&quot;/&quot; className=&quot;nav-item&quot;&gt;Home&lt;/Link&gt;
        &lt;Link to=&quot;/about-me&quot; className=&quot;nav-item&quot;&gt;About Me&lt;/Link&gt;
      &lt;/nav&gt; 
    &lt;/div&gt;
&lt;/RouterProvider&gt;
  )
}

Please give it a shot.

huangapple
  • 本文由 发表于 2023年7月14日 06:25:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683619.html
匿名

发表评论

匿名网友

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

确定