admin管理员组

文章数量:1023758

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
Add a ment  | 

1 Answer 1

Reset to default 0

Pass 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}
/>

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
Add a ment  | 

1 Answer 1

Reset to default 0

Pass 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