Last active
May 11, 2018 10:08
-
-
Save DmitryMasley/1654f1feee5982e270ad09da15f9bab1 to your computer and use it in GitHub Desktop.
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
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