Skip to content

Instantly share code, notes, and snippets.

@mrron313
Created November 13, 2019 05:09
Show Gist options
  • Save mrron313/ed72211d0ae053e7f34f67ae1a19a56b to your computer and use it in GitHub Desktop.
Save mrron313/ed72211d0ae053e7f34f67ae1a19a56b to your computer and use it in GitHub Desktop.
update state onchange select dropdown in react js
// Update state
constructor() {
super();
this.state = {
location: 'Bangladesh' // By default it will be set to "Bangladesh"
}
this.handleLocationChange = this.handleLocationChange.bind(this);
}
handleLocationChange = (e) => {
this.setState({ location: e.target.value });
}
// select drop down part in render
<select onChange={ (e) => this.handleLocationChange(e) } class="custom-select" id="location">
<option selected value="Bangladesh">Bangladesh</option>
<option value="India">India</option>
<option value="UK">UK</option>
</select>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment