Slide 1

Slide 1 text

Crossing platforms with JavaScript & React

Slide 2

Slide 2 text

@robdel12

Slide 3

Slide 3 text

JavaScript is EVERYWHERE

Slide 4

Slide 4 text

Web

Slide 5

Slide 5 text

Server

Slide 6

Slide 6 text

iOS

Slide 7

Slide 7 text

Android

Slide 8

Slide 8 text

Desktop

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

What is cross platform JS?

Slide 11

Slide 11 text

JS that can run on more than one platform

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

“Why did the iOS team implement it like this?”

Slide 14

Slide 14 text

“The Android app currently doesn’t support that”

Slide 15

Slide 15 text

https://twitter.com/dan_abramov/status/812047645732651009

Slide 16

Slide 16 text

Easier to share code across many teams

Slide 17

Slide 17 text

More team collaboration since there’s more overlap

Slide 18

Slide 18 text

It allows teams to own products & not be separated by technology

Slide 19

Slide 19 text

TL;DR your team now owns the iOS, Android, and (maybe) web apps.

Slide 20

Slide 20 text

Consistency is

Slide 21

Slide 21 text

Cheaper

Slide 22

Slide 22 text

If you can build iOS & Android apps in the same code base it should be cheaper

Slide 23

Slide 23 text

Why not bet on the web?

Slide 24

Slide 24 text

Native will be better than mobile web for a while

Slide 25

Slide 25 text

Why not take the web tooling & get native results?

Slide 26

Slide 26 text

You are betting on the web

Slide 27

Slide 27 text

Can’t beat them, join them

Slide 28

Slide 28 text

I decided to be ambitious

Slide 29

Slide 29 text

Build an Instagram clone for Web, iOS, & Android

Slide 30

Slide 30 text

Why an Instagram clone?

Slide 31

Slide 31 text

Use Impagination.js to power an Infinite scroll of images

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Impagination will work on any JS codebase

Slide 34

Slide 34 text

Building infinite scroll in React Native with Impagination http://bit.ly/reactnativeinfinitescroll

Slide 35

Slide 35 text

We’ve already used Impagination in four different platforms

Slide 36

Slide 36 text

What else can be shared?

Slide 37

Slide 37 text

Experiment time

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Three phases to the experiment • Planning • Implementation • Postmortem

Slide 40

Slide 40 text

Planning

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

The stack • React Native • React (DOM) • Auth0 (authentication) • Graph.cool (backend) • Impagination (infinite datasets)

Slide 43

Slide 43 text

What should the app do? • Login / Sign up • See your profile & images you’ve posted • Edit your profile • Post a new photo • Main list feed showing everyones posts

Slide 44

Slide 44 text

Web demo

Slide 45

Slide 45 text

Implementation

Slide 46

Slide 46 text

What’s the approach?

Slide 47

Slide 47 text

Build the web app

Slide 48

Slide 48 text

Start to build the native app

Slide 49

Slide 49 text

Realize I’ve already solved these problems in the web app

Slide 50

Slide 50 text

Refactor

Slide 51

Slide 51 text

ListPage.js

Slide 52

Slide 52 text

ListPage.js handles both UI & data right now

Slide 53

Slide 53 text

ListPage for native duplicates a lot form web ListPage

Slide 54

Slide 54 text

class ListPage extends React.Component { static propTypes = { data: React.PropTypes.object, } state = { dataset: null, datasetState: null, } setupImpagination() {} componentWillMount() {this.setupImpagination();} setCurrentReadOffset = (event) => {} render () { return (
{this.state.datasetState.map(record => { if (record.isPending && !record.isSettled) { return ; } return ; })}
); } } const FeedQuery = gql`query($skip: Int!, $first: Int!) { allPhotos(orderBy: createdAt_DESC, first: $first, skip: $skip) { } }`; export default graphql(FeedQuery, {options: {variables: { skip: 0, first: PAGE_SIZE }}})(ListPage); Web ListPage.js

Slide 55

Slide 55 text

` class ListPage extends React.Component { static propTypes = { data: React.PropTypes.object, } state = { dataset: null, datasetState: null, } setupImpagination() {} componentWillMount() {this.setupImpagination();} setCurrentReadOffset = (event) => {} render () { return ( {this.state.datasetState.map(record => { if(record.isPending && !record.isSettled) { return ; } return ; })} ); } } const FeedQuery = gql`query($skip: Int!, $first: Int!) { allPhotos(orderBy: createdAt_DESC, first: $first, skip: $skip) { } }`; export default graphql(FeedQuery, {options: {variables: { skip: 0, first: PAGE_SIZE }}})(ListPage); Native ListPage.js

Slide 56

Slide 56 text

Everything but the UI is the same

Slide 57

Slide 57 text

New structure

Slide 58

Slide 58 text

Presentation & container components

Slide 59

Slide 59 text

Slide 60

Slide 60 text

import ListPageView from ‘../components/presentational/ListPageView’; class ListPageContainer extends React.Component { state = { dataset: null, datasetState: null, } setupImpagination() {} componentWillMount() {this.setupImpagination();} setCurrentReadOffset = (event) => {} render () { return ( ; ); } } const FeedQuery = gql`query($skip: Int!, $first: Int!) { allPhotos(orderBy: createdAt_DESC, first: $first, skip: $skip) { } }`; export default graphql(FeedQuery, {options: {variables: { skip: 0, first: PAGE_SIZE }}})(ListPage);

Slide 61

Slide 61 text

Make the container component render a separate presentation component

Slide 62

Slide 62 text

Leave setting the readOffset to the presentation components

Slide 63

Slide 63 text

setCurrentReadOffset function is passed as a prop from the container component

Slide 64

Slide 64 text

t import React, { Component } from 'react'; import Infinite from 'react-infinite'; import Photo from '../presentational/Photo'; import LoadingPost from '../presentational/LoadingPost'; const ITEM_HEIGHT = 600; const HEADER_HEIGHT = 80; class ListPageView extends Component { setCurrentReadOffset = (event) => { let currentItemIndex = Math.ceil((window.scrollY - HEADER_HEIGHT) / ITEM_HEIGHT); this.props.setCurrentReadOffset(currentItemIndex); } render() { return (
{this.props.datasetState.map(record => { if (record.isPending && !record.isSettled) { return ; } return ; })}
); } } export default ListPageView; Web presentation component

Slide 65

Slide 65 text

Native presentation component import React, { Component } from 'react'; import Photo from '../presentational/Photo'; import LoadingPost from '../presentational/LoadingPost'; import { ScrollView } from 'react-native'; const ITEM_HEIGHT = 485; class ListPageView extends Component { setCurrentReadOffset = (event) => { let currentOffset = Math.floor(event.nativeEvent.contentOffset.y); let currentItemIndex = Math.ceil(currentOffset / ITEM_HEIGHT); this.props.setCurrentReadOffset(currentItemIndex); } render() { return ( {this.props.datasetState.map(record => { if(record.isPending && !record.isSettled) { return ; } return ; })} ); } } export default ListPageView;

Slide 66

Slide 66 text

This theme continues throughout the entire app

Slide 67

Slide 67 text

} />

Slide 68

Slide 68 text

Native app demo

Slide 69

Slide 69 text

Postmortem

Slide 70

Slide 70 text

Building the apps in time was hard…

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

Figuring out what code is shareable

Slide 74

Slide 74 text

Figuring out how to make that code shareable

Slide 75

Slide 75 text

React Router is neat & works cross platform There are different imports for React Native & React

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

Auth0 was very easy to implement on both platforms. There are different APIs for React Native & React

Slide 78

Slide 78 text

AsyncStorage vs localStorage

Slide 79

Slide 79 text

What all ended up being shared?

Slide 80

Slide 80 text

✅ List feed ✅ User profile ✅ Edit user profile ✅ Sign up ✅ New post

Slide 81

Slide 81 text

Beyond login mostly everything else is the same

Slide 82

Slide 82 text

The UI changed but not the business logic

Slide 83

Slide 83 text

Key takeaways

Slide 84

Slide 84 text

We’re in a post DOM world

Slide 85

Slide 85 text

Write JavaScript interaction models

Slide 86

Slide 86 text

The UI framework will change but the underlying model driving it won’t

Slide 87

Slide 87 text

“It’s just JavaScript”

Slide 88

Slide 88 text

We get stronger libraries by increasing the number of users & contributors.

Slide 89

Slide 89 text

React makes this very easy thanks to React & React Native

Slide 90

Slide 90 text

I was able to share the same five container components across three different platforms

Slide 91

Slide 91 text

Write one container component and many UI components

Slide 92

Slide 92 text

The core of this app is shared

Slide 93

Slide 93 text

That’s a cost savings

Slide 94

Slide 94 text

I own this entire product & its 3 platforms

Slide 95

Slide 95 text

In 2 weeks I was able do all of this

Slide 96

Slide 96 text

Cross platform JS FTW

Slide 97

Slide 97 text

Instagram also agrees with me https://engineering.instagram.com/react-native-at-instagram- dd828a9a90c7#.i364vchox

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

If this kind of stuff interests you

Slide 100

Slide 100 text

We’re hiring!

Slide 101

Slide 101 text

Thanks! @robdel12