Elizabeth
Elizabeth

Reputation: 196

React-select clears the value (state) of the other input fields when select option is changed (onChange)

I used React-select for my select on a form, when I click on select and the value of select is changed, the current state/value of the other fields input fields is cleared and onSubmit i only get the value of the selection

   const [values, setValues] = useState({
   username: "",
   email: "",
   time: "",
});

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

const optionsTime = [
   {
       value: "15",
       label: "15 mins",
   },
   {
       value: "30",
       label: "30 mins",
   },
   {
       value: "45",
       label: "45 mins",
   },
   {
       value: "60",
       label: "60 mins",
   },
   {
       value: "75",
       label: "75 mins",
   },
];

const handleSubmit = (e) => {
   e.preventDefault();
   console.log(`Option submited:`, values);
   alert("You are submitting " + values);
};
const handleTime = (e) => {
   //I used created a different onChange function for react-select and used e.value because react select //throws error of undefined id or value when e.target.value is used...
   if (values.time !== e.value) {
       setValues({ time: e.value });
   }

   console.log(`Option selected:`, e, values);
};
return (
   <div className="headerRow">
       <Navbar />
       <div className="col-lg-4 schedule-area d-none d-lg-block">
           <form id="schedule" onSubmit={handleSubmit}>
               <h3>Schedule A Brief</h3>
               <div className="form-group">
                   <input
                       id="username"
                       type="text"
                       placeholder="Contact Person"
                       className="form-control"
                       onChange={handleChange}
                       value={values.username}
                       required
                   />
               </div>
               <div className="form-group">
                   <input
                       id="email"
                       type="email"
                       placeholder="Email Address"
                       className="form-control"
                       onChange={handleChange}
                       value={values.email}
                       required
                   />
               </div>
               <div className="form-group">
                   <Select
                       id="time"
                       type="options"
                       placeholder="Select Brief Time Projection"
                       onChange={handleTime}
                       options={optionsTime}
                       valueInput={values.time}
                       isSearchable
                       required
                   />
               </div>
               <div onClick={handleSubmit} className="book_btn text-center">
                   Schedule a Brief
               </div>
           </form>
       </div>
   </div>
);

Please what can I do to get the values of all my input fields including the select field onSubmit

Upvotes: 1

Views: 962

Answers (2)

Dipesh KC
Dipesh KC

Reputation: 2463

Currently you are returning only time overriding the other old value in a values state

Replace

 const handleTime = (e) => {
     if (values.time !== e.value) {
       setValues({ time: e.value });
   }

To

const handleTime = (e) => {
 if (values.time !== e.value) {
      setValues((old)=>({...old, time: e.value })); }

Upvotes: 1

Elizabeth
Elizabeth

Reputation: 196

I changed

const handleTime = (e) => {
     if (values.time !== e.value) {
       setValues({ time: e.value });
   }

To:

const handleTime = (e) => {
    if (values.time !== e.value) {
      setValues({
        ...values,
        time: e.value
      });
    }

It updtates now...

Upvotes: 0

Related Questions