我想在使用 axios 从随机用户 API 获取后显示性别为女性的随机用户
我试过 .filter, .select 但没有用。
componentDidMount() {
axios
.get("https://randomuser.me/api/?results=5")
.filter(function(i, n) {
return n.gender === "female";
})
.then(response =>
response.data.results.map(user => ({
name: `${user.name.first} ${user.name.last}`,
username: `${user.login.username}`,
email: `${user.email}`,
image: `${user.picture.thumbnail}`,
gender: `${user.gender}`
}))
)
.then(users => {
this.setState({
users,
isLoading: false
});
})
.catch(error => this.setState({ error, isLoading: false }));
}
render() {
const { isLoading, users } = this.state;
return (
<React.Fragment>
<h2>Brief list</h2>
<div>
{!isLoading ? (
users.map(user => {
const { username, name, email, image, gender } = user;
return (
<div key={username}>
<p>{name}</p>
<div>
<img src={image} alt={name} />
</div>
<p>{gender}</p>
<p>{email}</p>
<hr />
</div>
);
})
) : (
<p>Loading...</p>
)}
</div>
</React.Fragment>
);
}
}
我只想显示那些女性用户
相关分类