Created
May 8, 2019 05:28
-
-
Save gaperton/bd2c11afb5e6631049fa9af3a9c3e521 to your computer and use it in GitHub Desktop.
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 UsersList = ({ filter, $selected }) => { | |
// $users can be modified by async function after the component is unmounted. | |
// We have to do something to prevent an exception. Let's do it in this custom hook. | |
const $users = useSafeLink([]); | |
// It's useful to know if there's an I/O peding. Another custom hook. | |
const ioComplete = useIO( async () => { | |
// This thing can happen after unmount. | |
$users.set( await fetchUsers( filter ) ); | |
}, [ filter ]); | |
return ( | |
<ul className="users-suggestions"> | |
{ ioComplete ? $users.value.map( user => ( | |
<li key={user.id} | |
className={ $selected.value && $selected.value.id === user.id ? 'selected' : '' } | |
onMouseDown={ () => $selected.set( user ) } | |
> | |
{ userToString( user ) } | |
</li> | |
)) : 'Loading...' } | |
</ul> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment