const [userDetail, setUserDetail] = useState([])
useEffect(() => {
getUsersData()
}, [])
const getUsersData = async () => {
const userResp = await MyService.getInstance().getEntities()
let data = []
console.log(userResp)
if (userResp) {
userResp.map((d) => {
data.push({
name: d.name,
username: d.username
})
})
setUserDetail(data) // < --------this line--------
console.log(data)
}
console.log(userDetail)
}
setUserDetail 不会设置 userDetail 数组的值,即使 console.log(userDetail) 显示空数组。 像这样:
答案 0 :(得分:0)
两件事:
const[ userDetail , setUserDetail ] = useState([])
useEffect( () => {
getUsersData()
}, [])
const getUsersData = async() =>{
const userResp = await MyService.getInstance().getEntities()
console.log(userResp)
if(userResp){
setUserDetail(
userResp.map((d) => (
{
name:d.name,
username:d.username
})
))
}
}
console.log(userDetail)