網(wǎng)站首頁 編程語言 正文
解決React報錯You?provided?a?`checked`?prop?to?a?form?field_React
作者:Borislav?Hadzhiev ? 更新時間: 2022-12-29 編程語言總覽
當(dāng)我們在多選框上設(shè)置了checked
屬性,卻沒有onChange
處理函數(shù)時,會產(chǎn)生"You provided a?checked
?prop to a form field without an?onChange
?handler"錯誤。為了解決該錯誤,可以使用defaultChecked
屬性,或者在表單字段上設(shè)置onChange
屬性。
這里有個例子用來展示錯誤是如何發(fā)生的。
// 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
表單上設(shè)置了checked
屬性,但卻沒有提供onChange
事件處理器。這使得input
的checked
屬性成為靜態(tài)的。
defaultChecked
解決該錯誤的一種方式是,使用defaultChecked
屬性取而代之。
export default function App() { return ( <div> <input type="checkbox" id="subscribe" name="subscribe" defaultChecked={true} /> </div> ); }
defaultChecked
屬性為多選框設(shè)置了一個初始值,但是該值不是靜態(tài)的,是可以被更改的。
defaultChecked
屬性常被用于不受控(不被開發(fā)者控制)的多選框。這意味你必須使用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> ); }
每當(dāng)你點擊按鈕時,多選框的checked
值就會被打印到控制臺上。
onChange
或者,我們可以在input
表單字段上設(shè)置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
的狀態(tài)變量中。
我們在多選框上設(shè)置了onChange
屬性,所以每當(dāng)值改變時,handleChange
函數(shù)就會被調(diào)用。
我們可以通過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
,所以復(fù)選框的初始值將是checked
。
翻譯原文鏈接:bobbyhadz.com/blog/react-…
原文鏈接:https://juejin.cn/post/7135818724287709192
相關(guān)推薦
- 2022-07-13 SpringBoot中的SmartInitializingSingleton接口的使用
- 2022-10-17 詳解Go語言中單鏈表的使用_Golang
- 2022-06-29 python人工智能tensorflow常見損失函數(shù)LOSS匯總_python
- 2022-07-27 python如何為list實現(xiàn)find方法_python
- 2022-04-05 android使用shape自定義Switch組件
- 2022-05-27 C++?超詳細分析數(shù)據(jù)結(jié)構(gòu)中的時間復(fù)雜度_C 語言
- 2022-07-22 Math.ceil() 函數(shù)使用介紹
- 2022-08-28 C#中類的使用教程詳解_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支