React Native for multi-platform mobile applications

Matteo Manchi
Matteo ManchiChief Executive Officer at ImprontaAdvance srl
React-Native
for multi-platform mobile applications
Matteo Manchi
Full stack web developer
CEO at Impronta Advance
Twitter: @matteomanchi
Agenda
From React to React Native
Technologies behind RN
Multi-platform compatibility
iOS and Android native modules
Let's start from the
beginning...
What is React.js?
React is a JavaScript library for building user interfaces.
Just the UI
One-way data flow
Virtual DOM
From Facebook
Some keywords
Component: Everything is a component
Props: some data passed to child component
State: some internal data of a component
JSX: XML-like syntax helps to define component's
structure
Virtual DOM: tree of custom objects representing a port
of the DOM.
Component definition
import React, {Component, PropTypes} from 'react';
class CountDown extends Component {
static propTypes = {
seconds: PropTypes.number.isRequired
};
constructor(props) {
super(props);
this.state = {
seconds: props.seconds
};
}
// ...
}
Component definition - Pt.2
class CountDown extends Component {
// ...
componentDidMount() {
setInterval(() => this.setState({
seconds: this.state.seconds-1
}), 1000);
}
render() {
return (
<span>Time left: {this.state.seconds}</span>
);
}
}
// Invoking
< CountDown seconds="10" />
With or without you JSX
// JSX
render() {
return (
<span>Time left: {this.state.seconds}</span>
);
}
// plain js
return React.createElement(
"span",
null,
"Time left: ",
this.state.seconds
);
Let's talk back about mobile
applications
What are common troubles developing (multi-platform)
mobile application?
Know how
Use native APIs
Performance
React Native
A framework for building native apps using React.
Yeah, the same React.js of web developers
Learn once, write anywhere
What is React Native?
A framework
Binds JSX to iOS Cocoa Touch or Android UI
Allow applications to run at near full speed
JSX to native UI
Exactly the same of React.js, but naming native components
// react-dom
render() {
return (
<div>
<span>Time left: {this.state.seconds}</span>
</div>
);
}
// react-native
render() {
return (
<view>
<text>Time left: {this.state.seconds}</text>
</view>
);
}
What do you mean with
near full speed?
Yes, like Titanium.
A lot of components
ActivityIndicatorIOS
DatePickerIOS
DrawerLayoutAndroid
Image
ListView
MapView
Modal
Navigator
NavigatorIOS
PickerIOS
ProgressBarAndroid
ProgressViewIOS
PullToRefreshViewAndroid
RefreshControl
ScrollView
SegmentedControlIOS
SliderIOS
Switch
TabBarIOS
TabBarIOS.Item
Text
TextInput
ToolbarAndroid
TouchableHighlight
TouchableNativeFeedback
TouchableOpacity
TouchableWithoutFeedback
View
ViewPagerAndroid
WebView
A lot of APIs
ActionSheetIOS
Alert
AlertIOS
Animated
AppRegistry
AppStateIOS
AsyncStorage
BackAndroid
CameraRoll
Dimensions
IntentAndroid
InteractionManager
LayoutAnimation
LinkingIOS
NativeMethodsMixin
NetInfo
PanResponder
PixelRatio
PushNotificationIOS
StatusBarIOS
StyleSheet
ToastAndroid
VibrationIOS
Requirements for
React Native
Node.js 4.0 or newer.
Xcode 7.0 or higher is required for iOS apps
Android SDK + gradle + AVD/Genymotion for Android
apps
Getting Started
$ npm install -g react-native-cli
$ react-native init AwesomeProject
$ cd AwesomeProject
// To run iOS app
$ open ios/AwesomeProject.xcodeproj
// To run Android app
$ react-native run-android
Technologies behind
React Native
React, JSX and Virtual DOM :)
Webpack loads all dependencies required by index.ios.js
and index.android.js and bundle them together
ES6 compatibility allows devs to use modern syntax and
features
NPM as module manager
Chrome Debugger as powerful debugger
Flow as type checker - optionally
All JavaScript's modules
Every module there are not DOM-depending can be used in
React Native. Like a browser, there are some polyfills that
allow to use modules you have skill with.
Flexbox
Geolocation
Network (fetch, ajax, etc)
Timers (timeout, interval, etc)
Inline CSS-like styles
Custom CSS implementation, easy to learn 'cause it's very
similar to browser's CSS.
With Flexbox.
In JavaScript.
https://facebook.github.io/react-native/docs/style.html
import {StyleSheet} from 'react-native';
const styles = StyleSheet.create({
base: {
width: 38,
height: 38,
},
background: {
backgroundColor: LIGHT_GRAY,
},
active: {
borderWidth: 4/2,
borderColor: '#00ff00',
},
});
<View style={[styles.base, styles.background]} />
What about
multi-platforms?
React Native actually supports iOS and Android. It
provides a lot of components and APIs ready to use in both
platforms.
A lot of components
ActivityIndicatorIOS
DatePickerIOS
DrawerLayoutAndroid
Image
ListView
MapView
Modal
Navigator
NavigatorIOS
PickerIOS
ProgressBarAndroid
ProgressViewIOS
PullToRefreshViewAndroid
RefreshControl
ScrollView
SegmentedControlIOS
SliderIOS
Switch
TabBarIOS
TabBarIOS.Item
Text
TextInput
ToolbarAndroid
TouchableHighlight
TouchableNativeFeedback
TouchableOpacity
TouchableWithoutFeedback
View
ViewPagerAndroid
WebView
Native Modules
React Native is madly growing up! Each release (almost
once per month) adds new features for both platforms
thanks to over 500 contributors.
Despite the big community around the project, can happen
that the native view you're looking for isn't supported by
the framework. In this case you have to wait for someone
else, or make your native module (or binding native API)
iOS Module - Pt. 1
// ShareManager.h
#import < UIKit/UIKit.h >
#import "RCTBridgeModule.h"
@interface RNShare : NSObject < RCTBridgeModule >
@end
iOS Module - Pt. 2
// ShareManager.m
@implementation ShareManager
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(share:(NSString *)text url:(NSString *)url) {
NSURL *cardUrl = [NSURL URLWithString:url];
NSArray *itemsToShare = @[text, url, cardUrl];
UIActivityViewController *activityVC =
[[UIActivityViewController alloc]
initWithActivityItems:itemsToShare
applicationActivities:nil];
UIViewController *root =
[[[[UIApplication sharedApplication] delegate]
window] rootViewController];
[root presentViewController:activityVC animated:YES completion:nil];
}
@end
iOS Module - Pt. 3
var ShareManager = require('react-native').NativeModules.ShareManager;
ShareManager.share('Hi from RomaJS!', 'http://www.romajs.org');
http://facebook.github.io/react-native/docs/native-modules-ios.html
Android Module - Pt. 1
// ShareModule.java
// package + imports
public class ShareModule extends ReactContextBaseJavaModule {
// ...
public ShareModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@Override
public String getName() {
return "ShareManager";
}
// continue
}
Android Module - Pt. 2
// ShareModule.java
public class ShareModule extends ReactContextBaseJavaModule {
// ...
@ReactMethod
public void share(String text, String url) {
Intent sharingIntent =
new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, text);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, url);
Intent chooser = Intent.createChooser(sharingIntent, "Share");
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.reactContext.startActivity(chooser);
}
}
Android Module - Pt. 3
// SharePackage.java
// package + imports
public class SharePackage implements ReactPackage {
@Override
public List< NativeModule >
createNativeModules(ReactApplicationContext reactContext) {
List< NativeModule > modules = new ArrayList<>();
modules.add(new ShareModule(reactContext));
return modules;
}
// ...
}
Android Module - Pt. 4
// MainActivity.java
// ...
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.addPackage(new SharePackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
// ...
Android Module - Pt. 5
var ShareManager = require('react-native').NativeModules.ShareManager;
ShareManager.share('Hi from RomaJS!', 'http://www.romajs.org');
http://facebook.github.io/react-native/docs/native-modules-android.html
Different platform,
different behavior
Take some actions depending to platform
Method 1: if/else
import {Platform} from 'react-native';
const styles = StyleSheet.create({
myView: {
backgroundColor: Platform.OS === 'ios' ? 'red' : 'lime'
}
});
Method 2: file extension
by webpack-based packager
// MyStyle.ios.js
export default const styles = StyleSheet.create({
myView: {
backgroundColor: 'red'
}
});
// MyStyle.android.js
export default const styles = StyleSheet.create({
myView: {
backgroundColor: 'lime'
}
});
Woah! Woah!
Questions?
Native Haters
What about competitors?
Cordova/Phonegap
Ionic
Xamarin
Titanium
Thank you
1 of 39

Recommended

React-Native for multi-platform mobile applications @ Codemotion Rome 2017 by
React-Native for multi-platform mobile applications @ Codemotion Rome 2017React-Native for multi-platform mobile applications @ Codemotion Rome 2017
React-Native for multi-platform mobile applications @ Codemotion Rome 2017Matteo Manchi
579 views47 slides
Introduzione a React Native - Facebook Developer Circle Rome by
Introduzione a React Native - Facebook Developer Circle RomeIntroduzione a React Native - Facebook Developer Circle Rome
Introduzione a React Native - Facebook Developer Circle RomeMatteo Manchi
229 views59 slides
Introduction to react native by
Introduction to react nativeIntroduction to react native
Introduction to react nativeDani Akash
404 views29 slides
React Native by
React NativeReact Native
React NativeFatih Şimşek
963 views18 slides
React Native for ReactJS Devs by
React Native for ReactJS DevsReact Native for ReactJS Devs
React Native for ReactJS DevsBarak Cohen
776 views44 slides
Introduction to React Native & Rendering Charts / Graphs by
Introduction to React Native & Rendering Charts / GraphsIntroduction to React Native & Rendering Charts / Graphs
Introduction to React Native & Rendering Charts / GraphsRahat Khanna a.k.a mAppMechanic
1.1K views20 slides

More Related Content

What's hot

A tour of React Native by
A tour of React NativeA tour of React Native
A tour of React NativeTadeu Zagallo
5.1K views97 slides
React Native in a nutshell by
React Native in a nutshellReact Native in a nutshell
React Native in a nutshellBrainhub
718 views12 slides
Introduction to React Native by
Introduction to React NativeIntroduction to React Native
Introduction to React NativeWaqqas Jabbar
360 views23 slides
React Native custom components by
React Native custom componentsReact Native custom components
React Native custom componentsJeremy Grancher
5.3K views50 slides
Experiences building apps with React Native @DomCode 2016 by
Experiences building apps with React Native @DomCode 2016Experiences building apps with React Native @DomCode 2016
Experiences building apps with React Native @DomCode 2016Adrian Philipp
780 views30 slides
React native by
React nativeReact native
React nativeOmid Nikrah
285 views25 slides

What's hot(20)

A tour of React Native by Tadeu Zagallo
A tour of React NativeA tour of React Native
A tour of React Native
Tadeu Zagallo5.1K views
React Native in a nutshell by Brainhub
React Native in a nutshellReact Native in a nutshell
React Native in a nutshell
Brainhub718 views
Introduction to React Native by Waqqas Jabbar
Introduction to React NativeIntroduction to React Native
Introduction to React Native
Waqqas Jabbar360 views
React Native custom components by Jeremy Grancher
React Native custom componentsReact Native custom components
React Native custom components
Jeremy Grancher5.3K views
Experiences building apps with React Native @DomCode 2016 by Adrian Philipp
Experiences building apps with React Native @DomCode 2016Experiences building apps with React Native @DomCode 2016
Experiences building apps with React Native @DomCode 2016
Adrian Philipp780 views
Intro To React Native by FITC
Intro To React NativeIntro To React Native
Intro To React Native
FITC8.8K views
Utilizing HTML5 APIs by Ido Green
Utilizing HTML5 APIsUtilizing HTML5 APIs
Utilizing HTML5 APIs
Ido Green4.6K views
Modern Web Applications Utilizing HTML5 APIs by Ido Green
Modern Web Applications Utilizing HTML5 APIsModern Web Applications Utilizing HTML5 APIs
Modern Web Applications Utilizing HTML5 APIs
Ido Green4.5K views
Intro to react native by ModusJesus
Intro to react nativeIntro to react native
Intro to react native
ModusJesus11.4K views
Matteo Manchi - React Native for multi-platform mobile applications - Codemot... by Codemotion
Matteo Manchi - React Native for multi-platform mobile applications - Codemot...Matteo Manchi - React Native for multi-platform mobile applications - Codemot...
Matteo Manchi - React Native for multi-platform mobile applications - Codemot...
Codemotion466 views
Optimizing React Native views for pre-animation by ModusJesus
Optimizing React Native views for pre-animationOptimizing React Native views for pre-animation
Optimizing React Native views for pre-animation
ModusJesus2.2K views
React native sharing by Sam Lee
React native sharingReact native sharing
React native sharing
Sam Lee815 views
Getting Started with React Native (and should I use it at all?) by Devin Abbott
Getting Started with React Native (and should I use it at all?)Getting Started with React Native (and should I use it at all?)
Getting Started with React Native (and should I use it at all?)
Devin Abbott771 views
What's This React Native Thing I Keep Hearing About? by Evan Stone
What's This React Native Thing I Keep Hearing About?What's This React Native Thing I Keep Hearing About?
What's This React Native Thing I Keep Hearing About?
Evan Stone691 views
Introduction to React Native by dvcrn
Introduction to React NativeIntroduction to React Native
Introduction to React Native
dvcrn514 views

Viewers also liked

React JS and why it's awesome by
React JS and why it's awesomeReact JS and why it's awesome
React JS and why it's awesomeAndrew Hull
108K views132 slides
Thinking Reactively by
Thinking ReactivelyThinking Reactively
Thinking ReactivelySabin Marcu
358 views139 slides
The Internet of Things - Decoupling Producers and Consumers of M2M Device Data by
The Internet of Things - Decoupling Producers and Consumers of M2M Device DataThe Internet of Things - Decoupling Producers and Consumers of M2M Device Data
The Internet of Things - Decoupling Producers and Consumers of M2M Device DataEurotech
2.7K views9 slides
React native first impression by
React native first impressionReact native first impression
React native first impressionAlvaro Viebrantz
1.5K views30 slides
React in Native Apps - Meetup React - 20150409 by
React in Native Apps - Meetup React - 20150409React in Native Apps - Meetup React - 20150409
React in Native Apps - Meetup React - 20150409Minko3D
28.9K views21 slides
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti... by
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...Codemotion
795 views64 slides

Viewers also liked(12)

React JS and why it's awesome by Andrew Hull
React JS and why it's awesomeReact JS and why it's awesome
React JS and why it's awesome
Andrew Hull108K views
Thinking Reactively by Sabin Marcu
Thinking ReactivelyThinking Reactively
Thinking Reactively
Sabin Marcu358 views
The Internet of Things - Decoupling Producers and Consumers of M2M Device Data by Eurotech
The Internet of Things - Decoupling Producers and Consumers of M2M Device DataThe Internet of Things - Decoupling Producers and Consumers of M2M Device Data
The Internet of Things - Decoupling Producers and Consumers of M2M Device Data
Eurotech2.7K views
React in Native Apps - Meetup React - 20150409 by Minko3D
React in Native Apps - Meetup React - 20150409React in Native Apps - Meetup React - 20150409
React in Native Apps - Meetup React - 20150409
Minko3D28.9K views
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti... by Codemotion
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
Codemotion795 views
Rethinking Best Practices by floydophone
Rethinking Best PracticesRethinking Best Practices
Rethinking Best Practices
floydophone130.2K views
ReactJS | 서버와 클라이어트에서 동시에 사용하는 by Taegon Kim
ReactJS | 서버와 클라이어트에서 동시에 사용하는ReactJS | 서버와 클라이어트에서 동시에 사용하는
ReactJS | 서버와 클라이어트에서 동시에 사용하는
Taegon Kim36.2K views
React + Redux Introduction by Nikolaus Graf
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
Nikolaus Graf25.6K views
CSS in React - The Good, The Bad, and The Ugly by Joe Seifi
CSS in React - The Good, The Bad, and The UglyCSS in React - The Good, The Bad, and The Ugly
CSS in React - The Good, The Bad, and The Ugly
Joe Seifi1.7K views

Similar to React Native for multi-platform mobile applications

Full Stack React Workshop [CSSC x GDSC] by
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]GDSC UofT Mississauga
46 views45 slides
Workshop 26: React Native - The Native Side by
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideVisual Engineering
2.8K views54 slides
JSAnkara Swift v React Native by
JSAnkara Swift v React NativeJSAnkara Swift v React Native
JSAnkara Swift v React NativeMuhammed Demirci
189 views41 slides
20180518 QNAP Seminar - Introduction to React Native by
20180518 QNAP Seminar - Introduction to React Native20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React NativeEric Deng
218 views53 slides
React js by
React jsReact js
React jsRajesh Kolla
1.2K views25 slides
React Native: React Meetup 3 by
React Native: React Meetup 3React Native: React Meetup 3
React Native: React Meetup 3Rob Gietema
846 views40 slides

Similar to React Native for multi-platform mobile applications(20)

Workshop 26: React Native - The Native Side by Visual Engineering
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native Side
Visual Engineering2.8K views
20180518 QNAP Seminar - Introduction to React Native by Eric Deng
20180518 QNAP Seminar - Introduction to React Native20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native
Eric Deng218 views
React Native: React Meetup 3 by Rob Gietema
React Native: React Meetup 3React Native: React Meetup 3
React Native: React Meetup 3
Rob Gietema846 views
Effective JavaFX architecture with FxObjects by Srikanth Shenoy
Effective JavaFX architecture with FxObjectsEffective JavaFX architecture with FxObjects
Effective JavaFX architecture with FxObjects
Srikanth Shenoy2K views
Mobile App Development: Primi passi con NativeScript e Angular 2 by Filippo Matteo Riggio
Mobile App Development: Primi passi con NativeScript e Angular 2Mobile App Development: Primi passi con NativeScript e Angular 2
Mobile App Development: Primi passi con NativeScript e Angular 2
Building cross-platform mobile apps with React Native (Jfokus 2017) by Maarten Mulders
Building cross-platform mobile apps with React Native (Jfokus 2017)Building cross-platform mobile apps with React Native (Jfokus 2017)
Building cross-platform mobile apps with React Native (Jfokus 2017)
Maarten Mulders306 views
Meteor Meet-up San Diego December 2014 by Lou Sacco
Meteor Meet-up San Diego December 2014Meteor Meet-up San Diego December 2014
Meteor Meet-up San Diego December 2014
Lou Sacco730 views
React: JSX and Top Level API by Fabio Biondi
React: JSX and Top Level APIReact: JSX and Top Level API
React: JSX and Top Level API
Fabio Biondi898 views
React Native +Redux + ES6 (Updated) by Chiew Carol
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
Chiew Carol697 views
React JS: A Secret Preview by valuebound
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
valuebound3.3K views
Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work... by Luciano Mammino
Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...
Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...
Luciano Mammino518 views

Recently uploaded

GDSC Mikroskil Members Onboarding 2023.pdf by
GDSC Mikroskil Members Onboarding 2023.pdfGDSC Mikroskil Members Onboarding 2023.pdf
GDSC Mikroskil Members Onboarding 2023.pdfgdscmikroskil
53 views62 slides
Design_Discover_Develop_Campaign.pptx by
Design_Discover_Develop_Campaign.pptxDesign_Discover_Develop_Campaign.pptx
Design_Discover_Develop_Campaign.pptxShivanshSeth6
32 views20 slides
What is Unit Testing by
What is Unit TestingWhat is Unit Testing
What is Unit TestingSadaaki Emura
24 views25 slides
Generative AI Models & Their Applications by
Generative AI Models & Their ApplicationsGenerative AI Models & Their Applications
Generative AI Models & Their ApplicationsSN
8 views1 slide
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx by
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptxlwang78
83 views19 slides

Recently uploaded(20)

GDSC Mikroskil Members Onboarding 2023.pdf by gdscmikroskil
GDSC Mikroskil Members Onboarding 2023.pdfGDSC Mikroskil Members Onboarding 2023.pdf
GDSC Mikroskil Members Onboarding 2023.pdf
gdscmikroskil53 views
Design_Discover_Develop_Campaign.pptx by ShivanshSeth6
Design_Discover_Develop_Campaign.pptxDesign_Discover_Develop_Campaign.pptx
Design_Discover_Develop_Campaign.pptx
ShivanshSeth632 views
Generative AI Models & Their Applications by SN
Generative AI Models & Their ApplicationsGenerative AI Models & Their Applications
Generative AI Models & Their Applications
SN8 views
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx by lwang78
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
lwang7883 views
Effect of deep chemical mixing columns on properties of surrounding soft clay... by AltinKaradagli
Effect of deep chemical mixing columns on properties of surrounding soft clay...Effect of deep chemical mixing columns on properties of surrounding soft clay...
Effect of deep chemical mixing columns on properties of surrounding soft clay...
AltinKaradagli9 views
MSA Website Slideshow (16).pdf by msaucla
MSA Website Slideshow (16).pdfMSA Website Slideshow (16).pdf
MSA Website Slideshow (16).pdf
msaucla76 views
Control Systems Feedback.pdf by LGGaming5
Control Systems Feedback.pdfControl Systems Feedback.pdf
Control Systems Feedback.pdf
LGGaming56 views
_MAKRIADI-FOTEINI_diploma thesis.pptx by fotinimakriadi
_MAKRIADI-FOTEINI_diploma thesis.pptx_MAKRIADI-FOTEINI_diploma thesis.pptx
_MAKRIADI-FOTEINI_diploma thesis.pptx
fotinimakriadi8 views
DevOps-ITverse-2023-IIT-DU.pptx by Anowar Hossain
DevOps-ITverse-2023-IIT-DU.pptxDevOps-ITverse-2023-IIT-DU.pptx
DevOps-ITverse-2023-IIT-DU.pptx
Anowar Hossain12 views
SUMIT SQL PROJECT SUPERSTORE 1.pptx by Sumit Jadhav
SUMIT SQL PROJECT SUPERSTORE 1.pptxSUMIT SQL PROJECT SUPERSTORE 1.pptx
SUMIT SQL PROJECT SUPERSTORE 1.pptx
Sumit Jadhav 15 views

React Native for multi-platform mobile applications

  • 2. Matteo Manchi Full stack web developer CEO at Impronta Advance Twitter: @matteomanchi
  • 3. Agenda From React to React Native Technologies behind RN Multi-platform compatibility iOS and Android native modules
  • 4. Let's start from the beginning...
  • 5. What is React.js? React is a JavaScript library for building user interfaces. Just the UI One-way data flow Virtual DOM From Facebook
  • 6. Some keywords Component: Everything is a component Props: some data passed to child component State: some internal data of a component JSX: XML-like syntax helps to define component's structure Virtual DOM: tree of custom objects representing a port of the DOM.
  • 7. Component definition import React, {Component, PropTypes} from 'react'; class CountDown extends Component { static propTypes = { seconds: PropTypes.number.isRequired }; constructor(props) { super(props); this.state = { seconds: props.seconds }; } // ... }
  • 8. Component definition - Pt.2 class CountDown extends Component { // ... componentDidMount() { setInterval(() => this.setState({ seconds: this.state.seconds-1 }), 1000); } render() { return ( <span>Time left: {this.state.seconds}</span> ); } } // Invoking < CountDown seconds="10" />
  • 9. With or without you JSX // JSX render() { return ( <span>Time left: {this.state.seconds}</span> ); } // plain js return React.createElement( "span", null, "Time left: ", this.state.seconds );
  • 10. Let's talk back about mobile applications What are common troubles developing (multi-platform) mobile application? Know how Use native APIs Performance
  • 11. React Native A framework for building native apps using React. Yeah, the same React.js of web developers Learn once, write anywhere
  • 12. What is React Native? A framework Binds JSX to iOS Cocoa Touch or Android UI Allow applications to run at near full speed
  • 13. JSX to native UI Exactly the same of React.js, but naming native components // react-dom render() { return ( <div> <span>Time left: {this.state.seconds}</span> </div> ); } // react-native render() { return ( <view> <text>Time left: {this.state.seconds}</text> </view> ); }
  • 14. What do you mean with near full speed? Yes, like Titanium.
  • 15. A lot of components ActivityIndicatorIOS DatePickerIOS DrawerLayoutAndroid Image ListView MapView Modal Navigator NavigatorIOS PickerIOS ProgressBarAndroid ProgressViewIOS PullToRefreshViewAndroid RefreshControl ScrollView SegmentedControlIOS SliderIOS Switch TabBarIOS TabBarIOS.Item Text TextInput ToolbarAndroid TouchableHighlight TouchableNativeFeedback TouchableOpacity TouchableWithoutFeedback View ViewPagerAndroid WebView
  • 16. A lot of APIs ActionSheetIOS Alert AlertIOS Animated AppRegistry AppStateIOS AsyncStorage BackAndroid CameraRoll Dimensions IntentAndroid InteractionManager LayoutAnimation LinkingIOS NativeMethodsMixin NetInfo PanResponder PixelRatio PushNotificationIOS StatusBarIOS StyleSheet ToastAndroid VibrationIOS
  • 17. Requirements for React Native Node.js 4.0 or newer. Xcode 7.0 or higher is required for iOS apps Android SDK + gradle + AVD/Genymotion for Android apps
  • 18. Getting Started $ npm install -g react-native-cli $ react-native init AwesomeProject $ cd AwesomeProject // To run iOS app $ open ios/AwesomeProject.xcodeproj // To run Android app $ react-native run-android
  • 19. Technologies behind React Native React, JSX and Virtual DOM :) Webpack loads all dependencies required by index.ios.js and index.android.js and bundle them together ES6 compatibility allows devs to use modern syntax and features NPM as module manager Chrome Debugger as powerful debugger Flow as type checker - optionally
  • 20. All JavaScript's modules Every module there are not DOM-depending can be used in React Native. Like a browser, there are some polyfills that allow to use modules you have skill with. Flexbox Geolocation Network (fetch, ajax, etc) Timers (timeout, interval, etc)
  • 21. Inline CSS-like styles Custom CSS implementation, easy to learn 'cause it's very similar to browser's CSS. With Flexbox. In JavaScript. https://facebook.github.io/react-native/docs/style.html
  • 22. import {StyleSheet} from 'react-native'; const styles = StyleSheet.create({ base: { width: 38, height: 38, }, background: { backgroundColor: LIGHT_GRAY, }, active: { borderWidth: 4/2, borderColor: '#00ff00', }, }); <View style={[styles.base, styles.background]} />
  • 23. What about multi-platforms? React Native actually supports iOS and Android. It provides a lot of components and APIs ready to use in both platforms.
  • 24. A lot of components ActivityIndicatorIOS DatePickerIOS DrawerLayoutAndroid Image ListView MapView Modal Navigator NavigatorIOS PickerIOS ProgressBarAndroid ProgressViewIOS PullToRefreshViewAndroid RefreshControl ScrollView SegmentedControlIOS SliderIOS Switch TabBarIOS TabBarIOS.Item Text TextInput ToolbarAndroid TouchableHighlight TouchableNativeFeedback TouchableOpacity TouchableWithoutFeedback View ViewPagerAndroid WebView
  • 25. Native Modules React Native is madly growing up! Each release (almost once per month) adds new features for both platforms thanks to over 500 contributors. Despite the big community around the project, can happen that the native view you're looking for isn't supported by the framework. In this case you have to wait for someone else, or make your native module (or binding native API)
  • 26. iOS Module - Pt. 1 // ShareManager.h #import < UIKit/UIKit.h > #import "RCTBridgeModule.h" @interface RNShare : NSObject < RCTBridgeModule > @end
  • 27. iOS Module - Pt. 2 // ShareManager.m @implementation ShareManager RCT_EXPORT_MODULE(); RCT_EXPORT_METHOD(share:(NSString *)text url:(NSString *)url) { NSURL *cardUrl = [NSURL URLWithString:url]; NSArray *itemsToShare = @[text, url, cardUrl]; UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil]; UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; [root presentViewController:activityVC animated:YES completion:nil]; } @end
  • 28. iOS Module - Pt. 3 var ShareManager = require('react-native').NativeModules.ShareManager; ShareManager.share('Hi from RomaJS!', 'http://www.romajs.org'); http://facebook.github.io/react-native/docs/native-modules-ios.html
  • 29. Android Module - Pt. 1 // ShareModule.java // package + imports public class ShareModule extends ReactContextBaseJavaModule { // ... public ShareModule(ReactApplicationContext reactContext) { super(reactContext); this.reactContext = reactContext; } @Override public String getName() { return "ShareManager"; } // continue }
  • 30. Android Module - Pt. 2 // ShareModule.java public class ShareModule extends ReactContextBaseJavaModule { // ... @ReactMethod public void share(String text, String url) { Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); sharingIntent.setType("text/plain"); sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, text); sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, url); Intent chooser = Intent.createChooser(sharingIntent, "Share"); chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); this.reactContext.startActivity(chooser); } }
  • 31. Android Module - Pt. 3 // SharePackage.java // package + imports public class SharePackage implements ReactPackage { @Override public List< NativeModule > createNativeModules(ReactApplicationContext reactContext) { List< NativeModule > modules = new ArrayList<>(); modules.add(new ShareModule(reactContext)); return modules; } // ... }
  • 32. Android Module - Pt. 4 // MainActivity.java // ... mReactInstanceManager = ReactInstanceManager.builder() .setApplication(getApplication()) .setBundleAssetName("index.android.bundle") .setJSMainModuleName("index.android") .addPackage(new MainReactPackage()) .addPackage(new SharePackage()) .setUseDeveloperSupport(BuildConfig.DEBUG) .setInitialLifecycleState(LifecycleState.RESUMED) .build(); // ...
  • 33. Android Module - Pt. 5 var ShareManager = require('react-native').NativeModules.ShareManager; ShareManager.share('Hi from RomaJS!', 'http://www.romajs.org'); http://facebook.github.io/react-native/docs/native-modules-android.html
  • 34. Different platform, different behavior Take some actions depending to platform
  • 35. Method 1: if/else import {Platform} from 'react-native'; const styles = StyleSheet.create({ myView: { backgroundColor: Platform.OS === 'ios' ? 'red' : 'lime' } });
  • 36. Method 2: file extension by webpack-based packager // MyStyle.ios.js export default const styles = StyleSheet.create({ myView: { backgroundColor: 'red' } }); // MyStyle.android.js export default const styles = StyleSheet.create({ myView: { backgroundColor: 'lime' } });
  • 38. Native Haters What about competitors? Cordova/Phonegap Ionic Xamarin Titanium