I am currently trying to make an application within React that accepts a form. This form contains input fields that the user can type directly into and this will generate a state change in the formInput object:
const [formInput, updateFormInput] = useState({
name: "",
job: "",
salary: "",
});
<input
placeholder="Name?"
onChange={(e) =>
updateFormInput({ ...formInput, name: e.target.value })
}
/>
<input
placeholder="Salary?"
onChange={(e) =>
updateFormInput({ ...formInput, job: e.target.value })
}
/>
<input
placeholder="Job?"
onChange={(e) =>
updateFormInput({ ...formInput, salary: e.target.value })
}
/>
I would now like to introduce an autocomplete feature based on some data, when the user presses the button, I would like that formUptdate object to change to the values of a new object and the auto complete to be represented within the fields on screen.
<button onClick={() => {
handleClick();
}}
>
Autofill
</button>
const handleClick = () => {
updateFormInput({
...formInput,
name: "Me",
job: "My Job",
salary: "My Salary",
});
};
Above is the new logic I have attempted, however when I click this button it does not trigger the expected results and my input fields remain the same.
Does anyone know why this might be?