[英]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.