Slide 1

Slide 1 text

Living in an async world of React Native Krzysztof Magiera (@kzzzf) [email protected]

Slide 2

Slide 2 text

REACT NATIVE JS Thread Native Thread UI Thread time

Slide 3

Slide 3 text

ASYNC STORAGE onboardingCompleted? render(MainApp) render(OnboardingComponent) YES N O

Slide 4

Slide 4 text

ASYNC STORAGE JS Thread Native Thread getItem('onboardingCompleted'); AsyncStorage if (YES) { return } else { return } UI Thread

Slide 5

Slide 5 text

ASYNC STORAGE JS Thread Native Thread getItem('onboardingCompleted'); AsyncStorage if (YES) { return } else { return } UI Thread layout render render rendering !

Slide 6

Slide 6 text

CREATING “SYNC” STORAGE - (NSDictionary *)constantsToExport RCTBridgeModule iOS public @Nullable Map getConstants() BaseJavaModule Android const MySyncStorageModule = require('NativeModules').MySyncStorageModule; const _data = MySyncStorageModule.data; function getItem(key) { return _data[key]; } function setItem(key, value) { _data[key] = value; MySyncStorageModule.asyncSet(key, value); } JS

Slide 7

Slide 7 text

SCREEN ORIENTATION https://www.npmjs.com/package/react-native-orientation Orientation.getInitialOrientation(); // PORTRAIT, LANDSCAPE, etc... Orientation.addOrientationListener((orientation) => { // orientation = PORTRAIT, LANDSCAPE, etc... }); DeviceEventEmitter.addListener('namedOrientationDidChange', (orientation) => { // orientation.rotationDegrees // orientation.isLandscape }); React Native Core (android only)

Slide 8

Slide 8 text

SCREEN ORIENTATION

Slide 9

Slide 9 text

SCREEN ORIENTATION JS Thread Native Thread UI Thread orientation changes if (isLandscape) { return } else { return } event Layout rendering

Slide 10

Slide 10 text

SCREEN ORIENTATION JS Thread Native Thread UI Thread orientation changes if (isLandscape) { return } else { return } event Layout rendering render Layout rendering !

Slide 11

Slide 11 text

SCREEN ORIENTATION ! ! ! ! ! ! ! ! !

Slide 12

Slide 12 text

CONTROL ORIENTATION CHANGES IN JS onOrientationChange = (orientation) => { lockToOrientation(orientation); this.setState({ orientation }) }; lockToOrientation(initialOrientation); render() { if (this.state.orientation === 'LANDSCAPE') { return ; } else { return } }

Slide 13

Slide 13 text

ANIMATED var someValue = new Animated.Value(0); // can be kept in the component state return ( Hello! ); Animated.spring( someValue, { toValue: 1, friction: 0.5, } ).start();

Slide 14

Slide 14 text

ANIMATED JS Thread Native Thread Animated.spring(...).start() UI Thread event animationLoop() rendering update some views UIManager setTimeout TimerModule

Slide 15

Slide 15 text

ANIMATED JS Thread Native Thread Animated.spring(...).start() UI Thread event animationLoop() rendering update some views UIManager setTimeout TimerModule event TimerModule animationLoop() rendering update some views UIManager event TimerModule animationLoop() update some views UIManager

Slide 16

Slide 16 text

ANIMATED ! ! ! ! ! ! ! ! !

Slide 17

Slide 17 text

ANIMATED - OFFLOADED Animated.spring( someValue, { toValue: 1, friction: 0.5, useNativeDriver: true, } ).start();

Slide 18

Slide 18 text

TOUCH EVENTS JS Thread Native Thread UI Thread rendering update some views UIManager handleTouch() event

Slide 19

Slide 19 text

handleTouch() f1 f2 f3 … sorry man, have to compose the frame input compose VSYNC input compose input rendering UI Thread JS Thread TOUCH EVENTS

Slide 20

Slide 20 text

TOUCH EVENTS ! ! ! ! ! ! ! ! !

Slide 21

Slide 21 text

TOUCH EVENTS

Slide 22

Slide 22 text

GESTURE HANDLING JS Thread Native Thread UI Thread handleTouch() send touch down event Touch DOWN Scroll recognizer Touchable

Slide 23

Slide 23 text

GESTURE HANDLING JS Thread Native Thread UI Thread handleTouch() send touch down event Touch DOWN Touch MOVE handleTouch() handleTouch() Touch MOVE Touch MOVE send touch move event send touch move event Scroll recognizer Touchable

Slide 24

Slide 24 text

GESTURE HANDLING JS Thread Native Thread UI Thread handleTouch() send touch down event Touch DOWN Touch MOVE handleTouch() handleTouch() Touch MOVE Touch MOVE send touch move event send touch move event send scroll event handleScroll() Scroll recognizer " Touchable #

Slide 25

Slide 25 text

GESTURE HANDLING JS Thread Native Thread UI Thread Touch DOWN Scroll recognizer handleTouch() send touch down event Touchable

Slide 26

Slide 26 text

GESTURE HANDLING JS Thread Native Thread UI Thread Touch DOWN handleTouch() handleTouch() Touch MOVE Touch MOVE Touch UP send touch move event send touch move event Scroll recognizer handleTouch() send touch down event Touchable

Slide 27

Slide 27 text

GESTURE HANDLING JS Thread Native Thread UI Thread Touch DOWN handleTouch() handleTouch() Touch MOVE Touch MOVE Touch UP send touch move event send touch move event Scroll recognizer handleTouch() send touch up event # handleTouch() send touch down event Touchable "

Slide 28

Slide 28 text

GESTURE HANDLING

Slide 29

Slide 29 text

JS Thread Native Thread UI Thread Touch DOWN handleTouch() send touch down event PanResponder GESTURE HANDLING Scroll recognizer

Slide 30

Slide 30 text

JS Thread Native Thread UI Thread Touch DOWN Touch MOVE handleTouch() handleTouch() Touch MOVE Touch MOVE send touch move event send touch move event handleTouch() send touch down event PanResponder GESTURE HANDLING Scroll recognizer

Slide 31

Slide 31 text

JS Thread Native Thread UI Thread Touch DOWN Touch MOVE handleTouch() handleTouch() Touch MOVE Touch MOVE send touch move event send touch move event handleTouch() send touch down event PanResponder " GESTURE HANDLING Scroll recognizer "

Slide 32

Slide 32 text

GESTURE HANDLING ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !

Slide 33

Slide 33 text

class Button extends React.Component { render() { return ( Hello ); } } GESTURE HANDLING

Slide 34

Slide 34 text

class Button extends React.Component { render() { return ( Hello ); } _onStateChange = (event) => { } } GESTURE HANDLING

Slide 35

Slide 35 text

class Button extends React.Component { render() { return ( Hello ); } _onStateChange = (event) => { this.setState({ pressed: event.state === State.BEGAN }); if (event.state === State.ACTIVE) { this.props.onClick(); } } } GESTURE HANDLING

Slide 36

Slide 36 text

class Draggable extends React.Component { constructor(props) { super(props); this._translateX = new Animated.Value(0); this._translateY = new Animated.Value(0); } render() { return ( ); } } GESTURE HANDLING

Slide 37

Slide 37 text

class Draggable extends React.Component { constructor(props) { super(props); this._translateX = new Animated.Value(0); this._translateY = new Animated.Value(0); } render() { return ( ); } } GESTURE HANDLING

Slide 38

Slide 38 text

GESTURE HANDLING class Draggable extends React.Component { constructor(props) { super(props); this._translateX = new Animated.Value(0); this._translateY = new Animated.Value(0); } render() { return ( ); } }

Slide 39

Slide 39 text

_onGestureEvent = (event) => { this._translateX.setValue(event.nativeEvent.translationX); this._translateY.setValue(event.nativeEvent.translationY); } _onHandlerStateChange = (event) => { if (event.nativeEvent.oldState === State.ACTIVE) { this._lastOffset.x += event.nativeEvent.lastTranslationX; this._lastOffset.y += event.nativeEvent.lastTranslationY; this._translateX.setOffset(this._lastOffset.x); this._translateX.setValue(0); this._translateY.setOffset(this._lastOffset.y); this._translateY.setValue(0); } } GESTURE HANDLING

Slide 40

Slide 40 text

_onHandlerStateChange = (event) => { if (event.nativeEvent.oldState === State.ACTIVE) { this._lastOffset.x += event.nativeEvent.lastTranslationX; this._lastOffset.y += event.nativeEvent.lastTranslationY; this._translateX.setOffset(this._lastOffset.x); this._translateX.setValue(0); this._translateY.setOffset(this._lastOffset.y); this._translateY.setValue(0); } } GESTURE HANDLING _onGestureEvent = Animated.event( [{ nativeEvent: { translationX: this._translateX, translationY: this._translateY }}], { useNativeDriver: true } );

Slide 41

Slide 41 text

GESTURE HANDLING render() { return ( ); } https://github.com/kmagiera/react-native-gesture-handler

Slide 42

Slide 42 text

Thank you! Krzysztof Magiera [email protected] @kzzzf