Skip to content

Instantly share code, notes, and snippets.

@elie222
Created August 18, 2019 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elie222/23ce601ea43bd240ea7c1de85a76df90 to your computer and use it in GitHub Desktop.
Save elie222/23ce601ea43bd240ea7c1de85a76df90 to your computer and use it in GitHub Desktop.
Using GraphQL code generator
import React from 'react'
import { Text, View, TouchableOpacity, ImageBackground, ScrollView } from 'react-native'
import gql from 'graphql-tag'
import { FlatGrid } from 'react-native-super-grid'
import { useItemsQuery } from '../../generated/graphql'
export const ITEMS_QUERY = gql`
query Items($communityId: String!) {
items(communityId: $communityId) {
_id
name
price
userId
communityId
imageUrl
locationLng
locationLat
}
}
`
const Items = (props: { navigation: any }) => {
const communityId = props.navigation.getParam('communityId')
const communityName = props.navigation.getParam('communityName')
const { loading, error, data } = useItemsQuery({
variables: {
communityId,
},
})
if (!data) return null
return (
<ScrollView>
<Text>{communityName}</Text>
<FlatGrid
items={data.items}
renderItem={({ item }) => (
<TouchableOpacity
onPress={() => {
props.navigation.navigate('Item', {
item,
})
}}
>
<ImageBackground
imageStyle={{ borderRadius: 10 }}
source={{ uri: item.imageUrl }}
>
<Text>{item.name}</Text>
</ImageBackground>
</TouchableOpacity>
)}
/>
</ScrollView>
)
}
export default Items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment