|
| 1 | +--- |
| 2 | +id: api-example |
| 3 | +slug: /api-example |
| 4 | +title: API connection - example |
| 5 | +sidebar_position: 3 |
| 6 | +tags: |
| 7 | + - Backend |
| 8 | + - MSW |
| 9 | + - Mocking |
| 10 | + - react-query |
| 11 | + - react-query example |
| 12 | + - axios |
| 13 | + - react native |
| 14 | + - orval |
| 15 | +description: Api example - check how to get data from backend and display it for users |
| 16 | +--- |
| 17 | + |
| 18 | +# Api example |
| 19 | + |
| 20 | +We have prepared example with react query in this file: [src/screens/DataFromBeScreen_EXAMPLE.tsx](https://github.com/binarapps/baca-react-native-template/blob/main/src/screens/DataFromBeScreen_EXAMPLE.tsx) |
| 21 | + |
| 22 | +```tsx |
| 23 | +// This is auto generated by orval scripts |
| 24 | +// success-line |
| 25 | +import { useArticlesControllerFindAll } from '@baca/api/query/articles/articles' |
| 26 | +import { ArticleEntity } from '@baca/api/types' |
| 27 | +import { Loader, Center, Text, Box, Spacer } from '@baca/design-system' |
| 28 | +import { useScreenOptions, useTranslation } from '@baca/hooks' |
| 29 | +import { useCallback } from 'react' |
| 30 | +import { ListRenderItem, FlatList } from 'react-native' |
| 31 | + |
| 32 | +export const DataFromBeScreen_EXAMPLE = () => { |
| 33 | + const { t } = useTranslation() |
| 34 | + |
| 35 | + useScreenOptions({ |
| 36 | + title: t('navigation.screen_titles.data_from_be_screen_example'), |
| 37 | + }) |
| 38 | + |
| 39 | + // success-line |
| 40 | + const { data: articles, isInitialLoading: isInitialLoadingArticles } = |
| 41 | + // success-line |
| 42 | + useArticlesControllerFindAll({ page: 1, pageSize: 10 }) |
| 43 | + |
| 44 | + const renderItem: ListRenderItem<ArticleEntity> = useCallback(({ item: { id, title } }) => { |
| 45 | + return ( |
| 46 | + <Box mb="1" bg="fg.brand.primary" borderRadius={2} m={2}> |
| 47 | + <Text>{'id: ' + id}</Text> |
| 48 | + <Text.MdRegular mb={2}>{'title: ' + title}</Text.MdRegular> |
| 49 | + </Box> |
| 50 | + ) |
| 51 | + }, []) |
| 52 | + |
| 53 | + return ( |
| 54 | + <Box flex={1}> |
| 55 | + <Center flex={1}> |
| 56 | + <Text.XlRegular>THIS IS EXAMPLE SCREEN</Text.XlRegular> |
| 57 | + <Text.XlRegular>which contains data from backend</Text.XlRegular> |
| 58 | + <Spacer y="1" /> |
| 59 | + <FlatList |
| 60 | + ListEmptyComponent={ |
| 61 | + !isInitialLoadingArticles ? ( |
| 62 | + <Center height={400} flex={1}> |
| 63 | + <Loader type="circle" /> |
| 64 | + </Center> |
| 65 | + ) : ( |
| 66 | + <Text>No data found</Text> |
| 67 | + ) |
| 68 | + } |
| 69 | + data={articles} |
| 70 | + renderItem={renderItem} |
| 71 | + /> |
| 72 | + </Center> |
| 73 | + </Box> |
| 74 | + ) |
| 75 | +} |
| 76 | +``` |
0 commit comments