Created
May 12, 2018 14:24
-
-
Save fede-rodes/565288e496705bc278941d9c0dfc8279 to your computer and use it in GitHub Desktop.
This file contains 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
//-------------------------------------------------------------------- | |
// COMPONENT DEFINITION: | |
//-------------------------------------------------------------------- | |
// See renderProps: https://www.youtube.com/watch?v=BcVAq3YFiuc | |
import React from 'react'; | |
import PropTypes from 'prop-types'; | |
import { Query } from 'react-apollo'; | |
import Text from '../Components/Text'; | |
import CenteredActivityIndicator from '../Components/CenteredActivityIndicator'; | |
const WithQuery = ({ children, ...rest }) => ( | |
<Query {...rest}> | |
{(queryProps) => { | |
if (queryProps.error) { | |
return <Text>Error :( {JSON.stringify(queryProps.error)}</Text>; | |
} | |
if (queryProps.loading) { | |
return <CenteredActivityIndicator />; | |
} | |
return children(queryProps); | |
}} | |
</Query> | |
); | |
WithQuery.propTypes = { | |
children: PropTypes.func.isRequired, | |
}; | |
export default WithQuery; | |
//-------------------------------------------------------------------- | |
// USAGE: | |
//-------------------------------------------------------------------- | |
const SpotsMapScreen = ({ navigation }) => { | |
const handleCardPress = (spotId) => { | |
navigation.navigate('SpotDetailsScreen', { | |
uuid: spotId, | |
}); | |
}; | |
return ( | |
<WithQuery query={GET_SPOTS}> | |
{({ data }) => ( | |
<SpotsMapWithListFallback | |
spots={data.spots} | |
cardComponent={Card} | |
onCardPress={handleCardPress} | |
/> | |
)} | |
</WithQuery> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment