簡體   English   中英

react-hook-form:如何顯示嵌套名稱的錯誤?

[英]react-hook-form: how to show errors for nested names?

我在 react-hook-form 中有一個register("test.new",{required:true})

現在如何顯示test.new錯誤

{errors.test.new && <p style={{ color: "yellow" }}>This field is required</p>}

不管用

在你輸入任何東西之前,沒有錯誤,所以errors對象沒有任何屬性,你需要添加一個可選的鏈接運算符來安全地獲取嵌套屬性,以防它未定義:

{errors.test?.new && (
  <p style={{ color: 'red' }}>This field is required</p>
)}

現場演示

代碼沙盒演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM