React报错之You provided a `checked` prop to a form field
2023-02-18 16:41:39 时间
正文从这开始~
总览
当我们在多选框上设置了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
。
相关文章
- [PHP] php使用curl_multi_exec并行同时执行http请求
- [PHP] PHP redis滑动窗口频率限制
- [PHP] xml转为数组函数
- [MySQL] 解决Error 1698: Access denied for user 'root'@'localhost'
- [MySQL] 注意MySQL5.1不支持utf8mb4 Error 1115: Unknown character set: 'utf8mb4'
- [日常] 前端资源测试机上忽略版本号的的nginx配置
- [PHP]PHP不支持方法重载和只支持方法覆盖
- [MySQL]varchar和char的的区别是什么
- [操作系统] 字节序中的大端序和小端序的区别
- [MySQL]事务的MVCC原理与幻读
- [PHP] PHP PDO与mysql的连接单例防止超时情况处理
- [PHP]使用preg_replace来转换手机号中间四位为* 或者进行复杂字符替换
- [PHP]抽象类和接口的区别
- [Php] windows下使用composer出现SHA384 is not supported by your openssl extension
- [labuladong算法小抄]手把手带你刷二叉树(第一期)
- [PHP] 新版本PHP7.4与新版本MySQL8认证问题
- [PHP] error_reporting(0)可以屏蔽Fatal error错误
- [PHP] PDO对象与mysql的连接超时
- [uniapp] 解决推送时报错plus not defined
- [PHP] time_wait与长连接短连接