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 ...
随机推荐
- 第06组 Beta冲刺 (5/5)
目录 1.1 基本情况 1.2 冲刺概况汇报 1.郝雷明 2. 方梓涵 3.曾丽莉 4.黄少丹 5. 董翔云 6.鲍凌函 7.杜筱 8.詹鑫冰 9.曹兰英 10.吴沅静 1.3 冲刺成果展示 1.1 ...
- liunx 服务器下面安装mysql8.0
闲来无事,准备自己搭建一个服务器高点事情,不可避免的就是需要使用到mysql数据库了.在Linux系统安装MySQL8.0,网上已经有很多的教程了,到自己安装的时候却发现各种各样的问题,现在把安装过程 ...
- 重载overload 、重写override
观点:重载和重写完全没有关系要联系到一起,唯一的联系就是他们都带有个'重'字,所以鄙人也随大流把他们放在了一起 注意:下面可复制的代码是正确的,错误的只会上传图片,不上传可复制的代码 重载 1.在同一 ...
- node.js环境安装及环境变量
1.nodejs官网下载对应系统的安装包 2.除了你想自定义安装的路径其他一切一直点next往下走 3.打开cmd命令窗口输入node -v,看到v.xx.xx代表node已经装好 node -v 4 ...
- 使用PowerShell安装MySQL
更新记录 2022年4月16日:本文迁移自Panda666原博客,原发布时间:2021年7月10日. 2022年4月16日:更新MySQL下载链接. 一.说明与准备工作 根据MySQL官网提供的安装M ...
- 第二章、DHCP原理与配置
目录 一.了解DHCP服务 1DHCP概述: 2DHCP好处 3DHCP的分配方式 二.DHCP工作过程 DHCP租约过程 三.使用 DHCP动态配置主机地址 1DHCP服务优点 2可分配的地址信息主 ...
- BUUCTF-神秘龙卷风
神秘龙卷风 通过提示知道压缩包密码是四位纯数字,通过爆破得到 得到一串编码 看样子应该是brainfuck编码 flag{e4bbef8bdf9743f8bf5b727a9f6332a8}
- 在两台配置了Win10操作系统的电脑之间直接拷贝文件
前提条件:需要一根网线 每台电脑需手动设置IP地址 设置IP地址随意,示例为:10.10.2.11 和 10.10.2.12 每台电脑需关闭Windows防火墙 测试网络是否连通 方式一 远程桌面连接 ...
- linux系统漏洞lynis扫描
1.下载lynis https://cisofy.com/downloads/lynis/ 或者 yum --enablerepo=epel -y install lynis 2. rz上传压缩包解压 ...
- W10修改被改的默认打开文本方式
今天不小心给默认方式打开点错了,身为处女座的我有洁癖非要非恢复过来,这是找到的 原文操作出处:win10系统如何还原文件默认打开方式?win10设置文件默认打开方式的方法 -Win7系统之家 (win ...