-
-
Save joeSaad/6ac82b6d4c63fe45751e60936696145e to your computer and use it in GitHub Desktop.
Incrementer Component React
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
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