0

I have a few buttons that increment by one a few separate object properties. Can I use the button name to indicate which object property to increment? Instead of writing the same function for each button?

after a button is clicked, send the button name and add this on to the object name like object.[event.target.name]= object.[event.target.name] +1

Example code

increaseClick = (id, e) => {
        console.log(e.target.name);
        const updatedData = this.state.students.map((student) => {
            if (id === student.id) {
                student.noHW = student.noHW + 1;
            }
            return student;
        });
        this.setState({
            students: updatedData,
        });
    };

// button code

<button name="noHW" onClick={(e) => lowerClick(id, e)}>
                                    -
                                </button>{' '}
                                {noHW}{' '}

can I change student.noHW to something like student.[e.target.name] to get which button was clicked which will correspond to the property I want to add 1 to.

Ferg
  • 1
  • 1

0 Answers0