React报错之Functions are not valid as a React child
正文从这开始~
总览
产生"Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render."错误,通常是因为以下两个原因:
- 从
render中返回一个函数引用而不是一个组件。 - 使用 react router 路由作为
<Route path="/about" element={About} />,而不是<Route path="/about" element={<About />} />。
这里有个例子来展示错误是如何发生的。
// App.js
/**
* ️ Functions are not valid as a React child.
* This may happen if you return a Component instead of <Component /> from render.
* Or maybe you meant to call this function rather than return it.
*/
const App = () => {
const getButton = () => {
return <button>Click</button>;
};
// ️ returning function not JSX element from render
return <div>{getButton}</div>;
};
export default App;
上面代码片段的问题在于,我们从render方法中返回getButton函数,而不是返回真正的JSX元素。
调用函数
为了解决这种情况下的错误,我们可以调用该函数。
const App = () => {
const getButton = () => {
return <button>Click</button>;
};
// now returning the actual button
// added parenthesis () to call the function
return <div>{getButton()}</div>;
};
export default App;
通过调用getButton函数,我们返回了button元素从而解决了该错误。
如果你正在尝试渲染一个真正的组件,确保将其用作<Component />而不是Component。
const App = () => {
const Button = () => {
return <button>Click</button>;
};
// Using component as <Button />, not Button
return (
<div>
<Button />
</div>
);
};
export default App;
另一个导致该错误的原因是,当我们为react router 路由传递一个元素时,比如<Route path="/about" element={About} /> 。
// ️ wrong syntax
<Route path="/about" element={About} />
// right syntax
<Route path="/about" element={<About />} />
在 react router v6 中,我们不向 Route 组件传递 children 属性,而是使用 element 属性。例如,<Route path="/about" element={<About />} />。
当使用react router时,请确保将应该为特定路由渲染的组件作为<Component />,而不是Component。
总结
可以通过以下两种方式解决错误:
- 从
render中返回组件而不是函数。 - 传递给路由中
element属性的是<Component />,而不是Component。
React报错之Functions are not valid as a React child的更多相关文章
- React报错之Objects are not valid as a React child
正文从这开始~ 总览 当我们尝试在JSX代码中,直接渲染对象或者数组时,会产生"Objects are not valid as a React child"错误.为了解决该错误, ...
- react 报错的堆栈处理
react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...
- vue 表单校验报错 "Error: please transfer a valid prop path to form item!"
vue 表单校验报错 "Error: please transfer a valid prop path to form item!" 原因:prop的内容和rules中定义的名称 ...
- DRDB报错------0: Failure: (119) No valid meta-data signature found.
一. 错误 drbdadm create-md datadrbdadm up data <--启动时报错 [root@data-- ~]# drbdadm up data : Failure: ...
- MYSQL安装报错 -- 出现Failed to find valid data directory.
运行环境:windows10数据库版本:mysql.8.0.12安装方式:rpm包直接安装 问题描述:mysql初始化的时候找不到对应的数据库存储目录 报错代码: 2018-10-13T03:29:2 ...
- 不修改系统日期和时间格式,解决Delphi报错提示 '****-**-**'is not a valid date and time
假如操作系统的日期格式不是yyyy-MM-dd格式,而是用strtodate('2014-10-01')) 来转换的话,程序会提示爆粗 '****-**-**'is not a valid date ...
- azure iothub create-device-identity样例报错: unable to find valid certification path ,及iothub-explorer Error: CERT_UNTRUSTED
https://docs.microsoft.com/zh-cn/azure/iot-hub/iot-hub-java-java-getstarted 在IDEA中执行上述的代码,会出现下面的报错信息 ...
- React报错 :browserHistory doesn't exist in react-router
由于版本问题,React中history不可用 import { hashHistory } from 'react-router' 首先应该导入react-router-dom包: import { ...
- Linux安装Apache报错:Cannot find a valid baseurl for repo: base/7/x86_64解决方案
最近使用CentOS7学习,安装安装Apache时候,使用yum安装Apache报错:本文适合CentOS7和RHEL7 # yum install httpd 出现:cannot find a va ...
随机推荐
- Kafka到底有多高可靠?(RNG NB)
在聊Kafka高可靠之前,先在评论区来波RNG NB好不好! 什么叫可靠性? 大家都知道,系统架构有三高:「高性能.高并发和高可用」,三者的重要性不言而喻. 对于任意系统,想要同时满足三高都是一件非常 ...
- 《HALCON数字图像处理》第一、二章笔记
目录 第一章 绪论 1.1 图像和图像处理 1.1.1 图像 1.1.2 数字图像 1.1.3 图像处理及其发展过程 1.2 数字图像处理的步骤和方法 1.3 数字图像处理系统的硬件组成 1.4 数字 ...
- Linux Cgroup v1(中文翻译)(4):Block IO Controller
Block IO Controller 1 概览 cgroup子系统blkio实现了block io控制器.无论是对存储结构上的叶子节点和还是中间节点,它对各种IO控制策略(proportional ...
- KALI2020忘记用户名和密码
时隔半年,打开kali发现忘记了自己精心研制的用户名密码......... 第一步 在开机的时候就按e键进入如下界面 第二步 用键盘上的上下箭头↑↓进行屏幕滚动,滑到最后一行发现修改目标 倒数第四行: ...
- Hash表、 继承
Hash表 我们来了解什么是Hash表?? 要想知道什么是哈希表,那得先了解哈希函数 二叉平衡树 红黑树 B B+树,它们的查找都是先从根节点进行查找,从节点取出数据或索引与查找值进行比较.那么,有没 ...
- 实现领域驱动设计 - 使用ABP框架 - 通用准则
在进入细节之前,让我们看看一些总体的 DDD 原则 数据库提供者 / ORM 无关性 领域和应用程序层应该与 ORM / 数据库提供程序 无关.它们应该只依赖于 Repository 接口,而 Rep ...
- arcgis创建postgre企业级数据库
什么是企业级地理数据库? 企业级地理数据库(ArcSD Enterprise,sde)是和 arcGIS 套件集成程度最高的地理数据库:创建时需要用到安装 arcGIS Server 时的 [ecp ...
- oracle备份数据库数据及导入数据库
1.oracle数据库备份和导入 bat 脚本 scott oracle数据库用户名称 123456 数据库scott用户下的密码 192.168.124.8 本电脑IP orcl 为oracle库 ...
- 人人都能学会的 Python 多线程指南~
大家好鸭!有没有想我~(https://jq.qq.com/?_wv=1027&k=rX9CWKg4) 在 Python 中,多线程最常见的一个场景就是爬虫,例如这样一个需求,有多个结构一样的 ...
- Effective C++阅读笔记 较详细 复杂条款带样例
一.让自己习惯C++ 条款01:视C++为一个语言联邦 C++可视为: C:以C为基础. 面向对象的C++:添加面向对象特性. 模板C++:泛型编程概念,使用模板. STL:使用STL的容器.迭代器. ...