Skip to content

Instantly share code, notes, and snippets.

@gaperton
Created May 8, 2019 05:28
Show Gist options
  • Save gaperton/bd2c11afb5e6631049fa9af3a9c3e521 to your computer and use it in GitHub Desktop.
Save gaperton/bd2c11afb5e6631049fa9af3a9c3e521 to your computer and use it in GitHub Desktop.
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