Skip to content

Instantly share code, notes, and snippets.

@newerton
Created September 15, 2022 17:39
Show Gist options
  • Save newerton/823fc5ca9950135f8b8109c34f5073de to your computer and use it in GitHub Desktop.
Save newerton/823fc5ca9950135f8b8109c34f5073de to your computer and use it in GitHub Desktop.
Apply Single responsibility principle (SRP)
const useUsers = () => {
const [users, setUsers] = useState([])
useEffect(() => {
const loadUsers = async () => {
const response = await fetch('/some-api')
const data = await response.json()
setUsers(data)
}
loadUsers()
}, [])
return { users }
}
const ActiveUsersList = () => {
const { users } = useUsers()
const weekAgo = new Date()
weekAgo.setDate(weekAgo.getDate() - 7)
return (
<ul>
{users.filter(user => !user.isBanned && user.lastActivityAt >= weekAgo).map(user =>
<li key={user.id}>
<img src={user.avatarUrl} />
<p>{user.fullName}</p>
<small>{user.role}</small>
</li>
)}
</ul>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment