,

Managing Counter Increment and Decrement Events in react.js and JavaScript – MNA Web Programming

Posted by

How to increment and decrement counter in React.js

button {
margin: 10px;
}

How to increment and decrement counter in React.js

In this article, we will learn how to create a simple counter in React.js and increment or decrement its value on button click.

class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}

incrementCount = () => {
this.setState({ count: this.state.count + 1 });
}

decrementCount = () => {
this.setState({ count: this.state.count – 1 });
}

render() {
return (

Count: {this.state.count}


);
}
}

ReactDOM.render(
,
document.getElementById(‘app’)
);