admin管理员组文章数量:1432343
I am using react-select in a child ponent but wanting to store the Select value in a state in the parent.
I appreciate that I have to store onChange in state and then pass this to the Select value, but I would like to keep this temporary onChange state in the child ponent not the parent.
So the question is how do I set the parent state (ing in on a prop) from the onChange state in the child.
Parent Component
campaignName (campaign){
//will setState here
console.log(campaign)
}
...
<FormGroup>
<Campaign
campaignName={this.campaignName.bind(this)}
campaignOptions={code in here that sets option}
/>
</FormGroup>
Child Component
updateValue (newValue) {
this.setState({
selectValue: newValue,
// how do I set the parent state from this
});
<Select
onChange={this.updateValue}
value={this.props.campaignName}
options={this.props.campaignOptions}
/>
I am using react-select in a child ponent but wanting to store the Select value in a state in the parent.
I appreciate that I have to store onChange in state and then pass this to the Select value, but I would like to keep this temporary onChange state in the child ponent not the parent.
So the question is how do I set the parent state (ing in on a prop) from the onChange state in the child.
Parent Component
campaignName (campaign){
//will setState here
console.log(campaign)
}
...
<FormGroup>
<Campaign
campaignName={this.campaignName.bind(this)}
campaignOptions={code in here that sets option}
/>
</FormGroup>
Child Component
updateValue (newValue) {
this.setState({
selectValue: newValue,
// how do I set the parent state from this
});
<Select
onChange={this.updateValue}
value={this.props.campaignName}
options={this.props.campaignOptions}
/>
Share
Improve this question
edited Dec 6, 2017 at 9:40
Nick Wild
asked Dec 6, 2017 at 8:21
Nick WildNick Wild
5751 gold badge12 silver badges27 bronze badges
1 Answer
Reset to default 0Pass the function directly from parent to child that will handle the onChange:
Parent Component
handleSelect(e) {
this.setState({
selectValue: e.target.value,
});
}
render() {
<ChildComponent selectOnChange={this.handleSelect} />
}
Child Component
<Select
onChange={this.props.selectOnChange}
value={this.props.campaignName}
/>
OR (if you want the method to stay on the child)
updateValue(e) {
this.props.selectOnChange(e); //call the passed props here
}
<Select
onChange={this.updateValue}
value={this.props.campaignName}
/>
本文标签: javascriptreactselect handle onChange with propsStack Overflow
版权声明:本文标题:javascript - react-select handle onChange with props - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745602160a2665629.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论