【发布时间】:2017-10-13 15:56:11
【问题描述】:
我有一个 React Select 表单,在 API 调用后加载了选项。工坊的初始状态值被设置,在表单中点击一个选项后更新。但是,Select 的视图不会更新。更重要的是,当提交表单时,工作坊成功保存到数据库中。我该怎么办?
renderForm() {
return (
<section className="col-md-12 col-sm-12">
<form className="col-md-12 col-sm-12"
onSubmit={ this.handleSubmit }>
// other form-groups
<div className="form-group">
<label>
Region:
<input className="form-control"
type="text"
value={ this.state.value }
onChange={ this.handleRegionChange } required />
</label>
</div>
<div className="form-group">
<label>
Workshop:
</label>
<Select name="form-field-workshop"
value={ this.state.workshop }
onChange={ this.handleWorkshopChange }
options={ this.renderFormWorkshops() }
clearable={ false }
searchable={ false }
required />
</div>
<input className="btn btn-default"
type="submit"
value="Submit" />
</form>
</section>
);
}
// handles the change of state when an option is selected
handleWorkshopChange(value) {
this.setState({
workshop: value.label
});
}
// displays the options in the Select form
renderFormWorkshops() {
return _.map(this.props.workshops.workshops, (it) => {
return (
{ value: it.id, label: it.name }
);
});
}
【问题讨论】:
-
从
value={ this.state.workshop) }这一行删除括号。应该是value={ this.state.workshop }
标签: javascript reactjs lodash react-select