React Select 不会更新单击选项时的值
React Select doesn't update value on clicking option
我有一个 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 }
);
});
}
你在你的构造函数中设置正确了吗?这里可能发生了一些上下文问题,这是绑定上下文的正确形式:
constructor(props) {
super(props);
this.handleWorkshopChange = this.handleWorkshopChange.bind(this);
}
如果您不使用箭头函数,则需要为依赖于使用 this
方法的情况绑定上下文。
另外,你这里有一个杂散的括号:value={ this.state.workshop) }
handleWorkshopChange(value) {
this.setState({
workshop: value.value
});
}
我有一个 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 }
);
});
}
你在你的构造函数中设置正确了吗?这里可能发生了一些上下文问题,这是绑定上下文的正确形式:
constructor(props) {
super(props);
this.handleWorkshopChange = this.handleWorkshopChange.bind(this);
}
如果您不使用箭头函数,则需要为依赖于使用 this
方法的情况绑定上下文。
另外,你这里有一个杂散的括号:value={ this.state.workshop) }
handleWorkshopChange(value) {
this.setState({
workshop: value.value
});
}