Skip to content

Instantly share code, notes, and snippets.

@saurabhpati
Created July 15, 2018 12:34
Show Gist options
  • Save saurabhpati/d86cdc19dd49391fe3a713dc7c94cfab to your computer and use it in GitHub Desktop.
Save saurabhpati/d86cdc19dd49391fe3a713dc7c94cfab to your computer and use it in GitHub Desktop.
playing with react
const Result = (props) => {
return (
<div>{props.counter}</div>
);
}
class Button extends React.Component {
handleClick = () => {
this.props.onClickFunction(this.props.incrementValue);
}
render() {
return (
<button type="button" onClick={this.handleClick}>{this.props.incrementValue}</button>
);
}
}
class App extends React.Component {
constructor() {
super();
this.state = {counter : 0};
}
incrementCounter = (incrementBy) => {
this.setState(prevState => ({counter: prevState.counter + incrementBy}));
}
render() {
return (
<div>
<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}/>
</div>
<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}/>
</div>
<Result counter={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