[英]Giving a default value to redux-form Field in a FieldArray when adding new Fields
It used to be in earlier versions of redux-form that you could pass an initialValue
prop directly to a field as opposed to passing an initialValues
value in your mapStateToProps
function. 过去在redux-form的早期版本中,您可以将initialValue
mapStateToProps
直接传递给字段,而不是在mapStateToProps
函数中传递initialValues
值。 Now it seems you can only do the latter. 现在看来您只能做后者。
I was wondering how I would set the default value of a newly pushed field in my field array to something like "Field No.2" (I would use the index passed to the field to get the number, I'm more stumped on how to pass the default value.) 我想知道如何将字段数组中新推送的字段的默认值设置为“ Field No.2”(我会使用传递给该字段的索引来获取数字,我更困惑于如何传递默认值。)
You can pass the initial value when you push the new entry to your array. 将新条目推送到数组时,可以传递初始值。 Given the example in the redux-form documentation: 给定redux-form文档中的示例:
const renderSubFields = (member, index, fields) => (
<li key={index}>
<button
type="button"
title="Remove Member"
onClick={() => fields.remove(index)}
/>
<h4>Member #{index + 1}</h4>
<Field
name={`${member}.firstName`}
type="text"
component={renderField}
label="First Name"
/>
<Field
name={`${member}.lastName`}
type="text"
component={renderField}
label="Last Name"
/>
</li>
)
const renderMembers = ({ fields }) => (
<ul>
<button type="button" onClick={() => fields.push({ firstname: 'field 1 firstname', lastname: 'field 1 lastname' })}>
Add Member
</button>
{fields.map(renderSubFields)}
</ul>
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.