Skip to content

Instantly share code, notes, and snippets.

@b3b00
Created January 16, 2019 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b3b00/5bb3f14449de6bfd3bf9db1c0d20e48d to your computer and use it in GitHub Desktop.
Save b3b00/5bb3f14449de6bfd3bf9db1c0d20e48d to your computer and use it in GitHub Desktop.
react - class
<div id="root"></div>
class Composant extends React.Component {
constructor(props) {
super(props);
this.state = {
message : "Hello"
};
this.onClickGoodBye = this.onClickGoodBye.bind(this);
}
render() {
return (
<div>
<h1>{this.state.message} {this.props.toto} !</h1>
<button type="button" class="btn btn-dark btn-block" onClick={this.onClickGoodBye}>toggle</button>
</div>);
}
onClickGoodBye = event => {
//alert("current state = >"+this.state.message+"<")
var newMessage = "Bye"
switch(this.state.message) {
case "Bye" : {
newMessage = "Hello"
break;
}
case "Hello" : {
newMessage ="Bye"
}
}
this.setState({message : newMessage});
};
}
ReactDOM.render(
<Composant toto="bidule"/>, document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment