40
React Native for Android Anirudh S Engineer, Hasura HASURA

React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

React Native for Android

Anirudh SEngineer, Hasura

HASURA

Page 2: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

WHAT IS REACT NATIVE?

Page 3: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local
Page 4: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Timeline

React JS deployed on Facebook’s newsfeed

2011

React deployed on instagram.com

2012

Open sourced at JS Conf US May 2013

React Native announced at React.js Conf

Feb 2015

React Native open sourced

Feb 2015

React Native released for Android Sep 2015

Page 5: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Target Audience for React Native

Web developer

Android developer

iOS developer

Knows Javascript - ES6Written & shipped Android

appsWritten & shipped iOS apps

Familiar with a modern framework

Familiar with Android best practices

Familiar with iOS best practices

Want to write mobile appsWant to write cross-platform

appsWant to write cross-platform

apps

Page 6: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

WHY REACT NATIVE?

Page 7: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Advantages over Android workflow

• Reusable code

• Faster build times (Live Reloading!)

• Faster deployment times

• React patterns

Page 8: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Blueprint to become a React Native dev

Native (Android Java)

Javascript ES6React

patterns

React Native basics

Backend Redux

Source: Spencer Carli (@spencer_carli)

Page 9: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

REACT PATTERNS

Page 10: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Features

• One-way data flow

• Virtual DOM

• JSX

• Architecture beyond HTML

Page 11: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

JSX

var HelloMessage = React.createClass({ render: function () { return <h1>Hello {this.props.message}!</h1>; } });

ReactDOM.render(<HelloMessage message="World" />, document.getElementById('root'));

Page 12: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Components

• Building block of any UI

• Modular, composable

• 2 types: Container & Presentation

• props & states

Page 13: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Component API

• this.props

• this.state

• this.setState(object newState)

Page 14: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Component Lifecycle API

• Mounting cycle

• Updating cycle

Page 15: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Mounting cycle

• constructor(object props)

• componentWillMount()

• render() -> React element

• componentDidMount()

Page 16: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Updating cycle

• componentWillReceiveProps(object nextProps)

• shouldComponentUpdate(object nextProps, object nextState) -> boolean

• componentWillUpdate(object nextProps, object nextState)

• render() -> React element

• componentDidUpdate(object prevProps, object prevState)

Page 17: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Styles

• Inline

<Text style={{color: ‘green’}}>Hello MODS</Text>

• Stylesheet

<Text style={styles.mods}>just red</Text>

const styles = StyleSheet.create({

mods: {

color: ‘green’

}

});

Page 18: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Flex box

• Specify the layout of children

• Combination of • flexDirection - orientation of primary axis

• justifyContent - distribution along primary axis

• alignItems - alignment along secondary axis

Page 19: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

REACT NATIVE

Page 20: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Setup

npm install -g react-native-cli

Page 21: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Hello World

import React, { Component } from 'react'; import { AppRegistry, Text } from 'react-native';

class HelloWorldApp extends Component { render() { return ( <Text>Hello world!</Text> ); }

}

AppRegistry.registerComponent('HelloWorldApp', () => HelloWorldApp);

Page 22: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

First party components

ActivityIndicator DrawerLayoutAndroid Image ListView

MapView Navigator Modal Picker ProgressBarAndroid

RefreshControl ScrollView Slider StatusBar Switch

Text TextInput ToolBarAndroid TouchableHighlight

TouchableNativeFeedback TouchableOpacity TouchableWithoutFeedback

View ViewPagerAndroid WebView

Page 23: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Networking

• Fetch & Promise

• Third-party libs that work with XMLHttpRequest

• WebSockets

Page 24: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Navigators

• Scenes

• Navigators

• React Native Router (react-native-router-flux)

Page 25: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Platform specific code

• Platform modules Platform.select({

ios: {

backgroundColor: 'blue',

},

android: {

backgroundColor: 'green',

}}

• Platform-specific extensions NewComponent.ios.js

NewComponent.android.js

Page 26: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Integration with existing apps

• Add com.facebook.react:react-native:+ to your app’s build.gradle

• Add an entry for the local RN maven directory in your project’s build.gradle

• Add Internet permission in your AndroidManifest.xml

• Create an Activity that • creates a ReactRootView

• starts a React application inside it

• sets it as the main content view

• Pass some activity lifecycle callbacks to the ReactInstanceManager

• Also pass back button events to React Native

• Hook up the dev menu

• Run your app

Page 27: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Native modules

• A native module is a Java class that usually extends the ReactContextBaseJavaModule class and implements the functionality required by the JavaScript

• getName method

• getConstants method

• Java methods annotated by @ReactMethod & return void

• Use corresponding Argument types

• Register the module in the createNativeModules method of your apps package

• The package needs to be provided in the getPackages method of the MainApplication.java

Page 28: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Native UI components

• Create the ViewManager subclass

• Implement the createViewInstance method

• Expose view property setters using @ReactProp (or @ReactPropGroup) annotation

• Register the manager in createViewManagers of the applications package

• Implement the JavaScript module

Page 29: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Debugging

• In-App Developer Menu

• Reload JS

• In-app Errors & Warnings

• Console logs

• Chrome Dev Tools

• Debugging Native code

• Performance Monitor

Page 30: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Testing

• Jest Tests - npm test

• Unit Tests - ./scripts/run-android-local-unit-tests.sh

• Integration Tests - ./scripts/run-android-local-integration-

tests.sh

Page 31: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

TOOLS

Page 32: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Tools

• Nuclide

• Deco

• Exponent

• CodePush

• Ignite

• Reactroton

Page 33: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

RESOURCES

Page 34: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Resources

• Awesome React Native

• JS.coach

• React Native playground

• F8 App

• makeitopen.com

Page 35: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

GROWING PAINS

Page 36: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Concerns

• Android is newer

• Open bugs

• Fix core RN bugs if your app is affected

• Getting used to the patterns

Page 37: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

CONTRIBUTING TO REACT NATIVE FOR ANDROID

Page 38: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

Submitting a Pull Request1. Fork the repo and create your branch from master.

2. Describe your test plan in your commit. If you've added code that should be tested, add tests!

3. If you've changed APIs, update the documentation.

4. If you've updated the docs, verify the website locally and submit screenshots if applicable.

$ cd website

$ npm install && npm start

go to: http://localhost:8079/react-native/index.html 5. Add the copyright notice to the top of any new files you've added.

6. Ensure tests pass on Travis and Circle CI.

7. Make sure your code lints (node linter.js <files touched>).

8. If you haven't already, sign the CLA.

9. Squash your commits (git rebase -i). one intent alongs with one commit makes it clearer for people to review and easier to understand your intention

Page 39: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local

anirudh24seven

Where can you find me?

Page 40: React Native for Android - DeveloperMarch€¦ · Integration with existing apps • Add com.facebook.react:react-native:+ to your app’s build.gradle • Add an entry for the local