Created
October 23, 2018 16:48
-
-
Save valex/2acb358b7c6e87aaa3eaed0cbccbef53 to your computer and use it in GitHub Desktop.
select
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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