Redux Form、Radio Button Fields,如何支持可变值?
Redux Form, Radio Button Fields, how to support variable values?
在我的 React redux 表单中,我有以下内容:
<fieldset className="form-group">
<legend>Radio buttons</legend>
{this.props.job_titles.map(jobTitle => (
<div className="form-check" key={jobTitle.id}>
<label className="form-check-label">
<Field
name="job_title_id"
component="input"
type="radio"
value={jobTitle.id}
/>
{' '}
{jobTitle.title}
</label>
</div>
))}
</fieldset>
这会正确呈现单选按钮,但是当您单击 select 单选按钮时,单选按钮永远不会设置为 selected。您不能 select 选项 - 表格已损坏。
奇怪的是,如果我将 value={jobTitle.id}
更新为 value="anything"
,那么单选按钮可以 selected。
我在 redux 表单文档中没有看到任何关于动态生成的单选按钮的内容。我做错了什么?
谢谢
将选中的 属性 设置为 state
或 prop
,然后在点击处理程序中更新它。
<Field
onClick={
() => {
this.setState((prevState) => {
return {isChecked: !prevState.isChecked};
});
}
}
name="job_title_id"
component="input"
type="radio"
checked={this.state.isChecked}
value={jobTitle.id}
/>
将值转换为字符串:
<Field
name="job_title_id"
component="input"
type="radio"
value={jobTitle.id.toString()}
/>
在我的 React redux 表单中,我有以下内容:
<fieldset className="form-group">
<legend>Radio buttons</legend>
{this.props.job_titles.map(jobTitle => (
<div className="form-check" key={jobTitle.id}>
<label className="form-check-label">
<Field
name="job_title_id"
component="input"
type="radio"
value={jobTitle.id}
/>
{' '}
{jobTitle.title}
</label>
</div>
))}
</fieldset>
这会正确呈现单选按钮,但是当您单击 select 单选按钮时,单选按钮永远不会设置为 selected。您不能 select 选项 - 表格已损坏。
奇怪的是,如果我将 value={jobTitle.id}
更新为 value="anything"
,那么单选按钮可以 selected。
我在 redux 表单文档中没有看到任何关于动态生成的单选按钮的内容。我做错了什么?
谢谢
将选中的 属性 设置为 state
或 prop
,然后在点击处理程序中更新它。
<Field
onClick={
() => {
this.setState((prevState) => {
return {isChecked: !prevState.isChecked};
});
}
}
name="job_title_id"
component="input"
type="radio"
checked={this.state.isChecked}
value={jobTitle.id}
/>
将值转换为字符串:
<Field
name="job_title_id"
component="input"
type="radio"
value={jobTitle.id.toString()}
/>