Last active
July 8, 2021 08:24
-
-
Save Siemko/0903d30c3d1c6cab9708063fe332cd85 to your computer and use it in GitHub Desktop.
Counter component in React.js
This file contains 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 { | |
state = { | |
title: "Counter", | |
counter: 0 | |
}; | |
incrementCounter = () => { | |
this.setState(prevState => ({ | |
counter: prevState.counter + 1 | |
})); | |
}; | |
decrementCounter = () => { | |
this.setState(prevState => ({ | |
counter: prevState.counter - 1 | |
})); | |
}; | |
render() { | |
return ( | |
<section className="section"> | |
<div className="container"> | |
<h1 className="title">{this.state.title}</h1> | |
<div className="level"> | |
<div className="level-item"> | |
<button className="button" onClick={this.incrementCounter}> | |
<span className="icon"> | |
<i className="fa fa-plus" /> | |
</span> | |
</button> | |
</div> | |
<div className="level-item"> | |
<h2>{this.state.counter}</h2> | |
</div> | |
<div className="level-item"> | |
<button className="button" onClick={this.decrementCounter}> | |
<span className="icon"> | |
<i className="fa fa-minus" /> | |
</span> | |
</button> | |
</div> | |
</div> | |
</div> | |
</section> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment