Slide 1

Slide 1 text

React Native for better or worst !

Slide 2

Slide 2 text

"

Slide 3

Slide 3 text

Radoslav Stankov @rstankov rstankov.com

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

# Side project

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

- implement a mobile app $ - one developer (me) ! - support iOS % / Android & - ship it as fast as possible '

Slide 10

Slide 10 text

Going native (swift/kotlin) wasn't an option

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

( backed by Google ) uses Dart * cross platform + compiles to native machine code , custom components - battery included

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

. backed by Facebook / uses Javascript (or TypeScript) 0 uses React 1 cross platform 2 minimal 3 native and JS bridge 4 native components

Slide 18

Slide 18 text

React Native in 2019 was 5 React Native in 2021 was 6 React Native in 2022 was 7 React Native in 2023 still is 7

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

React Native is fast enough has a rich ecosystem Flutter is faster has more batteries included I have done 3 React Native apps previously I know React/TypeScript/GraphQL extremely well I don't know Dart I don't know Flutter

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Expo in 2020 was ... 8 Expo in 2020 was ... 9 Expo in 2022 was ... 7 Expo in 2023 is ...:

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

$ Tech Stack

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Architecture

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

0 Make common operations easy 4 Have good code organization 1 Isolate dependencies + Extensibility and reusability

Slide 33

Slide 33 text

0 Make common operations easy 4 Have good code organization 1 Isolate dependencies + Extensibility and reusability

Slide 34

Slide 34 text

2 1 3 Support Components Screens

Slide 35

Slide 35 text

Support Components Screens

Slide 36

Slide 36 text

1) Support 2) Components 3) Screens

Slide 37

Slide 37 text

▾ src/ ▸ components/ ▸ hooks/ ▸ screens/ ▸ styles/ ▸ translations/ ▸ types/ ▸ utils/ config.ts routes.ts app.json App.tsx

Slide 38

Slide 38 text

▾ src/ ▸ components/ ▸ hooks/ ▸ screens/ ▸ styles/ ▸ translations/ ▸ types/ ▸ utils/ config.ts routes.ts app.json App.tsx 1 Support Components Pages 2 3

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

3 Screens

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

▾ src/ ▸ components/ ▸ hooks/ ▸ screens/ ▸ styles/ ▸ translations/ ▸ types/ ▸ utils/ config.ts routes.ts app.json App.tsx

Slide 43

Slide 43 text

▾ src/ ▸ components/ ▸ hooks/ ▸ screens/ ▸ styles/ ▸ translations/ ▸ types/ ▸ utils/ config.ts routes.ts app.json App.tsx

Slide 44

Slide 44 text

{ "expo": { "name": "Angry Building", "version": "1.48.0", "orientation": "portrait", "icon": "./assets/icon.png", "splash": { "image": "./assets/splash.png", "resizeMode": "contain", "backgroundColor": "#252629" } }, // ... }

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

▾ src/ ▸ components/ ▸ hooks/ ▸ screens/ ▸ styles/ ▸ translations/ ▸ types/ ▸ utils/ config.ts routes.ts app.json App.tsx

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

@react-navigation/native

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

... I still don't use it, because it was released fairly recently ; ... however the way the app is setup, transition should be easy <

Slide 52

Slide 52 text

▾ src/ ▸ components/ ▸ hooks/ ▸ screens/ ▸ styles/ ▸ translations/ ▸ types/ ▸ utils/ config.ts routes.ts app.json App.tsx

Slide 53

Slide 53 text

import screens from 'src/screens'; import { NavigationContainer } from '@react-navigation/native'; import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; import { createStackNavigator } from '@react-navigation/stack'; Sentry.init(); const RootStack = createStackNavigator(); export default function App() { return (

Slide 54

Slide 54 text

export default function App() { return ( ); } 14 pt

Slide 55

Slide 55 text

const Tab = createBottomTabNavigator(); function TabsScreen() { return (

Slide 56

Slide 56 text

/> ); }

Slide 57

Slide 57 text

const ScreenStack = createStackNavigator(); function Screen(props) { const initialRoute = props.route.params.initialRoute; return ( ); }

Slide 58

Slide 58 text

▾ src/ ▸ components/ ▸ hooks/ ▸ screens/ ▸ styles/ ▸ translations/ ▸ types/ ▸ utils/ config.ts routes.ts app.json App.tsx

Slide 59

Slide 59 text

import apartment from './apartment'; import home from './home'; // ... export default { apartment, home, // ... };

Slide 60

Slide 60 text

Screen States Layout

Slide 61

Slide 61 text

Loading State Screen States Layout

Slide 62

Slide 62 text

Loading State Error State Screen States Layout

Slide 63

Slide 63 text

Loading State Not Found Error Server Error Authorization Error Authentication Error Error State Screen States Layout

Slide 64

Slide 64 text

Loading State Not Found Error Server Error Authorization Error Authentication Error Error State Loaded State Screen States Layout

Slide 65

Slide 65 text

Loading State Not Found Error Server Error Authorization Error Authentication Error Error State Loaded State render Screen States Layout

Slide 66

Slide 66 text

interface IOptions { name: string; query?: DocumentNode; queryVariables?: object | ((params: P) => object); queryRefreshOnShow?: boolean; component: IComponent; type?: keyof typeof layouts; headerTitle?: ITranslation; background: IBackground; } interface IScreen { name: string; options: StackNavigationOptions; component: any; } export default function createScreen(options: IOptions): IScreen // ... }

Slide 67

Slide 67 text

screens/[name]/index.ts (createScreen) -> screens/index.ts -> app.ts

Slide 68

Slide 68 text

export default createScreen({ name: 'home', type: 'plain', query: QUERY, background: 'black', component({ data, fetchMore, refetch }) { usePushNotificationsRegister(); usePushNotificationHandle(data.viewer); return ( ); }, });

Slide 69

Slide 69 text

▾ src/ ▾ screens/ ▾ home/ ▸ Status/ background.png BuildingApartmentsList.tsx Header.tsx index.tsx Query.ts SelectEntrancePicker.tsx

Slide 70

Slide 70 text

http://graphql.org/

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

graphql-codegen --config codegen.yml

Slide 73

Slide 73 text

components/ApartmentStatus/Fragment.tsx import { gql } from '@apollo/client'; export default gql` fragment IApartmentStatusFragment on Apartment { id number name overdueAmount } `;

Slide 74

Slide 74 text

types/grahpql.ts export type IApartmentStatusFragment = { __typename: 'Apartment'; id: string; number?: string | null; name?: string | null; overdueAmount: number; };

Slide 75

Slide 75 text

import { IApartmentStatusFragment } from '~/types/graphql';

Slide 76

Slide 76 text

import { gql } from '@apollo/client'; import IHomeScreenStatusFragment from './Status/Fragment'; import IApartmentStatusFragment from 'src/components/ApartmentStatus/Fragment'; export default gql` query IHomeScreen($cursor: String) { building { id name apartments(first: 100, after: $cursor) { nodes { id name floor isViewerSelected ...IApartmentStatusFragment } pageInfo { hasNextPage endCursor } } ...IHomeScreenStatusFragment } } ${IHomeScreenStatusFragment} ${IApartmentStatusFragment} `; screens/home/Query.ts

Slide 77

Slide 77 text

Query Fragment Fragment Fragment Fragment Fragment Fragment

Slide 78

Slide 78 text

Screen Component Component Component Component Component Component

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

▾ src/ ▸ components/ ▸ hooks/ ▸ screens/ ▸ styles/ ▸ translations/ ▸ types/ ▸ utils/ config.ts routes.ts app.json App.tsx

Slide 81

Slide 81 text

import routes from '~/routes'; 
 routes.back; routes.home; routes.bulletinBoardPostUpdate(post);

Slide 82

Slide 82 text

Slide 83

Slide 83 text

import { useNavigation } from '@react-navigation/native'; import screens from 'src/screens'; type IScreenName = keyof typeof screens; export type IRoute = | { transition: 'goBack' } | { screen: IScreenName; params?: IParams; root?: boolean } | { screen: 'buildingTab' | 'apartmentTab' | 'cashReserveTab' | 'issuesTab'; params?: IParams; root: true; }; const routes = { back: { transition: 'goBack' } as IRoute, home: { root: true, screen: 'buildingTab' } as IRoute, // ... bulletinBoardPostUpdate(post: { id: string }) { return { screen: 'bulletinBoardPostUpdate', params: { post }, } as IRoute; }, };

Slide 84

Slide 84 text

import { useNavigation } from '@react-navigation/native'; import screens from 'src/screens'; type IScreenName = keyof typeof screens; export type IRoute = | { transition: 'goBack' } | { screen: IScreenName; params?: IParams; root?: boolean } | { screen: 'buildingTab' | 'apartmentTab' | 'cashReserveTab' | 'issuesTab'; params?: IParams; root: true; }; const routes = { back: { transition: 'goBack' } as IRoute, home: { root: true, screen: 'buildingTab' } as IRoute, // ... bulletinBoardPostUpdate(post: { id: string }) { return { screen: 'bulletinBoardPostUpdate', params: { post }, } as IRoute; }, };

Slide 85

Slide 85 text

import { useNavigation } from '@react-navigation/native'; import screens from 'src/screens'; type IScreenName = keyof typeof screens; export type IRoute = | { transition: 'goBack' } | { screen: IScreenName; params?: IParams; root?: boolean } | { screen: 'buildingTab' | 'apartmentTab' | 'cashReserveTab' | 'issuesTab'; params?: IParams; root: true; }; const routes = { back: { transition: 'goBack' } as IRoute, home: { root: true, screen: 'buildingTab' } as IRoute, // ... bulletinBoardPostUpdate(post: { id: string }) { return { screen: 'bulletinBoardPostUpdate', params: { post }, } as IRoute; }, };

Slide 86

Slide 86 text

import { useNavigation } from '@react-navigation/native'; import screens from 'src/screens'; type IScreenName = keyof typeof screens; export type IRoute = | { transition: 'goBack' } | { screen: IScreenName; params?: IParams; root?: boolean } | { screen: 'buildingTab' | 'apartmentTab' | 'cashReserveTab' | 'issuesTab'; params?: IParams; root: true; }; const routes = { back: { transition: 'goBack' } as IRoute, home: { root: true, screen: 'buildingTab' } as IRoute, // ... bulletinBoardPostUpdate(post: { id: string }) { return { screen: 'bulletinBoardPostUpdate', params: { post }, } as IRoute; }, };

Slide 87

Slide 87 text

| { screen: IScreenName; params?: IParams; root?: boolean } | { screen: 'buildingTab' | 'apartmentTab' | 'cashReserveTab' | 'issuesTab'; params?: IParams; root: true; }; const routes = { back: { transition: 'goBack' } as IRoute, home: { root: true, screen: 'buildingTab' } as IRoute, // ... bulletinBoardPostUpdate(post: { id: string }) { return { screen: 'bulletinBoardPostUpdate', params: { post }, } as IRoute; }, }; export default routes; export interface INavigation { navigate: (screen: string, params?: IParams) => void; goBack: VoidFunction; }

Slide 88

Slide 88 text

| { screen: IScreenName; params?: IParams; root?: boolean } | { screen: 'buildingTab' | 'apartmentTab' | 'cashReserveTab' | 'issuesTab'; params?: IParams; root: true; }; const routes = { back: { transition: 'goBack' } as IRoute, home: { root: true, screen: 'buildingTab' } as IRoute, // ... bulletinBoardPostUpdate(post: { id: string }) { return { screen: 'bulletinBoardPostUpdate', params: { post }, } as IRoute; }, }; export default routes; export interface INavigation { navigate: (screen: string, params?: IParams) => void; goBack: VoidFunction; }

Slide 89

Slide 89 text

import { useNavigation } from '@react-navigation/native'; import screens from 'src/screens'; type IScreenName = keyof typeof screens; export type IRoute = | { transition: 'goBack' } | { screen: IScreenName; params?: IParams; root?: boolean } | { screen: 'buildingTab' | 'apartmentTab' | 'cashReserveTab' | 'issuesTab'; params?: IParams; root: true; }; const routes = { back: { transition: 'goBack' } as IRoute, home: { root: true, screen: 'buildingTab' } as IRoute, // ... bulletinBoardPostUpdate(post: { id: string }) { return { screen: 'bulletinBoardPostUpdate', params: { post }, } as IRoute; }, };

Slide 90

Slide 90 text

export interface INavigation { navigate: (screen: string, params?: IParams) => void; goBack: VoidFunction; } export function navigate(navigation: INavigation, to: IRoute) { if ('transition' in to) { navigation.goBack(); } else if (to.root) { navigation.navigate('Main', to); } else { navigation.navigate(to.screen, to.params); } } export function useNavigate() { const navigation: any = useNavigation(); const fn = React.useCallback( (to: IRoute) => { navigate(navigation, to); }, [navigation], ); return fn; }

Slide 91

Slide 91 text

import routes, { useNavigate } from '~/routes'; export function MyComponent() { const navigate = useNavigate(); const onPress = () => { navigate(routes.home); } return ( Visit product ); }

Slide 92

Slide 92 text

2 Components

Slide 93

Slide 93 text

▾ src/ ▸ components/ ▸ hooks/ ▸ screens/ ▸ styles/ ▸ translations/ ▸ types/ ▸ utils/ config.ts routes.ts app.json App.tsx

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

= Component as directory components/ PublicComponent/ PrivateSubComponent/ Fragment.graphql Mutation.graphql index.ts styles.ts utils.ts

Slide 96

Slide 96 text

Only use this component

Slide 97

Slide 97 text

Slide 98

Slide 98 text

Slide 99

Slide 99 text

Slide 100

Slide 100 text

Slide 101

Slide 101 text

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

{...} {...}

Slide 104

Slide 104 text

1 Support

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

▾ src/ ▸ components/ ▸ hooks/ ▸ screens/ ▸ styles/ ▸ translations/ ▸ types/ ▸ utils/ config.ts routes.ts app.json App.tsx

Slide 107

Slide 107 text

▾ src/ ▸ components/ ▸ hooks/ ▸ screens/ ▸ styles/ ▸ translations/ ▸ types/ ▸ utils/ config.ts routes.ts app.json App.tsx

Slide 108

Slide 108 text

▾ src/ ▸ components/ ▸ hooks/ ▸ screens/ ▸ styles/ ▸ translations/ ▸ types/ ▸ utils/ config.ts routes.ts app.json App.tsx

Slide 109

Slide 109 text

▾ src/ ▸ components/ ▸ hooks/ ▸ screens/ ▸ styles/ ▸ translations/ ▸ types/ ▸ utils/ config.ts routes.ts app.json App.tsx

Slide 110

Slide 110 text

▾ src/ ▸ components/ ▸ hooks/ ▸ screens/ ▸ styles/ ▸ translations/ ▸ types/ ▸ utils/ config.ts routes.ts app.json App.tsx

Slide 111

Slide 111 text

> ? @

Slide 112

Slide 112 text

i18next

Slide 113

Slide 113 text

▾ src/ ▾ translations/ bg.json en.json es.json index.ts

Slide 114

Slide 114 text

import i18n from 'i18next'; import captureError from 'src/utils/captureError'; import { isProduction } from 'src/config'; import bg from './bg.json'; import en from './en.json'; import es from './es.json'; // NOTE(rstankov): Documentation https://www.i18next.com/overview/configuration-optio i18n.init({ resources: { bg: { translation: bg, }, en: { translation: en, }, es: { translation: es, }, }, lng: 'bg', saveMissing: true, missingKeyHandler: (_lngs: any, _ns: string, key: string, _fallbackValue: any) => { if (isProduction) { captureError(`Key not found t('${key}')`);

Slide 115

Slide 115 text

import i18n from 'i18next'; import captureError from 'src/utils/captureError'; import { isProduction } from 'src/config'; import bg from './bg.json'; import en from './en.json'; import es from './es.json'; // NOTE(rstankov): Documentation https://www.i18next.com/overview/configuration-optio i18n.init({ resources: { bg: { translation: bg, }, en: { translation: en, }, es: { translation: es, }, }, lng: 'bg', saveMissing: true, missingKeyHandler: (_lngs: any, _ns: string, key: string, _fallbackValue: any) => { if (isProduction) { captureError(`Key not found t('${key}')`);

Slide 116

Slide 116 text

import i18n from 'i18next'; import captureError from 'src/utils/captureError'; import { isProduction } from 'src/config'; import bg from './bg.json'; import en from './en.json'; import es from './es.json'; // NOTE(rstankov): Documentation https://www.i18next.com/overview/configuration-optio i18n.init({ resources: { bg: { translation: bg, }, en: { translation: en, }, es: { translation: es, }, }, lng: 'bg', saveMissing: true, missingKeyHandler: (_lngs: any, _ns: string, key: string, _fallbackValue: any) => { if (isProduction) { captureError(`Key not found t('${key}')`);

Slide 117

Slide 117 text

// NOTE(rstankov): Documentation https://www.i18next.com/overview/configuration-optio i18n.init({ resources: { bg: { translation: bg, }, en: { translation: en, }, es: { translation: es, }, }, lng: 'bg', saveMissing: true, missingKeyHandler: (_lngs: any, _ns: string, key: string, _fallbackValue: any) => { if (isProduction) { captureError(`Key not found t('${key}')`); return key; } throw new Error(`Key not found t('${key}')`); }, compatibilityJSON: 'v3', });

Slide 118

Slide 118 text

// NOTE(rstankov): Documentation https://www.i18next.com/overview/configuration-optio i18n.init({ resources: { bg: { translation: bg, }, en: { translation: en, }, es: { translation: es, }, }, lng: 'bg', saveMissing: true, missingKeyHandler: (_lngs: any, _ns: string, key: string, _fallbackValue: any) => { if (isProduction) { captureError(`Key not found t('${key}')`); return key; } throw new Error(`Key not found t('${key}')`); }, compatibilityJSON: 'v3', });

Slide 119

Slide 119 text

// NOTE(rstankov): Documentation https://www.i18next.com/overview/configuration-optio i18n.init({ resources: { bg: { translation: bg, }, en: { translation: en, }, es: { translation: es, }, }, lng: 'bg', saveMissing: true, missingKeyHandler: (_lngs: any, _ns: string, key: string, _fallbackValue: any) => { if (isProduction) { captureError(`Key not found t('${key}')`); return key; } throw new Error(`Key not found t('${key}')`); }, compatibilityJSON: 'v3', });

Slide 120

Slide 120 text

captureError(`Key not found t('${key}')`); return key; } throw new Error(`Key not found t('${key}')`); }, compatibilityJSON: 'v3', }); export type ITranslation = keyof typeof bg; export type ILanguage = 'bg' | 'en' | 'es'; export default i18n.t as (key: keyof typeof bg, interpolations?: any) => string; export function changeLanguage(newLanguage: ILanguage) { if (i18n.language === newLanguage) { return; } return i18n.changeLanguage(newLanguage); }

Slide 121

Slide 121 text

export type ITranslation = keyof typeof bg; export type ILanguage = 'bg' | 'en' | 'es'; export default i18n.t as (key: keyof typeof bg, interpolations?: any) => string; export function changeLanguage(newLanguage: ILanguage) { if (i18n.language === newLanguage) { return; } return i18n.changeLanguage(newLanguage); }

Slide 122

Slide 122 text

▾ src/ ▸ components/ ▸ hooks/ ▸ screens/ ▸ styles/ ▸ translations/ ▸ types/ ▸ utils/ config.ts routes.ts app.json App.tsx

Slide 123

Slide 123 text

import * as React from 'react'; import { View, StyleSheet } from 'react-native'; import s from 'src/styles'; import Logo from 'src/components/Logo'; export default React.memo(function Ribbon() { return ( <> > ); }); const styles = StyleSheet.create({ container: { backgroundColor: s.colors.black, }, view: { marginTop: 10, borderTopLeftRadius: s.radius, borderTopRightRadius: s.radius, width: '100%',

Slide 124

Slide 124 text

import * as React from 'react'; import { View, StyleSheet } from 'react-native'; import s from 'src/styles'; import Logo from 'src/components/Logo'; export default React.memo(function Ribbon() { return ( <> > ); }); const styles = StyleSheet.create({ container: { backgroundColor: s.colors.black, }, view: { marginTop: 10, borderTopLeftRadius: s.radius, borderTopRightRadius: s.radius, width: '100%',

Slide 125

Slide 125 text

import * as React from 'react'; import { View, StyleSheet } from 'react-native'; import s from 'src/styles'; import Logo from 'src/components/Logo'; export default React.memo(function Ribbon() { return ( <> > ); }); const styles = StyleSheet.create({ container: { backgroundColor: s.colors.black, }, view: { marginTop: 10, borderTopLeftRadius: s.radius, borderTopRightRadius: s.radius, width: '100%',

Slide 126

Slide 126 text

}); const styles = StyleSheet.create({ container: { backgroundColor: s.colors.black, }, view: { marginTop: 10, borderTopLeftRadius: s.radius, borderTopRightRadius: s.radius, width: '100%', height: s.radius * 2, marginBottom: -s.radius, backgroundColor: s.colors.white, }, topBackground: { position: 'absolute', top: -500, height: 500, right: 0, left: 0, backgroundColor: s.colors.black, alignItems: 'center', justifyContent: 'flex-end', paddingBottom: s.spacing.l, }, });

Slide 127

Slide 127 text

const styles = StyleSheet.create({ container: { backgroundColor: s.colors.black, }, view: { marginTop: 10, borderTopLeftRadius: s.radius, borderTopRightRadius: s.radius, width: '100%', height: s.radius * 2, marginBottom: -s.radius, backgroundColor: s.colors.white, }, topBackground: { position: 'absolute', top: -500, height: 500, right: 0, left: 0, backgroundColor: s.colors.black, alignItems: 'center', justifyContent: 'flex-end', paddingBottom: s.spacing.l, }, });

Slide 128

Slide 128 text

Slide 129

Slide 129 text

Slide 130

Slide 130 text

import variables from 'src/styles'; export type ISpacing = keyof typeof spacing; export interface ISpacingProps { marginBottom?: ISpacing | null; marginHorizontal?: ISpacing | null; marginLeft?: ISpacing | null; marginRight?: ISpacing | null; marginTop?: ISpacing | null; marginVertical?: ISpacing | null; margin?: ISpacing | null; padding?: ISpacing | null; paddingBottom?: ISpacing | null; paddingHorizontal?: ISpacing | null; paddingLeft?: ISpacing | null; paddingRight?: ISpacing | null; paddingTop?: ISpacing | null; paddingVertical?: ISpacing | null; } export function spacingStyle(props: ISpacingProps, style: any = {}) { if (props.marginTop) { style.marginTop = variables.spacing[props.marginTop]; } // ... return style; }

Slide 131

Slide 131 text

import { spacingStyle, ISpacing } from '~/styles/spacing'; interface IProps extends ISpacingProps { someProps: any, // ... } function MyComponent(props) { // ... return ( {/* ... */} ); }

Slide 132

Slide 132 text

No content

Slide 133

Slide 133 text

No content

Slide 134

Slide 134 text

No content

Slide 135

Slide 135 text

Thanks A