Home > OS >  How to add different events with this Class |REACT JS|
How to add different events with this Class |REACT JS|

Time:10-02

I wrote down the code below.

My outcome should be 4 buttons that increment and decrement a value.

Is working but all buttons change at the same time!

The outcome I would like to get button by button and not at the same time.

I've already tried with an Array but seems I'm not on the right way!


import React from 'react';

class Counter extends React.Component {
    constructor() {
        super();

        this.state = {
            cnt: 0

        };
    }


    handleDecrement = () => {
        this.setState({
            cnt: this.state.cnt   1
        });
    }

    handleIncrement = () => {
        this.setState({
            cnt: this.state.cnt - 1
        });
    }




    
    render() {

        

        return (

            
                
            <><div className = "btn"></div>
                <header>
                        <h1>Tarantino Shop</h1>
                </header>
                    <div>
                    <img src= "images/walltara.png" alt="cart" width = "80%"/>
                    </div>
                    
                    <div className="divprimario">

                    <div className="items">
                        <img src= "images/tara1.jpg" alt="cart" />  
                        <div className = "titles"> T-Shirt Pulp Fiction</div>
                        
                        <div>
                            <button onClick={this.handleDecrement}> </button>
                            <p>{this.state.cnt} </p>
                            <button onClick={this.handleIncrement}>-</button>
                        </div>  
                    </div> 
                    

                    <div className="items">
                        <img src= "images/tara2.jpg" alt="cart" />
                        <div className = "titles">T-Shirt Tarantino </div> 
                        <div>
                            <button onClick={this.handleDecrement}> </button>
                            <p>{this.state.cnt} </p>
                            <button onClick={this.handleIncrement}>-</button>
                        </div>
                    </div>


                    <div className="items">
                        <img src= "images/tara3.jpg" alt="cart" />
                        <div className = "titles">T-Shirt Le Iene</div> 
                        <div>
                            <button onClick={this.handleDecrement}> </button>
                            <p>{this.state.cnt} </p>
                            <button onClick={this.handleIncrement}>-</button>
                        </div>
                    </div>


                    <div className="items">
                        <img src= "images/tara4.jpg" alt="cart" />
                        <div className = "titles">T-Shirt Random</div> 
                        <div>
                            <button onClick={this.handleDecrement}> </button>
                            <p>{this.state.cnt} </p>
                            <button onClick={this.handleIncrement}>-</button>
                        </div>
                    </div>   


                    </div>

                    </>



                    );
            }
        }


export default Counter;
                                    

So, why all the buttons change at the same time? What am I'm doing wrong?

CodePudding user response:

you are only using one variable cnt to keep track of the count. If you want them to update separately, each button must increment or decrement a different state variable.

For example you could use pulpFictionCnt, tarantinoCnt etc to keep track of the different counts.

CodePudding user response:

Keep a separate component for your Counter and provide other data as props.

import React from "react";

class Counter extends React.Component {
  constructor() {
    super();

    this.state = {
      cnt: 0
    };
  }

  handleDecrement = () => {
    this.setState({
      cnt: this.state.cnt   1
    });
  };

  handleIncrement = () => {
    this.setState({
      cnt: this.state.cnt - 1
    });
  };

  render() {
    return (
      <>
        <div className="divprimario">
          <div className="items">
            <img src="images/tara1.jpg" alt="cart" />
            <div className="titles">{this.props.title}</div>

            <div>
              <button onClick={this.handleDecrement}> </button>
              <p>{this.state.cnt} </p>
              <button onClick={this.handleIncrement}>-</button>
            </div>
          </div>
        </div>
      </>
    );
  }
}

export default Counter;

Following may be some other component,

import { StrictMode } from "react";
import ReactDOM from "react-dom";

import Counter from "./Counter";

const rootElement = document.getElementById("root");
ReactDOM.render(
  <StrictMode>
    <div>
      <div className="btn"></div>
      <header>
        <h1>Tarantino Shop</h1>
      </header>
      <div>
        <img src="images/walltara.png" alt="cart" width="80%" />
      </div>
      <Counter title={"T-Shirt Pulp Fiction"} />
      <Counter title={"T-Shirt Tarantino"} />
      <Counter title={"T-Shirt Le Iene"} />
      <Counter title={"T-Shirt Random"} />
    </div>
  </StrictMode>,
  rootElement
);

Sandbox code here => https://codesandbox.io/s/laughing-bhabha-zsyvw?file=/src/Counter.js

  • Related