Last active
December 20, 2018 15:02
-
-
Save kaw2k/498789365b9b1dde2ed06761e3f237bf 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
type Loading<T extends object> = | |
| { loading: true } | |
| { loading: false; error: true } | |
| ({ loading: false; error: false } & T) | |
function usePlayer(id: PlayerId): Loading<{ data: Player }> { | |
const { value, loading, error } = useDocument(database.players.doc(id)) | |
if (loading) return { loading: true } | |
if (error) return { loading: false, error: true } | |
return { loading: false, error: false, data: value.data() } | |
} | |
const renderPlayer: React.SFC<{ id: PlayerId }> = ({ id }) => { | |
const player = usePlayer(id) | |
if (player.loading) return <div>Loading...</div> | |
if (player.error) return <div>error...</div> | |
return <div>welcome, {player.data.name}!</div> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment