【发布时间】:2020-09-18 08:32:04
【问题描述】:
尝试将数据绑定到下拉列表,但未绑定任何内容,下拉列表显示 NOTHING SELECTED。
function Drop() {
axios.get('/api/v1/users/')
.then(function (response) {
global_num = response.data;
// console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
let lst = [];
for (let i in global_num) {
lst.push(global_num[i]['username']);
}
return (
<select style={{ width : "300px" , height: "40px", margin:"20px", fontSize:"18px"}} name="res">
{lst?.map((list) => (
<option value={list}> {list} </option>
))}
</select>
);
}
用于将用户名显示为下拉选项的 API 结果
Response from API:
[
{
"id": 1,
"username": "hanwat007@gmail.com",
}
]
【问题讨论】:
-
因为
get是异步的,所以你的返回在api调用完成之前完成。
标签: typescript api react-hooks dropdown react-typescript