1

My Code Snippet is below:

        <TextInput 
           type="number"
           id="qty"
           defaultValue={qty}
           onChange={checkValue}
           useRef={qty}
        />
        const checkValue= (err) => {     
            err.preventDefault();
            setState({[Id]:Number(err.currentTarget.value)});
        };

        <Button
          type="submit"
          onClick={onSubmit}
         >
               Update
        </Button>

        const onSubmit = err => {
            err.preventDefault();
            const items = Items.map(p =>({
                id:p.id
                quantity:state[p.id]?state[p.id]:p.qty
              }));
               list.updateList(items,id);
        };

I have to update the multiple Products Quantity,but with the above code able to to change only one of the product,if the change both the products quantity only 2nd product quantity is getting changed,how to change both the products quantity.if i give only p.qty then even if i change only one product ,both the products updated with same value.Please help on this

Michael Rovinsky
  • 6,807
  • 7
  • 15
  • 30
sai k
  • 13
  • 3

1 Answers1

0

Set an empty object by default in useState hook:

import React, {useState} from 'react';

const MyComponent = (props) => {
  const [values, setValues] = useState({});
  const setSingleValue = (key, value) => setValues({...values, [key]: value});
  . . .

  return (
    . . .
    <TextInput onChange={e => setSingleValue('value-name', e.currentTarget.value)} . . . />
    . . .
  );
}
Michael Rovinsky
  • 6,807
  • 7
  • 15
  • 30