正文从这开始~

总览

当我们在多选框上设置了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. EasyExcel-合并单元格

    pom版本 <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</ ...

  2. 竟然还有人说ArrayList是2倍扩容,今天带你手撕ArrayList源码

    ArrayList是我们开发中最常用到的集合,但是很多人对它的源码并不了解,导致面试时,面试官问的稍微深入的问题,就无法作答,今天我们一起来探究一下ArrayList源码. 1. 简介 ArrayLi ...

  3. 第1期 考研中有关函数的一些基本性质《zobol考研微积分学习笔记》

    在入门考研微积分中,我们先复习一部分中学学的初等数学的内容.函数是非常有用的数学工具. 1.函数的性质理解: 首先考研数学中的所有函数都是初等函数.而函数的三个关键就是定义域.值域.对应关系f. 其中 ...

  4. Java中JSON字符串和对象的互转

    对象转换成json字符串: JSONObject.toJSONString(switchmes) JSON字符串转换成对象: Switchmes switchmes=(Switchmes) JSONO ...

  5. LeetCode. 812. 最大三角形面积

    812. 最大三角形面积 鞋带公式 鞋带公式,用于计算任意多边形的面积,可用于计算三角形的面积 已知 ΔABC 三个顶点的坐标 A:(x1,y1). B:(x2,y2). C:(x3,y3) 对应的矩 ...

  6. 【微服务专题之】.Net6下集成消息队列上-RabbitMQ

    ​ 微信公众号:趣编程ACE关注可了解更多的.NET日常实战开发技巧,如需源码 请公众号后台留言 源码;[如果觉得本公众号对您有帮助,欢迎关注] .Net中RabbitMQ的使用 [微服务专题之].N ...

  7. [开源] .Net ORM 访问 Firebird 数据库

    前言 Firebird 是一个跨平台的关系数据库系统,目前能够运行在 Windows.linux 和各种 Unix 操作系统上,提供了大部分 SQL-99 标准的功能.它既能作为多用户环境下的数据库服 ...

  8. 泛型容器类和ArrayList操作

    泛型 比如ArrayList<E> E就是泛型 在没有泛型之前,从集合读取到的每一个对象都必须进行转换,如果有人不小心插入了类型错误的对象,在运行时的转换处理就会出错 有了泛型之后,可以告 ...

  9. 攻防世界 miscmisc

    63.miscmisc(感觉这题挺有意思的,单独拿出来记录一下) 得到一张png,扔进kali中,foremost得到两个zip,打开其中一个,发现一张jpg和一个加密的zip,在jpg中分离出一个z ...

  10. CMU15445 (Fall 2019) 之 Project#3 - Query Execution 详解

    前言 经过前面两个实验的铺垫,终于到了给数据库系统添加执行查询计划功能的时候了.给定一条 SQL 语句,我们可以将其中的操作符组织为一棵树,树中的每一个父节点都能从子节点获取 tuple 并处理成操作 ...