giantmario
giantmario

Reputation: 35

My app crashes when I use for or while loops

I started learning reactjs. I am making this really simple app. So I made a decrement button but I want this button to work only when the count of an items is bigger or equals to zero. I tried using while and for loop but my app crashes when I use the loops. Any tips? Thanks!

Here is my code:

import React, { Component } from 'react';

class Cart extends Component {
  state = { 
    count: 0,
    message: ""
   };

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

   handelDecrement = () => {
     while(this.state.count > 0){
       this.setState({count: this.state.count - 1})
     }
   }

   handleStopShopping = () => {
     this.setState({message: this.state.message + "Thank you for your trust! Come back again."})
   }

  render() { 
    return ( 
      <div>
        <h5>Use plus sign to add items to your cart, or use the minus sign to delete items from your cart.</h5>
        {/*Printing the count*/}
        <span className = {this.getBadgeClasses()}>{this.showCount()}</span>
        {/*Increment Button*/}
        <button onClick = {this.handleIncrement} className = {this.incrementButton()}>+</button>
        {/*Decrement Button*/}
        <button onClick = {this.handelDecrement} className = {this.decrementButton()}>-</button> <br></br>

        {/*Info about how much items is in the cart*/}
        <h5><p className = "badge badge-info">{this.itemInfo()}</p></h5>

        <button onClick = {this.handleStopShopping} className = "btn btn-danger btn-sm">Stop Shopping</button> <br></br>
        <h5><p className = "badge badge-dark">{this.state.message}</p></h5>
      </div>
     );
  }

  showCount(){
    let {count} = this.state;
    return count <= 0 ? count = "Zero" : count;
  }

  incrementButton(){
    let btnClasses = "btn m-2 btn-sm btn-";
    btnClasses += this.state.count > 0 && this.state.count < this.state.count ? "dark" : "outline-dark";
    return btnClasses;
  }

  decrementButton(){
    let btnClasses = "btn btn-sm btn-";
    btnClasses += this.state.count === 0 && this.state.count < this.state.count ? "dark" : "outline-dark";
    return btnClasses;
  }

  itemInfo(){
    let itemMessage = "You have " + this.showCount() + " item/s in your cart";
    return itemMessage;
  }

  getBadgeClasses(){
    let badgeClasses = "badge m-2 badge-";
    badgeClasses += this.state.count <= 0 ? "warning" : "primary";
    return badgeClasses;
  }
}

export default Cart

Upvotes: 0

Views: 558

Answers (1)

DTul
DTul

Reputation: 679

Lets say count = 5.

If you are using

while(this.state.count > 0){
    this.setState({count: this.state.count - 1})
}

it will execute the code in the block 5 times before finishing.

What you are looking for is an if statement.

if(this.state.count > 0){
    this.setState({count: this.state.count - 1})
}

ALSO Calling setState in your loop happens async so count could be any number between 1 and 5 and be called a lot of times, which is probably causing the crashes

Upvotes: 3

Related Questions