Upgrade to Pro — share decks privately, control downloads, hide ads and more …

10 things the media hasn't told you about React Native

10 things the media hasn't told you about React Native

Let's examine 10 advanced topics you have to deal with when using React Native. Based from my experience at M6 Web and Osedea.

Co-created with Marvin Frachet https://twitter.com/mfrachet

Nicolas Cuillery

October 26, 2017
Tweet

More Decks by Nicolas Cuillery

Other Decks in Programming

Transcript

  1. REACT DOM VS REACT NATIVE const Avatar = (props) =>

    ( <div> <img src={props.url} /> <span>{props.name}</span> </div> ); const Avatar = (props) => ( <View> <Image source={{uri: props.url}} /> <Text>{props.name}</Text> </View> );
  2. CREATE REACT NATIVE APP CLI based on create-react-app providing minimal

    configurations for React project, including Expo npm install -g create-react-native-app create-react-native-app my-app cd my-app && npm start
  3. EXPO Set of tools and services that allow easy mobile

    development while abstracting usual tools Make livereload possible through network, app testable on real device...
  4. 3 LEVELS OF NATIVE INTEGRATIONS UI components or simple API

    (i.e. native module) ▸ Provided by React Native (StatusBar, Dimensions, …) ▸ Provided by the community (react-native-*) ▸ Home-made
  5. REACT NATIVE APIS ~70 APIs Try to standardize things between

    platforms: ▸ AsyncStorage ▸ Alert
  6. 3RD-PARTY LIBRARIES Multiples modules maintained by the community (more or

    less...) ▸ react-native-camera ▸ react-native-video ▸ react-native-vector-icons ▸ … The “link” command handles the changes in the native apps
  7. DO YOUR OWN BRIDGE Expose a native API... ▸ Call

    method from JS ▸ Callback support ▸ Listen from event … or a component ▸ Expose UIView (iOS) or ViewManager (Android) events to the bridge
  8. DO YOUR OWN BRIDGE ▸ iOS: RCTBridgeModule RCT_EXPORT_MODULE RCT_EXPORT_METHOD RCT_EXPORT_VIEW_PROPERTY

    ▸ Android: ReactContextBaseJavaModule @ReactMethod @ReactProp Simple examples: ▸ iOS component: RCTProgressViewManager.m ▸ Android native module: VibrationModule.java
  9. Place your screenshot here IMAGES IN REACT NATIVE <Image style={{

    width: 350, height: 500 }} source={require('./path.gif')} />
  10. REACT NATIVE & FRESCO ▸ Display of Gifs and WebP

    ▸ Advanced caching and loading system ▹ Bitmap caching ▹ Encoded memory cache (compressed) ▹ Disk cache
  11. INLINE STYLES INSPIRED BY CSS CSS Way .primary { background-color:

    red; } .secondary { background-color: blue; } <div className="primary"> Inline Styles const styles = { primary: { backgroundColor: 'red', }, secondary: { backgroundColor: 'blue', }, }; <View style={styles.primary}>
  12. REACT NATIVE ANIMATED ▸ High level declarative API ▸ Describes

    how to process an animation ▸ Use style properties to make animations ▸ Messages sent over the bridge on every frames
  13. Place your screenshot here this.state = { anim: new Animated.Value(-200)

    }; ____________________________________________________ Animated .timing( this.state.anim, { toValue: 0, duration: 200 } ).start(); ____________________________________________________ const style = { marginTop: this.state.anim }; return <Animated.View style={style}>{this.props.children}</Animated.View>; ANIMATED API
  14. ANIMATED & OFFLOADING ▸ Defining an animation before sending it

    to the native part ▸ The native part is responsible for the transitions ▸ No useless bridge exchange ▸ Only few properties supported...
  15. REACT NATIVE LOTTIE Project powered by AirBnb, provides a way

    to use Adobe After Effects animations inside of mobile applications using a part of Animated for transitions
  16. JAVASCRIPT VS NATIVE DILEMMA ▸ NavigatorIOS (Native) ▸ NavigatorExperimental (JS)

    ▸ react-navigation (JS) ▸ react-native-navigation (Native & JS)
  17. REACT-NAVIGATION const options = { headerMode: 'screen', headerComponent: Header };

    const config = { [SCENE_HOME]: { screen: HomeScene }, [SCENE_FAVORITE]: { screen: FavoriteScene }, [SCENE_TAG]: { screen: TagScene, path: 'tag/:tagCode' } }; const AppNavigator = StackNavigator(config, options);
  18. CONTEXT ▸ Mobile are less powerful than computer ▸ Mobile

    battery is important ▸ UX on mobile is the main key
  19. REMEMBER BEST PRACTICES ▸ Avoid complex functions (Big O) ▸

    Extract properties from the state ▸ Limit rendering lifecycles ▸ Lightweight data fetching ▸ Limit bridge exchanges
  20. SPYING ON THE MESSAGE QUEUE Listen to what happens between

    the JS thread and the native threads import MessageQueue from 'react-native/Libraries/BatchedBridge/MessageQueue'; MessageQueue.spy(true);
  21. SNOOPY Profiling tool that allows to use RxJS operator to

    manage output log from the message queue. Possibility to filter, map, reduce etc…
  22. SNOOPY import Snoopy from 'rn-snoopy'; import EventEmitter from 'react-native/Libraries/EventEmitter/EventEmitter'; import

    filter from 'rn-snoopy/stream/filter'; /* Create a react Event Emitter */ const emitter = new EventEmitter(); /* Create an events stream from the emitter */ const events = Snoopy.stream(emitter);
  23. SNOOPY /* Display only message TO the native side */

    filter({ type: Snoopy.TO_NATIVE }, true)(events).subscribe() /* Display only the message that aims to create view */ filter({ method:'createView' }, true)(events).subscribe() /* Display only the message that aims to create a view with an arg */ filter({ method:'createView', {args: {foo: {bar:1}}}}, true)(events).subscribe()
  24. MONITORING FROM NATIVE SIDE Android Studio provides a built in

    monitoring tool that can be used to check the device health while running a React Native
  25. REACT SHALLOW RENDERING Shallow rendering is already detached from the

    platform. Made easy with Enzyme { type: Title, props: { color: 'red', children: 'Hello LyonJS', } } <Title color="red"> Hello LyonJS </Title>
  26. ENJOY ENZYME API ! it('should call showOptions', () => {

    const showOptions = jest.fn(); const wrapper = shallow(<GameButton showOptions={showOptions} />); wrapper.find(TouchableOpacity).simulate('press'); expect(showOptions) .toHaveBeenCalledWith({ options: ['Switch Sides', 'Cancel'] }); });
  27. MOCKS PROVIDED BY RN Sooner or later RN components require

    native code. Jest “react-native” preset Use the same mocks as Facebook
  28. CONTEXT ▸ Mobile teams need QA teams to make functional

    tests ▸ E2E Testing exists on the web from some years ▸ Really used tool : Appium
  29. DRAWBACKS OF APPIUM ▸ Too hard to configure both on

    Android & IOS ▸ Weird unexpected errors ▸ Need to “sleep” for UI synchronization ▸ Slow ▸ Poor documentation
  30. WIX DETOX Open source project that aims to provide an

    abstraction layer for E2E testing on React Native Equivalent to Wdio or Protractor on the web
  31. WIX DETOX ▸ Tests are easy to write ▸ Protractor

    like API ▸ Based on EarlGrey & Espresso ▸ UI synchronized, no need to sleep ▸ No weird failures ▸ 5-10 time faster than Appium
  32. DUAL REPO The open-source repository is the copy of the

    internal repository used in production. Master is stable.
  33. PULL REQUEST WORKFLOW 1. A contributor opens a Pull Request,

    2. The approved Pull Request is… closed, 3. The changes are battle-tested in the internal repo 4. Once the changes are approved, the commit is cherry-picked on master
  34. RELEASE WORKFLOW ▸ One release per month (0.xx) ▸ One

    branch for each release (0.xx-stable) The master branch is published after a 1-month delay. Hot commits can be cherry-picked. At least one RC during the month.