Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot change state of one item across multiple inputs in a form component

I have a form that takes a couple of inputs. To store their states I created an object which works fine for all inputs except the date one.

// Input handler
const [values, setValues] = React.useState({
    applicant: '',
    position: '',
    startSalary: '',
    date: null,
    months: 0,
    increase: 0,
    interval: "",
    method: "",
});

const handleChange = (prop) => (event) => {
    setValues({ ...values, [prop]: event.target.value });
    console.log(values)
};


..

{/* Months Period */}
<div className="flex">
    <div className="input-wrapper flex justify-between w-full items-center">
        <FormControl sx={{ width: '95%', marginRight: '20px' }}>
          <InputLabel htmlFor="outlined-adornment-amount">Months</InputLabel>
          <OutlinedInput
            id="outlined-adornment-amount"
            value={values.months}
            onChange={handleChange('months')}
            startAdornment={<InputAdornment position="start">n</InputAdornment>}
            label="Months"
            type="number"
          />
        </FormControl>
    </div>
</div>

{/* Date Picker */}
<div className="input-wrapper flex justify-between items-center">
    <LocalizationProvider dateAdapter={AdapterMoment}>
      <DatePicker
        label="Start Date"
        value={values.date}
        onChange={handleChange('date')}
        renderInput={(params) => <TextField {...params} />}
      />
    </LocalizationProvider>
</div>
..

When I change the date on the client side it returns enter image description here

However, this works isolated for the datepicker:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

...
const [value, setValue] = React.useState(null);

<div className="input-wrapper flex justify-between items-center">
    <LocalizationProvider dateAdapter={AdapterMoment}>
      <DatePicker
        label="Start Date"
        value={value}
        onChange={(newValue) => {
          setValue(newValue);
        }}
        renderInput={(params) => <TextField {...params} />}
      />
    </LocalizationProvider>
</div>
...

>Solution :

By seeing above code, seems like DatePicker isn’t returning you an event object.

Simply check, by doing a console.log in your handleChange method.
Also,you can add if checks to manage such cases.

const handleChange = (prop) => (event) => {
    console.log(event);
    if(prop === 'date') {   
      setValues({ ...values, [prop]: event});
    } else {
       setValues({ ...values, [prop]: event.target.value });
    }
    console.log(values)
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading