简体   繁体   中英

Fetch initial value from Drop Down list in react.js

I have a drop down list of countries which is populated using an API like below

<select 
  name="country"
  onChange={this.handleChange}
  className="form-control"
 > 
 <option value="" disabled>Select a Country</option>
   {rows} //populated using an API & has a defult value
</select>

I am using handleChange function to collect values like below

handleChange = event => {
    this.setState({ [event.target.name]: event.target.value });
  };

Drop down list has a default value. I would like to collect that value if user don't change any value.

I'd suggest storing that default value in the state

state = {
  country: <DEFAULT_VALUE>
}

...

render() {
  return (
    <select
      ...
      value={this.state.country}
    >
      ...
    </select>
  )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM