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文件中把 ...
随机推荐
- LOJ数列分块 9 题解
\(1.\) 题意 给定一个长度 \(n\) 序列,每次查询区间 \(l, r\) 的众数. \(2.\) 思路 如果边界是 \([l,r]\),\(l\) 在第 \(a\) 块,\(r\) 在第 \ ...
- CF1665A GCD vs LCM
- 1.Shell编程循环语句(if 、while、 until)
循环语句 for循环语句 读取不同的变量值,用来逐个执行同一组命令 格式: for 变量名 in 取值列表 do 命令序列 done 示例:批量创建用户并设置密码 [root@localhost da ...
- Skywalking光会用可不行,必须的源码分析分析 - Skywalking Agent &插件解析
3 Skywalking源码导入 接上文,已经学习了Skywalking的应用,接下来我们将剖析Skywalking源码,深度学习Skywalking Agent. 3.1 源码环境搭建 当前最新版本 ...
- js 循环生成元素,并为元素添加click事件,结果只执行最后一个点击事件
问题描述:有一个参数集合data,for循环为每一个参数生成一个dom元素,并附加onclick事件.生成之后发现点击事件里的参数全是data集合里的最后一个. 代码如下: var dom=$('#d ...
- React中setState方法说明
setState跟新数据是同步还是异步? setState跟新数据是异步的. 如何用代码表现出来是异步的. 点击按钮更新数据,然后去打印这个值看一下 setState跟新数据是异步的 class Fa ...
- 令你瞠目结舌的 Python 代码技巧
0. for-else 在 Python 中,else 不仅可以与 if 搭配使用,还可以与 for 结合. python答疑 咨询 学习交流群2:660193417### for x in rang ...
- Overfitting & Train Set & Test Set
假设数据集是独立同分布的,可以将数据集划分为不同的比例:Train Set and Test Set. 同时在Train Set and Test Set上做精度测试,或者隔一段时间在Test Set ...
- Linux操作系统(4):磁盘分区、挂载
Outline: ① lsblk :查看所有设备挂载情况 ② df -h :查询系统整体磁盘使用情况 ③ du -h /目录 :查询指定目录的磁盘占用情况 ④ mount :查询系统中已经挂载的设备 ...
- Linux操作系统(3):crond 任务调度
crontab 进行 定时任务的设置.概述: 任务调度:是指系统在某个时间执行的特定的命令或程序. 任务调度分类: 1.系统工作:有些重要的工作必须周而复始地执行.如病毒扫描等 2.个别用户工作:个别 ...