Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

75
Views
Multiple Increments for multiple inputs

Code should increment or decrement his own input, for diameter should work only button for diameter etc. But every click increment all inputs

The problem is every click increment all inputs that i want to change

const [countDiameter,setCountDiameter] = useState(1);
    const [countFriction,setCountFriction] = useState(1);
    const [countRobotsQnt, setCountRobotsQnt] = useState(1);
    const [countSimulationTime, setCountSimulationTime] = useState(1);

    const incrementCount = () =>{
      setCount(countDiameter + 1);
    };

    const decrementCount = (i) =>{
      if(countDiameter>0){setCount(countDiameter -1);}
    };
<div className='inputForm'>
                    <input type="number" id='InputDiameter' pattern="^-?[0-9]\d*\.?\d*$" value = {count}  onChange={(event) =>{ setCount(event.target.value) }}/>
                    <label for="InputDiameter" className='static-value'>w</label>
                  </div>
                  <div className='ButtonCount' onClick={incrementCount}><button type='button'><span className="countText">+</span></button></div>
                  <div className='ButtonCount' onClick={decrementCount}><button type='button'><span className="countText">-</span></button></div>
                </div>

<div className='inputForm'>
                    <input type="number" id='InputNumber' pattern="^-?[0-9]\d*\.?\d*$" value = {count}  onChange={(event) =>{ setCount(event.target.value) }}/>
                    <label for="inputNumber" className='static-value'>qnt</label>
                  </div>
                  <div className='ButtonCount' onClick={incrementCount}><button type='button'><span className="countText">+</span></button></div>
                  <div className='ButtonCount' onClick={decrementCount}><button type='button'><span className="countText">-</span></button></div>
                </div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I am not that familiar with React, but according to the comments above, I would have tried the following:
First, set a generic decrement and increment function that accepts a state and a setter function.

const incrementState = (setter, state)=>{
    setter(state + 1)
}
const decrementState = (setter, state)=>{
    if (state > 0) {
        setter(state - 1);
    }
}

[diameter, setDiameter] = setState(1/*or your desired initial value*/) 

In your HTML code, you can then call your decrement/increment function by passing the above parameters to this function:

<div className='inputForm'>
    <input type="number" id='InputDiameter' pattern="^-?[0-9]\d*\.?\d*$" value={diameter} onChange={(event)=>{
    setDiameter(event.target.value) }}/>
    <label for="InputDiameter" className='static-value'>w</label>
</div>
<div className='ButtonCount' onClick={incrementState(setDiameter,diameter)}><button type='button'><span
            className="countText">+</span></button></div>
<div className='ButtonCount' onClick={decrementState(setDiameter,diameter)}><button type='button'><span
            className="countText">-</span></button></div>
</div>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!