Skip to content

Instantly share code, notes, and snippets.

@DmitryMasley
Last active May 11, 2018 10:08
Show Gist options
  • Save DmitryMasley/1654f1feee5982e270ad09da15f9bab1 to your computer and use it in GitHub Desktop.
Save DmitryMasley/1654f1feee5982e270ad09da15f9bab1 to your computer and use it in GitHub Desktop.
import React from “react”;
class MyClass extends React.Component {
constructor(props) {
this.state = {
count: 0,
error: null
}
super(props);
}
validate() {
let error = null;
if(this.state.count > 10) {
error = "count is too big";
}
if(this.state.count < 0) {
error = "count can't be less than 0";
}
this.setState({
error
})
}
increaseCount() {
this.setState({
count: this.state.count + 1;
})
this.validate();
}
decreaseCount() {
this.setState({
count: this.state.count - 1;
})
this.validate();
}
render() {
return (<div>
<span>{`Count is: ${this.state.count}`}</span>
<button
onClick={
this.increaseCount();
}>
+
</button>
<button
onClick={
this.decreaseCount();
}
>
-
</button>
{this.state.error ?
<div>{this.state.error}</div>
: null}
</div>)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment