正文从这开始~

总览

当我们在多选框上设置了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. django框架6

    内容概要 神奇的双下划线查询 外键字段的创建 外键字段操作 多表查询 基于对象的跨表查询 基于双下划线的跨表查询 双下线查询扩展 如何查看SQL语句 内容详情 神奇的双下划线查询 1.查询年龄大于20 ...

  2. SCI论文写作注意事项

    1. 先写结论:(划定范围,以防添加无效的内容)     并非一开始就把整个结论都写出来,而是把

  3. 线程安全性-原子性之Atomic包

    先了解什么是线程安全性:当多个线程访问某个类时,不管运行时环境采用何种调度方式或者这些进程将如何交替执行,并且在主调代码中不需要任何额外的同步或协同,这个类都能表现出正确的行为,那么就称为这个类是线程 ...

  4. Docker容器配置远程登录

    Docker容器配置远程登录 前言 docker 的网络模式主要有三种,bridge.host.none: pridge是docker安装后自动创建的虚拟网卡,创建容器时默认使用此模式. host是指 ...

  5. 入坑KeePass(二)重置keepass设置

    保留好.kdbx和密钥文件,软件的文件可以删除掉,重新下载并解压设置就恢复默认了

  6. .NET中如何在同步代码块中调用异步方法

    更新记录 本文迁移自Panda666原博客,原发布时间:2021年7月2日. 在同步代码块中调用异步方法,方法有很多. 一.对于有返回值的Task 在同步代码块中直接访问 Task 的 Result ...

  7. C语言学习之我见-strncpy()字符串复制函数(可控制范围)

    strncpy()函数,用于两个字符串值的复制. (1)函数原型 char *strncpy(char * _Dest,const char * _Source,size_t _Count); (2) ...

  8. ElasticSearch7.3学习(三十二)----logstash三大插件(input、filter、output)及其综合示例

    1. Logstash输入插件 1.1 input介绍 logstash支持很多数据源,比如说file,http,jdbc,s3等等 图片上面只是一少部分.详情见网址:https://www.elas ...

  9. 一文搞懂jsBridge的运行机制

    我司的APP是一个典型的混合开发APP,内嵌的都是前端页面,前端页面要做到和原生的效果相似,就避免不了调用一些原生的方法,jsBridge就是js和原生通信的桥梁,本文不讲概念性的东西,而是通过分析一 ...

  10. VM Ware 给Centos虚拟机配置静态IP

    前言:在大家的日常运维工作中,肯定多多少少都会用到虚拟机,但是默认情况下VMware创建的虚拟机每次启动都有可能更换IP这就对我们的工作造成了一定的麻烦,下面我就给大家介绍下,如何给centos虚拟机 ...