user1234
user1234

Reputation: 3159

How to increase/decrease count on click of same button in Js/ReactJs

I want to increment/decrement counter based on button click. Scenario: There is an initial value on the button- say: 5 When a user clicks on the button, it increments to +1 - o/p: 6 however when the user clicks again on the button it, it'll reduce to 5 again. this case I dont have 2 buttons- inc and dec to increase/decrease the count.

code:

class Hello extends React.Component {
constructor() {
   super()
   this.state = {
     count: 10,

   }
 }
 getCount( c ) {

    const clicked = this.state.clicked
if(c == 1){
  this.setState({count: this.state.count +1, clicked: true});
} else {
  this.setState({count: this.state.count -1})
}

  }
  render() {
    return  <div>
                    <button onClick={this.getCount.bind(this, 1)}> click me for increase </button>
                    <button onClick={this.getCount.bind(this, 0)}> click me for decrease </button>
                    <div><h1>{this.state.count}</h1></div>

                     </div>;
  }
}

ReactDOM.render(
  <Hello name="World" />,
  document.getElementById('root')
);

Upvotes: 1

Views: 13637

Answers (3)

Deva
Deva

Reputation: 95

class Home extends Component {
    constructor(props) {
        super(props)
        this.state = {
            value: 0,
        }
    }
    addMore = () => {
        this.setState({
            value:this.state.value+1
        })
    }
    addLess = () => {
        this.setState({
            value:this.state.value > 0 ? this.state.value-1 : 0,
        })
    }
    render() {
        return(
<div className="">
       <div className="d-flex justify-content-center">
       <span className="bg-dark px-3 py-2 text-white br-50" onClick={this.addMore} 
        style={{cursor:'pointer'}}>+</span>
       <input type="text" max="0" min="0" className="mx-3 w-25 text-center" value= 
       {this.state.value}  />
       <span className="bg-dark px-3 py-2 text-white br-50"  onClick={this.addLess} 
       style={{cursor:'pointer'}}>-</span>
       </div>
</div>
        )
    }
}

export default Home;

Upvotes: 1

Senthuran
Senthuran

Reputation: 1837

I have tried the below solution.

class App extends Component{
 constructor(){
   super()
   this.state={
     count:0,
   }
 }

 increment(){
   this.setState(
     {
       count:this.state.count+1
     }
   );
 };

 decrement(){
   this.setState(
     {
    count:this.state.count-1
     }
   );
 };

 reset(){
   this.setState(
     {
       count:0
     }
   )
 }

 render(){
    return(
        <div style={{
          position: 'absolute', left: '50%', top: '50%',
          transform: 'translate(-50%, -50%)'
      }}>
            <h1>Current Count :{this.state.count}</h1>
            <button className="inc"  onClick={(e)=>this.increment(e)}>Increment!</button>
            <button  className="dec" onClick={(e)=>this.decrement(e)}>Decrement!</button>
            <button className="reset" onClick={(e)=>this.reset(e)}>Reset!</button>

        </div>

    );
 }




}

export default App;

In addition to the increment and decrement the above code has a reset button to reset the count to 0.

Upvotes: 1

Cat_Enthusiast
Cat_Enthusiast

Reputation: 15688

You would need an additional state-value to keep track of the status of the button.

class Hello extends React.Component {
   constructor() {
      super()
      this.state = {
        count: 100,
        clicked: false
      }
    }
  getCount() {
    const clicked = this.state.clicked
    if(clicked){
      this.setState({count: this.state.count - 1, clicked: false});
    } else {
      this.setState({count: this.state.count + 1, clicked: true})
    }

  }
  render() {
    return  <div>
                    <button onClick={this.getCount.bind(this)}>Like | {this.state.count}</button>
                </div>;
  }
}

ReactDOM.render(
  <Hello name="World" />,
  document.getElementById('container')
);

Upvotes: 3

Related Questions