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 ...
随机推荐
- fpn(feature-Pyramid-network)学习笔记
FPN(特征金字塔网络)学习笔记 论文 在物体检测里面,有限计算量情况下,网络的深度(对应到感受野)与 stride 通常是一对矛盾的东西,常用的网络结构对应的 stride 一般会比较大(如 32) ...
- 使用argparse进行调参
argparse是深度学习项目调参时常用的python标准库,使用argparse后,我们在命令行输入的参数就可以以这种形式python filename.py --lr 1e-4 --batch_s ...
- 二叉树遍历在Unity中的实现
前言:今天放一天,想到要放国庆假了就心烦气躁,躺床上又焦虑,回想起面试官的一副扑克脸,马上跳起来看了看数据结构. 今天复习了二叉树,包括一些基本概念和特性,当看到二叉树遍历的章节时,马上联想到了Uni ...
- CF335E Counting Skyscrapers 题解
提供一种最劣解第一且巨大难写的做法( Bob 显然真正的楼量可以达到 \(314!\),是没办法直接做的,再加上唯一方案的样例,可以猜测有简单的结论. 考虑当楼高度为 \(k(k<h)\) 时, ...
- python 企业微信发送文件
import os import json import urllib3 class WinxinApi(object): def __init__(self,corpid,secret,chatid ...
- Nginx安装及支持https代理配置和禁用TSLv1.0、TSLv1.1配置
Linux安装Nginx Nginx安装及支持https代理配置和禁用TSLv1.0.TSLv1.1配置. 下载安装包 [root@localhost ~]# wget http://nginx.or ...
- Ubuntu 配置 .NET 使用环境
本文迁移自Panda666原博客,原发布时间:2021年3月29日. 说明 测试使用的环境 Linux版本:Ubuntu Server 20.04 LTS x64 .NET SDK版本:5.0 其他版 ...
- [pwn基础]动态链接原理
目录 [pwn基础]动态链接原理 动态链接概念 动态链接调用so例子 GOT(全局偏移表) got表劫持小实验 PLT(延迟绑定) PLT概念 延迟绑定(PLT表) 实战学习 [pwn基础]动态链接原 ...
- Vue.js与Node.js一起打造一款属于自己的音乐App(收藏)
更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/118755888
- Json多层级动态结构数据解析
一.工具 (1)GSON Google Gson是一个简单的基于Java的库,用于将Java对象序列化为JSON,反之亦然. 它是由Google开发的一个开源库. 以下几点说明为什么应该使用这个库 - ...