Redux窗体预先填充的数据
问题描述:
我使用的是组件的加载形式,我从api响应中获得我的文本字段的值,当我填充并提交这些值时,因为我没有触及域 - 我我看到验证错误 - 就像它们是空的 - 如何将这些现有值传递给道具?Redux窗体预先填充的数据
const emailValfromapi = this.props.data && this.props.data.email ? this.props.data.email : [];
const { handleSubmit, fields: { email, abc, def, etc } } = this.props;
<fieldset className="form-group">
<div className="input-wrapper">
<input value={emailValfromapi} id="email" {...email} type="email" className={email.touched && email.error ? 'error-red' : ''} required />
<label className="input-label" htmlFor="Email">To</label>
{ email.touched && email.error && <div className="alert-danger"> { email.error } </div> }
</div>
</fieldset>
答
在redux-form
你不为自己输入的值传递,而是使用the Field
component包投入,让库处理数据绑定。在这种情况下,输入是填充的,但redux-form
不知道它的存在,因为它没有包含在Field
组件中。这个例子给你一个关于Field
组件的基本用法的简要说明。
此外,要将初始值传递给表单组件,您需要使用initialValues
prop(最好由this example描述)。
希望这会有所帮助!