【发布时间】:2016-09-30 17:33:18
【问题描述】:
我正在尝试使用处于状态的选项创建动态选择。
我尝试了两件事:
- 我尝试使用无状态函数创建自定义选择器。我将具有可以用选项填充选择的数据的道具传递给函数。像这样:
const renderFieldSelect = ({ name, label, templateList}) => {
if(templateList && templateList.entities) {
return (
<div>
<div className="col-md-4" >
<label>{label}</label>
</div>
<div className="col-md-4">
<Field
className="form-control"
name={name}
component="select"
>
{
templateList.result.map((type, index) =>
return <option value={templateList.entities.template[type].id}>{templateList.entities.template[type].name}</option>
)
}
</Field>
</div>
</div>
)
}else {
return <div></div>
}
//... more stuff
class Bla extends Component {
//... more stuff
render(){
return (
<div>
<Field
component={renderFieldSelect}
name='templateList'
label='label'
templateList={this.props.templateList}
/>
<Field
component="input"
name='addNewOption'
type='text'
/>
<button onClick={addOption}>Add new option</button>
</div>
)
}
}
但是当我更新模板列表(使用输入和按钮)时,对象不会用新的 选项(我觉得很奇怪)。我知道 addOption 函数正在工作,因为它通过它的 reducer
- 我试图用 FieldArray 创建类似的东西,但我不知道如何更新 fields 参数。文档不太清楚这个道具有/可能有什么。另外,我也不知道你是否可以创建一个“字段”并只返回 the 或类似的东西。
【问题讨论】:
标签: reactjs redux redux-form