簡體   English   中英

Formik-如何從組件的道具中設置輸入字段值?

[英]Formik- how to set input field value from component's props?

我正在使用Formik作為我的表單。
其中一個輸入應該由props值填充。
我該怎么做?
這是我的方法:

<input
     type="text"
     id="country"
     readOnly
     value={props.countryCode}
     // {...formik.getFieldProps("country")}
 />    

但它很可能會干擾Formik ,這就是它不起作用的原因。
什么是正確的解決方案?

謝謝!

您可以在 initialValues 中注入 props 值。

將 enableReinitialize 設置為 true。 所以當 props 改變時 Form 會被重新填充。

<Formik
    initialValues={{ country: props.countryCode }}
    enableReinitialize={true}
 />

 <input
     type="text"
     id="country"
     readOnly
     value={values.country}

 />    

暫無
暫無

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

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