網站首頁 編程語言 正文
解決React報錯You?provided?a?`checked`?prop?to?a?form?field_React
作者:Borislav?Hadzhiev ? 更新時間: 2022-12-29 編程語言總覽
當我們在多選框上設置了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
。
翻譯原文鏈接:bobbyhadz.com/blog/react-…
原文鏈接:https://juejin.cn/post/7135818724287709192
相關推薦
- 2023-01-01 c語言之如何求e的近似值_C 語言
- 2022-12-09 PyTorch中關于tensor.repeat()的使用_python
- 2023-02-01 Python局部函數及用法詳解(含nonlocal關鍵字)_python
- 2022-10-18 AJAX請求以及解決跨域問題詳解_AJAX相關
- 2023-10-10 前端根據后端數據生成表格 行列合并 指定表頭
- 2022-07-21 將本地jar添加到Maven倉庫,使用pom.xml引用
- 2022-08-18 C#實現從位圖到布隆過濾器的方法_C#教程
- 2022-07-07 python中list*n生成多維數組與for循環生成多維數組的區別說明_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支