Created
August 13, 2017 11:55
-
-
Save koslib/7fd9133c7fea13f360c12b3b6465105d to your computer and use it in GitHub Desktop.
React.js Simple Components demo
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 Button extends React.Component { | |
handleClick = () => { | |
this.props.onClickFunction(this.props.incrementValue) | |
} | |
render() { | |
return ( | |
<button onClick={this.handleClick}> | |
+{this.props.incrementValue} | |
</button> | |
); | |
} | |
} | |
const Result = (props) => { | |
return <div>{props.currentCounter}</div> | |
} | |
class App extends React.Component { | |
state = {counter: 0}; | |
incrementCounter = (incrementValue) => { | |
this.setState((prevState) => ({ | |
counter: prevState.counter + incrementValue | |
})); | |
}; | |
render() { | |
return ( | |
<div> | |
<Button incrementValue={1} onClickFunction={this.incrementCounter}/> | |
<Button incrementValue={5} onClickFunction={this.incrementCounter}/> | |
<Button incrementValue={10} onClickFunction={this.incrementCounter}/> | |
<Button incrementValue={100} onClickFunction={this.incrementCounter}/> | |
<Result currentCounter={this.state.counter}/> | |
</div> | |
); | |
} | |
} | |
ReactDOM.render(<App />, mountNode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment