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

199
Views
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:

...
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>
...
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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)
};

PS: Answering to your comment,

This is happening because DatePicker is doing the hard work to extract value from event object.

It is overriding the default onChange event if input

This is demo of how it might be happening:

const DatePicker = (props) => {
const {onChange = ()=> {}} = props;

const handleChange = (event)=> {
    const value = event.target.value;

    onChange(value); // this value you are receiving.

} 

return <input type="date" onChange={handleChange} />


}
about 4 years ago · Juan Pablo Isaza Report

0

DatePicker returns date not an event. You can handle it like this

const handleChange = (prop) => (event) => {
    if(prop === 'date') {
      return setValues({ ...values, [prop]: event });
    }
    setValues({ ...values, [prop]: event.target.value });
};
about 4 years ago · Juan Pablo Isaza Report

0

DatePicker will provide you the array of dates so you can store the dates using the following method:-

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

Moment is the npm package used for changing the date from iso string to normal form and you can also format the dates using this package.

https://www.npmjs.com/package/moment

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!