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 ...
随机推荐
- Thymeleaf 公共css,js提取及自有css,js导入
https://www.jianshu.com/p/2102fa4772ba
- Python 3函数的参数冒号注释
Python 3.7版本,函数的参数可以通过冒号来进行注释 def f(ham: str, eggs: str = 'eggs') -> str : print("Annotation ...
- 关于string类型的大小写转换函数
对于string类型的有力工具 transform(s.begin(), s.end(), s.begin(),::tolower)大写转小写 transform(s.begin(), s.end() ...
- 【Spring】AOP实现原理(三):创建代理
AbstractAutoProxyCreator 在AbstractAutoProxyCreator的wrapIfNecessary方法中,调用getAdvicesAndAdvisorsForBean ...
- 论文阅读 Exploring Temporal Information for Dynamic Network Embedding
10 Exploring Temporal Information for Dynamic Network Embedding 5 link:https://scholar.google.com.sg ...
- arcgis创建postgre企业级数据库
什么是企业级地理数据库? 企业级地理数据库(ArcSD Enterprise,sde)是和 arcGIS 套件集成程度最高的地理数据库:创建时需要用到安装 arcGIS Server 时的 [ecp ...
- java 改变图片的DPI
代码如下: public class test01 { private static int DPI = 300; public static void main(String[] args) { S ...
- UML之顺序图(时序图)
1 顺序图 1.1 顺序图的概念 顺序图(sequence diagram): 用来描述为了完成确定事务,对象之间按照时间消息交互的顺序关系. 1.2 顺序图样式和元素 (1) 对象及命名 (2) 生 ...
- c# 通过反射,字符串 转换 类
eg:已经知道字符串 "userInfo"是一个表名,并且在代码中也有自己的userInfo类,如何把这个字符串"userInfo" 转换成类, "u ...
- CF484A Bits
CF484A Bits 题目 https://codeforces.com/problemset/problem/484/A 题解 思路 知识点:贪心,位运算. 每位独立考虑,要使 \(1\) 的数量 ...