javascript - TypeError: Cannot destructure property 'basename' of 'React2.useContext(...)' as it

It has been a while since I've worked with React-Router and the first time with v6. My application

It has been a while since I've worked with React-Router and the first time with v6. My applications is using:

  • Vite
  • React
  • Material-UI

I've attempted the following:

  • Searched the web
  • reread the docs multiple times
  • followed the react-router tutorial and applied it to my application

I've been taking a step by step approach to the application where I test each change before I go onto the next. It appears my routing is correct as I tested it by typing in the routes as the URL. However, when I attempt to add Link, I'm getting the out of context error. I know I'm missing something simple but I'm just not seeing it. Here is the code with imports omitted for brevity.

index.jsx

ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <ThemeProvider theme={theme}>
      {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
      <CssBaseline />
      <App />
    </ThemeProvider>
  </React.StrictMode>
)

app.jsx

const router = createBrowserRouter([
  {
    path: "/",
    element: "",
    errorElement: <ErrorPage />,
    children: [
      {
        path: "/about",
        element: <About />,
      },
      {
        path: "/products",
        element: <Products />,
      },
      {
        path: "/",
        element: <Home />,
      },
    ]
  },

]);

function App() {
  const [count, setCount] = useState(0)

  return (  
    <><HeaderBar />
    <RouterProvider router={router}>
      <div>
        <Outlet />
        </div>
      </div>
    </RouterProvider></>
  )
}

export default App

HeaderBar.jsx (only failing element is shown for brevity)

<Button
  key={page.text}
  onClick={handleCloseNavMenu}
  sx={{ my: 2, color: 'white', display: 'block' }}
  ponent={Link}
  to={page.target}
>
  {page.text}
</Button>

I have attempted to create the context by trying to wrap <BrowserRouter> around various ponents, but receive error messages about having 2 routers in my application.

It has been a while since I've worked with React-Router and the first time with v6. My applications is using:

  • Vite
  • React
  • Material-UI

I've attempted the following:

  • Searched the web
  • reread the docs multiple times
  • followed the react-router tutorial and applied it to my application

I've been taking a step by step approach to the application where I test each change before I go onto the next. It appears my routing is correct as I tested it by typing in the routes as the URL. However, when I attempt to add Link, I'm getting the out of context error. I know I'm missing something simple but I'm just not seeing it. Here is the code with imports omitted for brevity.

index.jsx

ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <ThemeProvider theme={theme}>
      {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
      <CssBaseline />
      <App />
    </ThemeProvider>
  </React.StrictMode>
)

app.jsx

const router = createBrowserRouter([
  {
    path: "/",
    element: "",
    errorElement: <ErrorPage />,
    children: [
      {
        path: "/about",
        element: <About />,
      },
      {
        path: "/products",
        element: <Products />,
      },
      {
        path: "/",
        element: <Home />,
      },
    ]
  },

]);

function App() {
  const [count, setCount] = useState(0)

  return (  
    <><HeaderBar />
    <RouterProvider router={router}>
      <div>
        <Outlet />
        </div>
      </div>
    </RouterProvider></>
  )
}

export default App

HeaderBar.jsx (only failing element is shown for brevity)

<Button
  key={page.text}
  onClick={handleCloseNavMenu}
  sx={{ my: 2, color: 'white', display: 'block' }}
  ponent={Link}
  to={page.target}
>
  {page.text}
</Button>

I have attempted to create the context by trying to wrap <BrowserRouter> around various ponents, but receive error messages about having 2 routers in my application.

Share Improve this question edited Mar 4, 2024 at 3:54 Drew Reese 205k18 gold badges246 silver badges274 bronze badges asked Mar 4, 2024 at 3:41 HowardHoward 392 silver badges7 bronze badges 1
  • Simply put, your <HeaderBar> is outside the <RouterProvider> so <Link> will fail. Move it inside – Phil Commented Mar 4, 2024 at 3:50
Add a ment  | 

1 Answer 1

Reset to default 5

The issue here is that the HeaderBar is rendered outside a router, e.g. any routing context, so the Link ponent doesn't work. Move it into the router.

The RouterProvider ponent also doesn't consume any children prop, so the "child" JSX is pletely ignored and should be removed.

Example using a layout route ponent to render the header and Outlet for the nested routes:

const AppLayout = () => (
  <>
    <HeaderBar />
    <Outlet />
  </>
);

const router = createBrowserRouter([
  {
    path: "/",
    element: <AppLayout />,
    errorElement: <ErrorPage />,
    children: [
      {
        path: "/about",
        element: <About />,
      },
      {
        path: "/products",
        element: <Products />,
      },
      {
        path: "/",
        element: <Home />,
      },
    ]
  },

]);

function App() {
  const [count, setCount] = useState(0)

  return <RouterProvider router={router} />;
}

export default App;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745647622a4638066.html

相关推荐

  • Go 1.15 相比 Go 1.14 有哪些值得注意的改动?

    本系列旨在梳理 Go 的 release notes 与发展史,来更加深入地理解 Go 语言设计的思路。docgo1.15Go 1.15 在 Go 1.14 的基础上带来了一些重要的更新和改进。虽然没有语言层面的重大变化,但工具链、运行

    2小时前
    00
  • Firecrawl MCP 实战

    前言最近热衷于找一些好玩的MCP,集成在cursor中,给大模型外挂许多有趣的功能,在开发的代码的同时,在IDE中可以获得更多的乐趣。例如:什么是MCP?本地如何开发MCP ServerMCP实战 | cursor 如何一句话操作 gitH

    2小时前
    00
  • Arthas classloader (查看 classloader 的继承树,urls,类加载信息)

    @toc二、命令列表2.2 classclassloader相关命令2.2.4 classloader (查看 classloader 的继承树,urls,类加载信息)提示查看 classloader 的继承树,urls,类加载信息。可以

    2小时前
    00
  • YashanDB |自研 YFS,为共享集群打造专属“高性能存储引擎”

    在构建 YashanDB 共享集群的过程中,我们面临一个关键抉择:底层存储服务到底是选择通用现成方案,还是自研一套更贴合需求的组件?经过多轮实测、权衡和落地实践,我们选择了“自研 YFS 文件系统”这条更具挑战但更具潜力的道路。本文就来讲讲

    2小时前
    00
  • 运维实战来了|手把手教你构建 YashanDB 的 Prometheus Exporter

    今天和大家分享一位用户的实战投稿,主题是:如何从零构建一个适用于 YashanDB 的 Prometheus Exporter,打通监控全链路,打造更智能的数据库运维体系。一、为什么需要 YashanDB Exporter?在数据库运维中,

    1小时前
    00
  • YashanDB 知识库|数据误删别慌!一文教你搞定“表闪回”

    在日常开发或测试中,难免会出现误删表数据的情况。别急,YashanDB 支持灵活的表闪回功能,让你轻松恢复被误删的数据,不再手忙脚乱。以下结合实际场景,演示如何通过闪回查询、闪回表、回收站机制等手段,把误删的数据完美还原。一、删除操作下的数

    1小时前
    00
  • 推荐一个非常实用的在线AI工具网站

    在线工具网是一个包含AI工具、站长工具、开发人员工具、实用工具、AI助手,能够提供最新AI知识库、在线编码、正则表达式、加密解密、二维码生成、在线进制转换、JSON解析格式化、JavaScript、css、httml格式化混淆压缩、时间

    1小时前
    00
  • Oracle Linux 8.10 编译安装sysbench

    1.首先下载sysbench二进制包代码语言:javascript代码运行次数:0运行复制[root@mysql8_3 software]# wget .zip--2025-04-24 16:14:25-- .zip正在解析主机 githu

    1小时前
    00
  • Tencent Edge Cloud Product

    1. Support containerized deploymentTencent Cloud Edge Availability Zone uses containerization technology to allow users

    1小时前
    10
  • 告别一眼ai,bokeh

    不再一眼看穿AI,Bokeh模型让你拥有单反级画质!你是不是用AI画图总觉得“有点假”,一看就知道不是人拍的?Bokeh 3.5M模型来了,就是要让AI画出来的图也能像单反相机拍的,细节、质感、氛围全都在线。无论你是画插画、做设计、还是玩摄

    1小时前
    00
  • 最近爆火的大模型 MCP,到底是个啥?

    标签:#MCP #AI助理 #人工智能 #爆款技术 #模型上下文协议 #科技解释 #通俗讲解 #ChatGPT #AI动手时代 #新技术“给我发一封邮件,告诉老板我今天请假。”想象一下,如果你对 AI 助手下达这样的指令,它能不能立刻帮你写

    1小时前
    10
  • 【云顾问最佳实践】游戏行业如何通过MySQL主从切换演练构建高可用护城河?

    当数据库成为游戏世界的"复活点"在典型的游戏行业架构中,MySQL数据库如同数字世界的复活水晶,承载着玩家角色数据、装备交易记录、社交关系链等核心资产。如下图游戏行业架构图所示,用户请求通过接入层后,进入登录服、平台服、

    1小时前
    30
  • 《微服务必解之惑:分布式事务方案大揭秘》

    微服务架构凭借其灵活性、可扩展性以及便于维护的特性,成为众多企业构建应用的首选架构。但随着架构复杂度的提升,分布式事务问题逐渐浮出水面,成为开发者们亟待攻克的难关。今天,我们就一同深入探讨如何在微服务架构中解决这一棘手问题。微服务架构,就像

    1小时前
    20
  • 开发体育赛事直播系统:用户管理机制与内容审核技术实现方案

    本体育赛事直播系统技术架构由 东莞梦幻网络科技 自主研发。管理端基于 ThinkPHP 框架 开发,承担平台内容审核、用户管理、权限配置等关键职能。以下将从技术角度出发,系统化阐述该体育直播平台在用户管理、内容控制与互动机制等方面的实现方案

    49分钟前
    00
  • 非靶向代谢组学—基础知识3(测序报告解读)

    非靶向代谢组学—基础知识3(测序报告解读)最近重新看了某测序公司的非靶向代谢组学的分析报告,感觉收获很大。这次就对非靶向代谢组学的常规下游分析内容进行一个整理,并不涉及代码,主要对基础概念的解读。1.代谢组学概述代谢组学(metabolom

    46分钟前
    00
  • Kubernetes学习笔记(二)

    一、应用程序 Pod基本概念:pod存在的意义:1、容器之间资源共享实现:演示共享网络:[root@k8s-master ~]# vim pod-network.yaml[root@k8s-master ~]# kubectl apply

    43分钟前
    00
  • Edge cloud service solution based on mobile micro data center

    [Background]During large-scale events and conferences, the location has a high demand for edge computing power and edge

    23分钟前
    00
  • 使用开源免费雷池WAF防火墙,接入保护你的网站

    使用开源免费雷池WAF防火墙,接入保护你的网站大家好,我是星哥,昨天介绍了《开源免费WEB防火墙,不让黑客越雷池一步!》链接:今天讲一下如何把网站接入到雷池WAF。首先需要安装雷池WAF。雷池工作原理(图)image-20250424190

    20分钟前
    00
  • Abaqus做裂纹扩展仿真,只需要4步

    随着汽车工业的持续进步,轮胎作为车辆不可或缺的一部分,其性能标准也愈发严苛。在众多性能指标中,轮胎的疲劳破坏特性尤为引人瞩目,因为它直接关系到轮胎的可靠性和使用寿命。而整胎的疲劳破坏往往以橡胶基体的损坏为起点,Abaqus这意味着橡胶基体的

    11分钟前
    00
  • 架构人生:我技术生涯中的那些第一次

    我从事软件开发这个工作已经20多年了,技术生涯中也经历过很多的第一次,回首往昔,每一个第一次我都记忆犹新、历历在目。这些第一次奠定了我的职业基础,也奠定了我的人生之路。写第一行职业代码我大学的专业是工业自动化,第一份工作是到一家国企做仪表工

    3分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信