Skip to content

Instantly share code, notes, and snippets.

@valex
Created October 23, 2018 16:48
Show Gist options
  • Save valex/2acb358b7c6e87aaa3eaed0cbccbef53 to your computer and use it in GitHub Desktop.
Save valex/2acb358b7c6e87aaa3eaed0cbccbef53 to your computer and use it in GitHub Desktop.
select
<!DOCTYPE html>
<html>
<head>
<title>defaultValue</title>
<meta charset="utf-8">
</head>
<body>
<div id="app">
<!-- my app renders here -->
</div>
<script charset="utf-8" crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script type="text/babel">
class MySelect1 extends React.Component {
render() {
return (
<select defaultValue="move">
<option value="stay">Should I stay</option>
<option value="move">or should I go</option>
<option value="trouble">If I stay it will be trouble</option>
</select>
)
}
}
class MySelect2 extends React.Component {
constructor(params) {
super(params);
this.state = {
value: 'move'
};
// This binding is necessary to make `this` work in the callback
this._onChange = this._onChange.bind(this);
};
_onChange(event) {
this.setState({value: event.target.value});
};
render() {
return (
<select value={this.state.value} onChange={this._onChange}>
<option value="stay">Should I stay</option>
<option value="move">or should I go</option>
<option value="trouble">If I stay it will be trouble</option>
</select>
)
}
}
class MySelect3 extends React.Component {
render() {
return (
<select defaultValue={["stay", "move"]} multiple={true}>
<option value="stay">Should I stay</option>
<option value="move">or should I go</option>
<option value="trouble">If I stay it will be trouble</option>
</select>
)
}
}
ReactDOM.render(
<div>
<MySelect1 />
<MySelect2 />
<MySelect3 />
</div>
,
document.getElementById('app')
);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment