Created
September 15, 2022 17:39
-
-
Save newerton/823fc5ca9950135f8b8109c34f5073de to your computer and use it in GitHub Desktop.
Apply Single responsibility principle (SRP)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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