Slide 1

Slide 1 text

React Native at Product Hunt Radoslav Stankov 24/09/2020

Slide 2

Slide 2 text

!

Slide 3

Slide 3 text

Radoslav Stankov @rstankov blog.rstankov.com
 twitter.com/rstankov
 github.com/rstankov
 speakerdeck.com/rstankov

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Architecture

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

https://rstankov.com/appearances

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Beta

Slide 14

Slide 14 text

" #

Slide 15

Slide 15 text

$

Slide 16

Slide 16 text

We had a terrible experience with React Native ~2 years ago. Most issues we had seems to be fixed now. We did two weeks spike to test this.

Slide 17

Slide 17 text

We have only 2 developers who have some Swift experience and 0 developers with Android experience.

Slide 18

Slide 18 text

Apollo for Swift isn't very good.

Slide 19

Slide 19 text

The team knows how to deal with React/Apollo.

Slide 20

Slide 20 text

We need todo fast UI iterations and experiments.

Slide 21

Slide 21 text

We will need Android as well

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

% Tech Stack

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

& Have good defaults ' Have good code organization ( Make common operations easy ) Isolate dependencies * Extensibility and reusability

Slide 29

Slide 29 text

& Have good defaults ' Have good code organization ( Make common operations easy ) Isolate dependencies * Extensibility and reusability

Slide 30

Slide 30 text

2 1 3 Support Components Screens

Slide 31

Slide 31 text

Support Components Screens

Slide 32

Slide 32 text

1) Support 2) Components 3) Screens

Slide 33

Slide 33 text

components/ hooks/ screens/ styles/ types/ utils/ app.ts config.ts routes.ts

Slide 34

Slide 34 text

1 Support Components Pages 2 3 components/ hooks/ screens/ styles/ types/ utils/ app.ts config.ts paths.ts

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

3 Screens

Slide 37

Slide 37 text

components/ hooks/ screens/ styles/ types/ utils/ app.ts config.ts routes.ts

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

@react-navigation/native +

Slide 40

Slide 40 text

import * as Screens from '~/screens' const MainStack = createStackNavigator(); function MainStackScreen() { return ( ); }

Slide 41

Slide 41 text

import * as Screens from '~/screens' const MainStack = createStackNavigator(); function MainStackScreen() { return ( ); }

Slide 42

Slide 42 text

components/ hooks/ screens/ styles/ types/ utils/ app.ts config.ts routes.ts

Slide 43

Slide 43 text

export Activity from './Activity'; export AddNoteToStack from './AddNoteToStack'; export AddToStackForm from './AddToStack'; export AddToStackPhotos from './AddToStackPhotos'; export Comments from './Comments'; export Feedback from './Feedback'; export Gallery from './Gallery'; export Home from './Home'; export Login from './Login'; // ...

Slide 44

Slide 44 text

screens/ Home/ WithSearch/ FeedItem/ Query.graphql index.ts utils.ts

Slide 45

Slide 45 text

Screen States Layout

Slide 46

Slide 46 text

Loading State Screen States Layout

Slide 47

Slide 47 text

Loading State Error State Screen States Layout

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

interface IOptions { name: string; query?: GraphQL.DocumentNode; queryVariables?: object | ((params: Params) => object); component: React.FC>; type?: 'push' | 'actionSheet' | 'overlay'; title?: string; background?: IScreenBackground, safeArea?: boolean; } function createScreen(options: IOptions) { // ... }

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

export default createScreen({ name: 'Home', query: QUERY, queryVariables: { cursor: null, }, background: 'rainbow', component({ data, fetchMore, refetch }) { useFeedNavigationOptions(); return ( ); }, });

Slide 54

Slide 54 text

http://graphql.org/

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

apollo client:codegen

Slide 57

Slide 57 text

components/ProfileAvatar/Fragment.ts import gql from 'graphql-tag'; export default gql` fragment ProfileAvatarFragment on Profile { id name kind imageUrl } `;

Slide 58

Slide 58 text

// ==================================================== // GraphQL fragment: ProfileAvatarFragment // ==================================================== export interface ProfileAvatarFragment { __typename: "Profile"; id: string; name: string; kind: string; imageUrl: string | null; } graphql/types.ts

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

components/ProfileCard/Fragment.ts import gql from 'graphql-tag'; import ProfileAvatarFragment from '~/components/ProfileAvatar/Fragment'; export default gql` fragment ProfileCardFragment on Profile { id name slug ...ProfileAvatarFragment } ${ProfileAvatarFragment} `;

Slide 61

Slide 61 text

screens/Profile/Query.ts import gql from 'graphql-tag'; import ProfileCardFragment from '~/components/ProfileCard/Fragment';
 // ... more fragments export default gql` query ProfileScreen($id: ID!) { profile(id: $id) { id ...ProfileCardFragment // ... more fragments } } ${ProfileCardFragment} `;

Slide 62

Slide 62 text

Query Fragment Fragment Fragment Fragment Fragment Fragment

Slide 63

Slide 63 text

Screen Component Component Component Component Component Component

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

components/ hooks/ screens/ styles/ types/ utils/ app.ts config.ts routes.ts

Slide 66

Slide 66 text

export interface IRoute { transition?: 'push' | 'navigate' | 'goBack'; screenName: string; params?: any; } const routes = { back(): IRoute { return { transition: 'goBack', }; }, login(reason?: string): IRoute { return { screenName: 'Login', params: { reason }, }; }, profile(profile: { id: string }): IRoute { return { screenName: 'Profile', params: { id: profile?.id }, }; }, // .... }; export default routes;

Slide 67

Slide 67 text

}; export default routes; export function navigate(navigation: INavigation, to: IRoute) { if (to.transition === 'navigate') { navigation.navigate(to.screenName, to.params); } else if (to.transition === 'goBack') { navigation.goBack(); } else { navigation.push(to.screenName, to.params); } } export function useNavigate() { const navigation = useNavigation(); const fn = React.useCallback( (to: IRoute) => { navigate(navigation, to); }, [navigation], ); return fn; }

Slide 68

Slide 68 text

import routes from '~/routes'; 
 routes.back(); routes.login({ reason: "like" }); routes.profile(profile);

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

Slide 71

Slide 71 text

2 Components

Slide 72

Slide 72 text

components/ hooks/ screens/ styles/ types/ utils/ app.ts config.ts routes.ts

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

Slide 76

Slide 76 text

interface IProps extends ISpacingProps { bold?: boolean; center?: boolean; children: any; color?: IThemeColors; numberOfLines?: number; size?: IFontSize; style?: TextStyle | TextStyle[]; testID?: string; uppercase?: boolean; withTheme?: boolean; } React.forwardRef((props: IProps, ref) => { // ... });

Slide 77

Slide 77 text

Only use this component

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

Slide 80

Slide 80 text

Slide 81

Slide 81 text

Slide 82

Slide 82 text

Slide 83

Slide 83 text

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

{...} {...}

Slide 86

Slide 86 text

{...} {...} {...} {...} {...}

Slide 87

Slide 87 text

Utility Styling Domain

Slide 88

Slide 88 text

LikeButton Button

Slide 89

Slide 89 text

AnswerCard Like Button

Slide 90

Slide 90 text

Slide 91

Slide 91 text

Atomic Design

Slide 92

Slide 92 text

...Kinda -

Slide 93

Slide 93 text

. generic components / domain components

Slide 94

Slide 94 text

1 Support

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

components/ hooks/ screens/ styles/ types/ utils/ app.ts config.ts routes.ts

Slide 97

Slide 97 text

components/ hooks/ screens/ styles/ types/ utils/ app.ts config.ts routes.ts

Slide 98

Slide 98 text

components/ hooks/ screens/ styles/ types/ utils/ app.ts config.ts routes.ts

Slide 99

Slide 99 text

Slide 100

Slide 100 text

Slide 101

Slide 101 text

⚠ We do this on mobile, because we can't make Flex component to have "gap" property!

Slide 102

Slide 102 text

import variables from '~/styles/variables'; 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 103

Slide 103 text

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

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

react-native-dynamic +

Slide 106

Slide 106 text

import { DynamicStyleSheet, useDynamicStyleSheet } from 'react-native-dynamic'; import { colors } from '~/styles/theme'; const dynamicStyles = new DynamicStyleSheet({ item: { backgroundColorc: colors.backgroundColor // ... }, // ... }); function MyComponent(props) { const styles = useDynamicStyleSheet(dynamicStyles); // ... return ( {/* ... */} ); }

Slide 107

Slide 107 text

import { useColor } from '~/styles/theme'; const backgroundColor = useColor('background');

Slide 108

Slide 108 text

import { useDarkMode, DynamicValue } from 'react-native-dynamic'; export const colors = { text: new DynamicValue('#000000', '#eaeaea'), subtitle: new DynamicValue('#999999', '#878889'), lightBackground: new DynamicValue('#fffff', '#282a2e'), background: new DynamicValue('#f9f9f9', '#1e2023'), border: new DynamicValue('#e8e8e8', '#414141'), // .... }; export type IThemeColors = keyof typeof colors; export function useColor(color: IThemeColors, withTheme: boolean = true) { const value = colors[color]; const isDark = useDarkMode(); return withTheme && isDark ? value.dark : value.light; }

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

Recap

Slide 111

Slide 111 text

/ GraphQL 1 Screens ( createScreen 2 screens helper & Components 3 isolating dependancies * directory as folder 4 domain components 5 Recap

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

Thanks 6

Slide 115

Slide 115 text

https://speakerdeck.com/rstankov Thanks 6

Slide 116

Slide 116 text

No content