Created
July 11, 2019 07:00
-
-
Save hoodwink73/ac2624677b50d234201b79d2242f2490 to your computer and use it in GitHub Desktop.
React Suspense with Apollo Cache (as presented in the talk, State of React by Jared Palmer @reacteurope 2019)
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 UserDetailRepos = ({login}) => { | |
const {data} = useQuery(USER_REPOS, { | |
variables: {login} | |
}) | |
const { | |
user: { | |
reposositories: { nodes } | |
} | |
} = data | |
return ( | |
<>{nodes && nodes.length > 0 ? nodes.map((node) => <Node node={node} /> ) : null}</> | |
) | |
} |
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
export const useQuery = (query, props) => { | |
try { | |
// try to read data from the cache | |
const data = Client.readQuery({query, ...props}) | |
return {data} | |
} catch (error) { | |
throw Client.query({query, ...props}) | |
.then(data => ({data})) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment