Last active
January 3, 2019 01:23
-
-
Save haldarmahesh/2d3500d1813f2820ae92bc74e1902228 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, { Component } from 'react' | |
| export default class Counter extends Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = { | |
| counter: 0 | |
| } | |
| this.handleCounter = this.handleCounter.bind(this); | |
| } | |
| handleCounter(value) { | |
| this.setState({ | |
| counter: value | |
| }); | |
| } | |
| render() { | |
| return ( | |
| <div> | |
| <div> | |
| <button onClick={() => this.handleCounter(this.state.counter + 1)}> + Increment</button> | |
| <button onClick={() => this.handleCounter(this.state.counter - 1)}> - Decrement</button> | |
| </div> | |
| <div> | |
| Current: {this.state.counter} | |
| </div> | |
| </div> | |
| ) | |
| } | |
| } |
Also there is no need for a handleCounter method if you already use a lambda - you could just directly call setState and make the code a lot smaller - this example really is a bad one for comparing classes to hooks >_<
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is recommended to use prevState in setState instead of this.state inside render method.