学无先后,达者为师

网站首页 编程语言 正文

Error: A <Route> is only ever to be used as the child of <Routes> element, never rendered directly.

作者:谢小妞 更新时间: 2022-02-28 编程语言

原因

React路由版本问题,你可以查看自己的package.json文件,查看react-router-dom的版本,应该是 6 版本。

在 React-Router v6版本中,Route更改了使用方式。

//最新语法
   <Routes>
            <Route path="/center" element={<Center />} />
            <Route path='/home' element={< Home/>} />
          </Routes>
          {/* 5版本 */}
          <Route path="/center" component={ Center} />
          <Route path='/home' component={ Home} />

解决
1 切换5点几版本

npm i react-router-dom@5.2.0

2 按照最新语法

原文链接:https://blog.csdn.net/weixin_45663264/article/details/121909850

栏目分类
最近更新