报错信息:

Error: A < Route > is only ever to be used as the child of < Routes > element, never rendereddirectly.Please wrap your < Route> in a < Routes >

原因:

react-router-dom版本 v5 和 v6 改版,使用时候有区别

V6 版本:

import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import routers from './routers';const App = () => {return (<Router><Routes> // 不是老版本的:Switch{routers.map((item, index) => {return (<Routekey={index}exactpath={item.path}element={<item.component />} // 不是老版本的:component 或 render/>);})}</Routes></Router>);
};export default App;

V5 版本:

import React from 'react';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import routers from './routers';const App = () => {return (<Router><Switch>{routers.map((item, index) => {return (<Routekey={index}exactpath={item.path}component={<item.component />}//或://render={() => {//  return <item.component/>;//}}/>);})}</Switch></Router>);
};export default App;

routers 文件夹 index.js

import Inbox from '../views/Inbox';
import About from '../views/About'const routers = [{title: 'About',path: '/',component: About,},{title: 'Inbox',path: '/inbox',component: Inbox,},
];export default routers;

解决办法:

第一种:
使用最新 V6 的语法规则 – 如上图改造

第二种:
更改react-router-dom的版本,改为 V5的

// 命令:如 5.2.0
//npm方式:npm i react-router-dom@5.2.0//cnpm方式:cnpm i react-router-dom@5.2.0//yarn方式:yarn add react-router-dom@5.2.0

到此,结束~~


欢迎有兴趣的前端同学一起讨论交流:152693086

Error: A <Route> is only ever to be used as the child of <Routes> element, never rendereddirectly相关推荐

  1. 关于react-router-dom 6.0.1的更新基本写法 解决Error: A <Route> is only ever to be used as the child of <Routes>

    文章目录 前言 一.先准备好要切换的组件 二.在 V5 版本则是这样使用 v5版本:`Router`直接包裹着`Route组件` 三.在V6版本则是这样使用 v6版本:`Route`需要先被`Rout ...

  2. 关于react-router-dom 6.0.1的基础写法 解决Error A <Route> is only ever to be used as the child of <Routes>

    在新建react项目中 使用react-router-dom 6.0.1版本难免会遇到以下报错 Error: A <Route> is only ever to be used as th ...

  3. 关于react-router-dom 6.0.1的基础写法 解决Error: A <Route> is only ever to be used as the child of <Routes>

    在新建react项目中 使用react-router-dom 6.0.1版本难免会遇到以下报错 Error: A <Route> is only ever to be used as th ...

  4. index.tsx:19 Uncaught Error: A <Route> is only ever to be used as the child of <Routes> element, nev

    今天做练习react路由出现错误,index.tsx:19 Uncaught Error: A is only ever to be used as the child of element, nev ...

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

    错误示例 index.jsx import React from "react"; import ReactDom from "react-dom";impor ...

  6. react-router-dom ^6.0.2使用过程中报错 Error: A <Route> is only ever to be used as the child of <Routes>

    因为react-router-dom更新的之后路由使用时做了一写改变 废话不多说,上代码 旧版本: import React from 'react' import ReactDOM from 're ...

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

    2021年写法 2022年写法

  8. 【无标题】A <Route> is only ever to be used as the child of <Routes> element, never rendered directly. Pl

    版本问题的语法错误:可以选择npm i react-router-dom@5.2.0回到5版本,或者直接用6版本语法 A <Route> is only ever to be used a ...

  9. Reacte路由报错:A <Route> is only ever to be used as the child of <Routes> element, never rendered direct

    由于react-router-dom更新到6版本以后,有一些写法开始与5不同 1.包裹方式不同 A <Route> is only ever to be used as the child ...

最新文章

  1. python获取天气分析_Python爬取南京市往年天气预报,使用pyecharts进行分析
  2. 一套完整的数字无线监控系统需要哪些设备和材料?
  3. Python中将数据矢量化运算所带来的时间加快
  4. 一个教务系统多少钱_销售管理系统报价多少钱
  5. 汇编伪指令EVEN(数据对齐的伪指令,使得下一个变量的起始地址是偶数字节的)
  6. 激进or务实?HEVC、AV1 和私有Codecs现状
  7. Mysql学习总结(63)——Mysql数据库架构方案选择与分析
  8. Ogre:ManualObject
  9. html网页跳转代码大全
  10. 信源编码与信道编码区别(十一)
  11. 区块链赋能数字交通建设 PPT
  12. 第二课:更换国内下载源(阿里源为例)
  13. php解析抖音视频链接,PHP抖音视频无水印解析接口
  14. LATEX编译出现Undefined control sequence.
  15. 盘点:2017年GitHub上30个优秀的机器学习项目
  16. Failed to parse time specification
  17. NV21 to NV12(YUV420SP)
  18. 外贸跨境电商必备九款实用APP软件盘点,外贸人必看
  19. GEE学习笔记:在GEE中下载Sentinel-2影像
  20. python爬虫简单实例-爬取17K小说网小说

热门文章

  1. 浏览器无法上网,其他软件可以
  2. 向NS2中添加协议PING
  3. NAOMI: Non-Autoregressive MultiresolutionSequence Imputation(非自回归多分辨率序列填补)论文详解
  4. 基于分布式的智联招聘数据的大屏可视化分析与预测
  5. 博客添加音乐插件、网站运行时间、文章阅读次数和网站访客统计
  6. 插头DP 概率DP / 期望DP
  7. Pandas基础入门(一)
  8. 百度seo指南_白杨SEO:如何让网页被百度收录以及提高它的排序?
  9. csu1671 经营小卖部(DP 完全背包 好题)
  10. 开题报告:基于java医院在线预约挂号系统 毕业设计论文开题报告模板