Skip to content

Instantly share code, notes, and snippets.

@joeSaad
Created November 1, 2016 18:07
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 joeSaad/6ac82b6d4c63fe45751e60936696145e to your computer and use it in GitHub Desktop.
Save joeSaad/6ac82b6d4c63fe45751e60936696145e to your computer and use it in GitHub Desktop.
Incrementer Component React
class Form extends React.Component {
constructor(props) {
super(props);
this.state = {value: 0};
this.handleChange = this.handleChange.bind(this);
this.incrementValue = this.incrementValue.bind(this);
this.decrementValue = this.decrementValue.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
incrementValue(event) {
if (this.state.value < this.props.max){
this.state.value++;
this.setState({value: this.state.value});
}
}
decrementValue(event) {
if (this.state.value > this.props.min){
this.state.value--;
this.setState({value: this.state.value});
}
}
render() {
return (
<div>
<input
type="text"
value={this.state.value}
onChange={this.handleChange}
min="0" max="10"
/>
<button onClick={this.incrementValue}>
+
</button>
<button onClick={this.decrementValue}>
-
</button>
</div>
);
}
}
ReactDOM.render(
<Form min="0" max="20" />,
document.getElementById('root')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment