React报错之You provided a `checked` prop to a form field
正文从这开始~
总览
当我们在多选框上设置了checked 属性,却没有onChange 处理函数时,会产生"You provided a checked prop to a form field without an onChange handler"错误。为了解决该错误,可以使用defaultChecked 属性,或者在表单字段上设置onChange 属性。
这里有个例子用来展示错误是如何发生的。
// App.js
export default function App() {
// ️ Warning: You provided a `checked` prop to a form field
// without an `onChange` handler. This will render a read-only field.
// If the field should be mutable use `defaultChecked`.
// Otherwise, set either `onChange` or `readOnly`.
return (
<div>
<input type="checkbox" id="subscribe" name="subscribe" checked={true} />
</div>
);
}
上述代码片段的问题在于,我们在input表单上设置了checked属性,但却没有提供onChange事件处理器。这使得input的checked属性成为静态的。
defaultChecked
解决该错误的一种方式是,使用defaultChecked属性取而代之。
export default function App() {
return (
<div>
<input
type="checkbox"
id="subscribe"
name="subscribe"
defaultChecked={true}
/>
</div>
);
}
defaultChecked属性为多选框设置了一个初始值,但是该值不是静态的,是可以被更改的。
defaultChecked属性常被用于不受控(不被开发者控制)的多选框。这意味你必须使用ref或者表单元素来访问表单字段的值。
// App.js
import {useRef} from 'react';
// ️ Example of uncontrolled checkbox
export default function App() {
const ref = useRef(null);
const handleClick = () => {
console.log(ref.current.checked);
};
return (
<div>
<input
ref={ref}
type="checkbox"
id="subscribe"
name="subscribe"
defaultChecked={true}
/>
<button onClick={handleClick}>Click</button>
</div>
);
}
每当你点击按钮时,多选框的checked值就会被打印到控制台上。
onChange
或者,我们可以在input表单字段上设置onChange属性,并处理事件。
import {useState} from 'react';
export default function App() {
const [isSubscribed, setIsSubscribed] = useState(false);
const handleChange = event => {
setIsSubscribed(event.target.checked);
// ️ this is the checkbox itself
console.log(event.target);
// ️ this is the checked value of the field
console.log(event.target.checked);
};
return (
<div>
<input
type="checkbox"
id="subscribe"
name="subscribe"
onChange={handleChange}
checked={isSubscribed}
/>
</div>
);
}
我们做的第一件事是将input的checked值存储在一个叫做isSubscribed的状态变量中。
我们在多选框上设置了
onChange属性,所以每当值改变时,handleChange函数就会被调用。
我们可以通过event对象上的target属性来访问多选框。类似地,我们可以通过event.target.checked来访问多选框的值。
初始值
如果你想为多选框提供一个初始值,只需将它传递给useState()钩子。
import {useState} from 'react';
export default function App() {
// ️ set checked to true initially
const [isSubscribed, setIsSubscribed] = useState(true);
const handleChange = event => {
setIsSubscribed(event.target.checked);
// ️ this is the checkbox itself
console.log(event.target);
// ️ this is the checked value of the field
console.log(event.target.checked);
};
return (
<div>
<input
type="checkbox"
id="subscribe"
name="subscribe"
onChange={handleChange}
checked={isSubscribed}
/>
</div>
);
}
我们向useState钩子传递了true,所以复选框的初始值将是checked。
React报错之You provided a `checked` prop to a form field的更多相关文章
- vue 表单校验报错 "Error: please transfer a valid prop path to form item!"
vue 表单校验报错 "Error: please transfer a valid prop path to form item!" 原因:prop的内容和rules中定义的名称 ...
- react.js Warning: Failed form propType: You provided a value prop to a form field without an onChange handler. This will render a read-only field.
错误信息: eact.js:20483 Warning: Failed form propType: You provided a value prop to a form field without ...
- react 报错的堆栈处理
react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...
- Navicat 用ssh通道连接时总是报错 (报错信息:SSH:expected key exchange group packet form serve
转:https://blog.csdn.net/qq_27463323/article/details/76830731 之前下了一个Navicat 11.0 版本 用ssh通道连接时总是报错 (报错 ...
- 【spring data jpa】使用jpa的@Query,自己写的语句,报错:org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'status' cannot be found on null
报错: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'status' ...
- 【spring data jpa】jpa中使用in查询或删除 在@Query中怎么写 ,报错:org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'goodsConfigUid' cannot be found on null 怎么处理
示例代码如下: @Modifying @Transactional @Query("delete from GoodsBindConfigMapping gbc " + " ...
- React报错 :browserHistory doesn't exist in react-router
由于版本问题,React中history不可用 import { hashHistory } from 'react-router' 首先应该导入react-router-dom包: import { ...
- react报错 TypeError: Cannot read property 'setState' of undefined
代码如下: class test extends Component { constructor(props) { super(props); this.state = { liked: false ...
- React报错之Cannot find name
正文从这开始~ .tsx扩展名 为了在React TypeScript中解决Cannot find name报错,我们需要在使用JSX文件时使用.tsx扩展名,在你的tsconfig.json文件中把 ...
随机推荐
- python:selenium测试登录在chrome中闪退
问题描述:使用selenium.webdriver时测试网页,进行自动登录测试总是在登录成功时闪退.使用指定驱动器位置的方式chrome也会闪退 1.正常使用chrome驱动打开一个网页,正常访问 f ...
- Swoole一键操作基于阿里云的RDS数据库迁移+OSS文件搬迁
传统的数据库搬迁思路是把数据库表的结构及数据都查询出来,然后通过循环进行数据结构重组拼接.然后导出!数据量少的话,这样当然是没毛病.当数据量太大的时候,服务器的内存开销就吃不住了,很容易炸掉,导致服务 ...
- Git出现“filename too long”错误处理
更新记录 本文迁移自Panda666原博客,原发布时间:2021年5月8日. 怎么肥事? Windows系统下,在Git使用过程中,出现"filename too long"错误提 ...
- python PIL 图片素描化
from PIL import Image import numpy as np a = np.asarray(Image.open("D://7.jpg").convert('L ...
- JavaScript产生随机颜色
//获取rgb类型的颜色 IE7不支持 function randomColor(){ var r = Math.floor(Math.random()*256); var g = Math.floo ...
- BPC 脚本
// //税款计算(现金流) // //2018年5月11日修改,去除之前的送数逻辑,新增BADI计算相关科目 // *SELECT(%010804ZH%, "ID", " ...
- 不同network中的两个docker容器
1. 创建docker网络 docker network create --subnet 172.18.0.1/16 test docker network ls 2. 创建两个容器指定docker ...
- Oracle创建用户和表空间
一.概述 1.数据库实际管理中,不同业务系统需要使用'不同的用户'进行管理维护和使用,这样做把业务数据和系统数据独立分开管理,利于数据库系统管理: 2.在数据库中创建业务系统用户时候,建议为用户创建指 ...
- labview从入门到出家4--用事件结构实现运算功能
使用事件结构可以快速定位响应界面的操作事件,如按下,拖动,双击的事件.基本上我们所要实现的所有功能,都可以通过条件结构+事件结构去实现,比如后面进阶篇将会讲到的状态机就是通过条件结构和事件结构组成的. ...
- HashSet 添加/遍历元素源码分析
HashSet 类图 HashSet 简单说明 HashSet 实现了 Set 接口 HashSet 底层实际上是由 HashMap 实现的 public HashSet() { map = new ...