正文从这开始~

总览

当我们在多选框上设置了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事件处理器。这使得inputchecked属性成为静态的。

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>
);
}

我们做的第一件事是将inputchecked值存储在一个叫做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的更多相关文章

  1. vue 表单校验报错 "Error: please transfer a valid prop path to form item!"

    vue 表单校验报错 "Error: please transfer a valid prop path to form item!" 原因:prop的内容和rules中定义的名称 ...

  2. 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 ...

  3. react 报错的堆栈处理

    react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...

  4. Navicat 用ssh通道连接时总是报错 (报错信息:SSH:expected key exchange group packet form serve

    转:https://blog.csdn.net/qq_27463323/article/details/76830731 之前下了一个Navicat 11.0 版本 用ssh通道连接时总是报错 (报错 ...

  5. 【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' ...

  6. 【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 " + " ...

  7. React报错 :browserHistory doesn't exist in react-router

    由于版本问题,React中history不可用 import { hashHistory } from 'react-router' 首先应该导入react-router-dom包: import { ...

  8. react报错 TypeError: Cannot read property 'setState' of undefined

    代码如下: class test extends Component { constructor(props) { super(props); this.state = { liked: false ...

  9. React报错之Cannot find name

    正文从这开始~ .tsx扩展名 为了在React TypeScript中解决Cannot find name报错,我们需要在使用JSX文件时使用.tsx扩展名,在你的tsconfig.json文件中把 ...

随机推荐

  1. QQ空间未授权评论_已忽略

    看群友们聊天时发现的, 大概是做了查看了动态访问时间的一个设置, 但是仅自己可见的说说还是被评论了的这么一个问题. 闲的没事就翻了一下找一下问题. 这个方法嘎嘎鸡肋, 可以说完全没用, 交到tsrc, ...

  2. windiws下安装Composer

    1.先下载Composer-Setup.exe,下载地址:下载Composer .会自动搜索php.exe的安装路径,如果没有,就手动找到php路径下的php.exe. 2.在PHP目录下,打开php ...

  3. LVGL库入门教程03-布局方式

    LVGL布局方式 LVGL的布局 上一节介绍了如何在 LVGL 中创建控件.如果在创建控件时不给控件安排布局,那么控件默认会被放在父容器的左上角. 可以使用 lv_obj_set_pos(obj, x ...

  4. 程序分析与优化 - 7 静态单赋值(SSA)

    本章是系列文章的第七章,终于来到了鼎鼎大名的SSA,SSA是编译器领域最伟大的发明之一,也是影响最广的发明. 本文中的所有内容来自学习DCC888的学习笔记或者自己理解的整理,如需转载请注明出处.周荣 ...

  5. 对互斥事件和条件概率的相互理解《考研概率论学习之我见》 -by zobol

    1.从条件概率来定义互斥和对立事件 2.互斥事件是独立事件吗? 3.每个样本点都可以看作是互斥事件,来重新看待条件概率 一.从条件概率来定义互斥和对立事件 根据古典概率-条件概率的定义,当在" ...

  6. NET架构师的基本职责

    NET架构师的基本职责1 职责 对本公司大健康平台提出技术研究及可行性报告; 结合需求设计高扩展性.高性能.安全.稳定.可靠的技术系统; 可以通过配置实现业务需求的变化,跟踪并研究***并应用于产品; ...

  7. Vue.js与ElementUI搭建无限级联层级表格组件

    前言 今天,回老家了.第一件事就是回家把大屏安排上,写作的感觉太爽了,终于可以专心地写文章了.我们今天要做的项目是怎么样搭建一个无限级联层级表格组件,好了,多了不多说,赶快行动起来吧!项目一览 到底是 ...

  8. 【python基础】第02回 计算机基础2

    上节内容回顾 1.绝对路径与相对路径 1.路径的概念 用来标识资源的位置 2.绝对路径 类似于全球GPS定位(给到任何人都可以顺利的找到相应的资源) eg: D:\aaa\a.txt 3.相对路径 需 ...

  9. 一个月后,我们又从 MySQL 双主切换成了主 - 从!

    这是悟空的第 157 篇原创文章 官网:www.passjava.cn 你好,我是悟空. 一.遇到的坑 一个月前,我们在测试环境部署了一套 MySQL 高可用架构,也就是 MySQL 双主 + Kee ...

  10. echart图表中y轴小数位数过长展示效果不佳

    业务中后端返回的精密数据,小数过长,导致所有数据差距不大,在图表中显示重合为一条直线 解决方法设置echart的min属性 min: "dataMin", 但是设置了以后又出现了问 ...