Let say I have a useState to store a user object:
const [user, setUser] = useState({
name: "",
age: "",
type: ""
})
and a select box to show the type
<select
className="input"
value={user.type}
onChange={handleType}
>
<option value='A'>A</option>
<option value='B'>B</option>
</select>
I want to call a api to get the user data and the selected option will change to user.type
useEffect(() => {
axios
.get("https://api.test.com/users/1", {})
.then((res) => {
console.log(res);
setUser(res.data.user);
})
.catch((err) => console.log(err));
}, []);
However, the selected option does not change.
2
Answers
You can spread the response like this
I hope my example could help you. if you update data to the value of select, updated data is not included in the options list. the select value will not update.
example