Skip to content

Instantly share code, notes, and snippets.

@kevincolten
Created May 30, 2017 00:39
Show Gist options
  • Save kevincolten/2c7b91d15a09c304b34108cd5a3ddd86 to your computer and use it in GitHub Desktop.
Save kevincolten/2c7b91d15a09c304b34108cd5a3ddd86 to your computer and use it in GitHub Desktop.
React Example
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Welcome</title>
</head>
<body>
<div id="welcome">
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.23.1/babel.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/15.5.0/react.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/15.5.0/react-dom.js"></script>
<script type="text/babel" src="./script.js"></script>
</body>
</html>
'use strict';
class Welcome extends React.Component {
constructor() {
super();
this.state = {
name: 'there',
color: 'red'
};
}
changeName = (e) => {
let color;
if (e.target.value.length % 3 === 0 && e.target.value.length % 5 === 0) {
color = 'blue';
} else if (e.target.value.length % 3 === 0) {
color = 'green';
} else if (e.target.value.length % 5 === 0) {
color = 'red';
} else {
color = 'black';
}
this.setState({
name: e.target.value,
color: color
});
}
render() {
return (
<div>
<h1 style={ {color: this.state.color} }>Hello, {this.state.name}!</h1>
<input type="text" onChange={this.changeName} />
</div>
);
}
}
ReactDOM.render(<Welcome />, document.getElementById('welcome'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment