正文从这开始~

总览

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

最新文章

  1. 【FOL】第二周
  2. 一个 Sql语句优化的问题- STATISTICS 统计信息
  3. ural1057 Amount of Degrees
  4. java搭建finagle(1)
  5. Nginx+Tomcat集群部署
  6. Redis & Sentinel 安装脚本
  7. WebSocket能干啥
  8. deflate与gzip
  9. js 计算过去和未来的时间距离现在多少天?
  10. iOS UIBezierPath类 介绍
  11. WebX框架学习笔记之二----框架搭建及请求的发起和处理
  12. MongoDB的常用命令和增查改删
  13. Mac之日常操作
  14. rownum用法
  15. 【Teradata 】TD最大列数
  16. js计算本地时间
  17. H.264开源解码器评测
  18. OO作业总结报告3
  19. java 构造json对象数组
  20. python中split()、os.path.split()函数用法

热门文章

  1. MathType7安装使用及please restart word to load mathtype addin properly的问题
  2. 【题解】Codeforces Round #798 (Div. 2)
  3. Camunda BPM的总体架构介绍
  4. JS:object
  5. ms10_002 IE浏览器漏洞
  6. UiPathExcel写入操作
  7. Linux开放指定端口命令(CentOS)
  8. 利用IDEA搭建SpringBoot项目,整合mybatis
  9. multiset 用法复习
  10. go-zero微服务实战系列(十一、大结局)