Skip to content

Instantly share code, notes, and snippets.

@guoyunhe
Last active March 13, 2018 13:40
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 guoyunhe/995f79320bce0eba2ad211f4e9bace2f to your computer and use it in GitHub Desktop.
Save guoyunhe/995f79320bce0eba2ad211f4e9bace2f to your computer and use it in GitHub Desktop.
React Code Templates
import React, { Component } from 'react'
class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
name: '',
description: '',
active: false
};
this.onChange = this.onChange.bind(this);
this.onCheck = this.onCheck.bind(this);
this.onSubmit = this.onSubmit.bind(this);
}
onChange(e) {
this.setState({ [e.target.name]: e.target.value });
}
onCheck(e) {
this.setState({ [e.target.name]: !this.state[e.target.name] });
}
onSubmit(e) {
e.preventDefault();
// Do something...
}
render() {
const { title } = this.props;
const { name, description, active } = this.state;
return (
<form className="MyComponent" onSubmit={this.onSubmit}>
<h1>{title}</h1>
<input type="text" name="name" value={name} onChange={this.onChange} />
<input type="text" name="description" value={description} onChange={this.onChange} />
<input type="checkbox" name="active" value={true} checked={active} onClick={this.onCheck} />
</form>
);
}
}
export default MyComponent;
import React, { Component } from 'react'
class MyComponent extends Component {
constructor(props) {
super(props);
}
render() {
const { name, message } = this.props;
return (
<div className="MyComponent">
My component? {name} component.
{message}
</div>
);
}
}
export default MyComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment