let onlineUsers = users.filter(user => user.online);
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
users: [
{
username: '张佳一',
online: true
},
{
username: '马飞国',
online: false
},
{
username: '赵新新',
online: true
},
{
username: '吴明飞',
online: false
},
{
username: '刘国光',
online: true
},
{
username: '何洁',
online: true
}
]
}
}
render() {
const usersOnline = this.state.users.filter(user => user.online === true);
const renderOnline = usersOnline.map(user => <li key={user.username}>{user.username}</li>);
return (
<div>
<h1>当前在线用户:</h1>
<ul>
{renderOnline}
</ul>
</div>
);
}
};
文章转载自路条编程,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。