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

フロントエンドエンジニアが React Native を触ってみた話

フロントエンドエンジニアが React Native を触ってみた話

【RECRUIT Technologies NIGHT vol.5】リクルート流フロントエンド開発 / https://atnd.org/events/86112

Kamakura Masaya

March 09, 2017
Tweet

Other Decks in Technology

Transcript

  1. ຊ೔ͷ಺༰ • React Native ʹ͍ͭͯ • ͳͥ React Native ͳͷ͔

    • React Native ΞʔΩςΫνϟ • σϞΞϓϦ঺հ
  2. ຊ೔ͷ಺༰ • React Native ʹ͍ͭͯ • ͳͥ React Native ͳͷ͔

    • React Native ΞʔΩςΫνϟ • σϞΞϓϦ঺հ
  3. React Native ͱ͸ • Facebook͕த৺ͱͳͬͯ։ൃΛ͍ͯ͠ΔωΠςΟϒΞϓϦ։ൃͷͨΊͷ ϑϨʔϜϫʔΫ • ಛ௃ • Webٕज़/஌ࣝ(HTML/CSS/JavaScript/React)Λ࢖ͬͯɺiOS/Android

    ͷΞϓϦΛ։ൃͰ͖Δ • Native UI (WebViewͰͳ͍) • ϋΠύϑΥʔϚϯε • ։ൃޮ཰͕ྑ͍(Live Reload, Hot Reloading)
  4. • Web ΞϓϦͰ React Λ࠾༻͢Δέʔε͕૿͍͑ͯΔ • React Λॻ͚Δਓࡐ͕૿͍͑ͯΔ • ਓһ֬อָ͕ʹͳΔ

    • React Native Λ࢖͏͜ͱʹΑͬͯ React(Web) Λ࢖͑ΔΤϯδχΞ ͷ࢓ࣄ෯Λ֦େͰ͖Δ
  5. WebView → React Native https://engineering.instagram.com/react-native-at-instagram-dd828a9a90c7 • WebView ͷ໰୊఺ • ىಈ͕஗͍

    • UXʹωΠςΟϒײ͕ͳ͍ • React Native Խ͢Δ͜ͱʹΑΓ • ىಈ࣌ؒͱUXΛେ෯ʹվળ
  6. True Native → React Native https://engineering.instagram.com/react-native-at-instagram-dd828a9a90c7 • ίʔυͷڞ௨Խ • Live

    Reload, Hot Reloadingͷ ͓͔͛ͰίϯύΠϧɺΠϯετʔ ϧαΠΫϧ͕ૣ͘ͳΓ։ൃύ ϑΥʔϚϯε޲্ • ΑΓૣ͘ػೳΛϦϦʔεͰ͖Δ Α͏ʹͳͬͨ
  7. React(Web)/React Nativeͱͷҧ͍ ϙΠϯτ View ComponentɺϥΠϑελΠϧ౳ͷߟ͑ํ͸ಉ༷ ཁૉ͸ React Native ͷ Component

    ʹॻ͖׵͑ Style CSSͷ஌͕ࣝͦͷ··࢖͑Δ Flexbox ʹΑΔ Component ͷ഑ஔ Data Flow fluxΞʔΩςΫνϟͷར༻͕Մೳ Navigation ʢը໘ભҠʣ react-routerϥΠΫͳϞδϡʔϧ͕͋Δ
  8. View render() { return ( <View style={styles.container}> <Text style={styles.title}> My

    Counter App! </Text> <Text style={styles.value}> {this.state.count} </Text> <Button title="Add +1" onPress={() => this.inc()} /> </View> ); }
  9. Style const styles = StyleSheet.create({
 container: {
 flex: 1,
 alignItems:

    'center',
 paddingTop: 65,
 backgroundColor: '#F5FCFF',
 },
 title: {
 fontSize: 25,
 margin: 10,
 },
 value: {
 color: '#333333',
 marginBottom: 5,
 fontSize: 40,
 },
 }); • CSS in JS • StyleSheet.createͰఆٛͨ͠ ελΠϧΛͦΕͧΕͷ ComponentʹదԠ͢Δ • Componentͷ഑ஔ͸Flexbox Ͱࢦఆ͢Δ
  10. Style const styles = StyleSheet.create({
 container: {
 flex: 1,
 alignItems:

    'center',
 paddingTop: 65,
 backgroundColor: '#F5FCFF',
 },
 title: {
 fontSize: 25,
 margin: 10,
 },
 value: {
 color: '#333333',
 marginBottom: 5,
 fontSize: 40,
 },
 }); • CSS in JS • StyleSheet.createͰఆٛͨ͠ ελΠϧΛͦΕͧΕͷ ComponentʹదԠ͢Δ • Componentͷ഑ஔ͸Flexbox Ͱࢦఆ͢Δ
  11. Style render() { return ( <View style={styles.container}> <Text style={styles.title}> My

    Counter App! </Text> <Text style={styles.value}> {this.state.count} </Text> <Button title="Add +1" onPress={() => this.inc()} /> </View> ); } const styles = StyleSheet.create({
 container: {
 flex: 1,
 alignItems: 'center',
 paddingTop: 65,
 backgroundColor: '#F5FCFF',
 },
 title: {
 fontSize: 25,
 margin: 10,
 },
 value: {
 color: '#333333',
 marginBottom: 5,
 fontSize: 40,
 },
 }); StyleSheet render()
  12. Style const styles = StyleSheet.create({
 container: {
 flex: 1,
 alignItems:

    'center',
 paddingTop: 65,
 backgroundColor: '#F5FCFF',
 },
 title: {
 fontSize: 25,
 margin: 10,
 },
 value: {
 color: '#333333',
 marginBottom: 5,
 fontSize: 40,
 },
 }); • CSS in JS • StyleSheet.createͰఆٛͨ͠ ελΠϧΛͦΕͧΕͷ ComponentʹదԠ͢Δ • Componentͷ഑ஔ͸Flexbox Ͱࢦఆ͢Δ
  13. Style(Flexbox:flexDirection) const styles = StyleSheet.create({
 container: {
 flex: 1,
 flexDirection:

    'row', alignItems: 'center',
 paddingTop: 65,
 backgroundColor: '#F5FCFF',
 },
 title: {
 fontSize: 25,
 margin: 10,
 },
 value: {
 color: '#333333',
 marginBottom: 5,
 fontSize: 40,
 },
 });
  14. Style(Flexbox:justifyContent) const styles = StyleSheet.create({
 container: {
 flex: 1,
 alignItems:

    ‘center', justifyContent: ‘space-between’,
 paddingTop: 65,
 backgroundColor: '#F5FCFF',
 },
 title: {
 fontSize: 25,
 margin: 10,
 },
 value: {
 color: '#333333',
 marginBottom: 5,
 fontSize: 40,
 },
 });
  15. Style(FAQ) • CSS Modules ͕࢖͍͍ͨʁ • ελΠϧఆٛΛผϑΝΠϧ(.css)ʹͯ͠importͯ͠ར༻͢Δํ๏ • React Native

    ͷελΠϧఆٛ͸ CSS ʹࣅͯΔ͚Ͳ΍ͬͺΓผ෺ • ίϯϙʔωϯτʹ෇༩Ͱ͖ͳ͍ϓϩύςΟ͕͋ΔͱܯࠂΛͩ͠ ͯ͘ΕΔ • ҰԠɺ࣮ݱͰ͖Δύοέʔδ͸͋Δ • https://github.com/sabeurthabti/react-native-css
  16. • ٙࣅΫϥε͕࢖͍͍ͨʁ • JSଆͰ൑ఆͯ͠ελΠϧࢦఆ͢Δඞཁ͕͋Δ • React Native ൛ Radium Έ͍ͨͳͷ΋ଘࡏ͢Δ

    • https://github.com/vitalets/react-native-extended-stylesheet Style(FAQ) :first-child i === 0 :last-child i === arr.length - 1 :nth-child(even) i % 2 :nth-child(odd) !(i%2)
  17. • CSS ʹ׳Ε͗͢Δͱ React Native ͷελΠϧఆٛ͸࠷ॳށ࿭͏͔΋ ͠Εͳ͍ • ֎෦ϞδϡʔϧͰղܾ͢Δબ୒ࢶ΋͋Δ͕ɺReact Native

    ͷόʔ δϣϯ্͕͕Δ͜ͱͰղܾ͞ΕΔ͜ͱ΋͋ΔͷͰɺ·ͣ͸ඪ४ͷ ΍ΓํͰ΍ͬͨ΄͏͕ྑ͍ Style
  18. iOS/Androidͷҧ͍ • Write Once, Run Anywhere? - No! • ϓϥοτϑΥʔϜ͝ͱʹUI/UXͷϕετϓϥΫςΟε͸ҧ͏

    • φϏήʔγϣϯʢ໭ΔϘλϯʣ • ϋϯόʔΨʔϝχϡʔ / λϒϝχϡʔ
  19. Platform.OS, Platform.select if (Platform.OS === ‘android’) { // Android ͷΈ࣮ߦ͢Δίʔυ

    } else if (Platform.OS === ‘ios’) { // iOS ͷΈ࣮ߦ͢Δίʔυ } // files ComponentIOS.js ComponentAndroid.js // component const Component = Platform.select({ ios: () => require('ComponentIOS'), android: () => require('ComponentAndroid'), })(); <Component />;
  20. ϑΝΠϧࣗମΛ෼͚Δ // Files example.ios.js example.android.js // Usage import example from

    './example'; // Ϗϧυ࣌ʹ൑ఆ͞ΕΔ $ react-native run-ios $ react-native run-android
  21. ຊ೔ͷ಺༰ • React Native ʹ͍ͭͯ • ͳͥ React Native ͳͷ͔

    • React Native ΞʔΩςΫνϟ • σϞΞϓϦ঺հ
  22. ͳͥ React Native ͳͷ͔ • Ϣʔβʔ਺ͷଟ͍ΞϓϦͰ࠾༻͞Ε͍ͯΔʢFB, Instagram, Airbnbʣ • (R-Tech)FEඪ४Ͱ͋Δ

    React/Redux ͷαʔϏεͰ͋Ε͹ɺWeb൛Ͱ ։ൃͨ͠ίʔυͷڞ༗Խ͕Մೳ • FEඪ४Ͱ։ൃͰ͖ΔΤϯδχΞ૿Ճத
  23. ͳͥ React Native ͳͷ͔ • Web ΞϓϦΛ࡞ͬͨޙʹεϚϗΞϓϦ൛΋ཉ͍͠ͱ͍͏ཁ๬΋͋Δ • WebView ϕʔεͷαʔϏε͕ଟ͍ͷͰ

    React Native ԽʹΑΔύ ϑΥʔϚϯε޲্͕ૂ͑Δ • ୺຤ੑೳ΋ྑ͘ͳ͍ͬͯͷͰɺ౤ࢿޮՌ͸ΞϓϦಛੑʹґଘ͢Δ
  24. ຊ೔ͷ಺༰ • React Native ʹ͍ͭͯ • ͳͥ React Native ͳͷ͔

    • React Native ΞʔΩςΫνϟ • σϞΞϓϦ঺հ
  25. Data Flow Touch Event Handle Event Main Thread Native Modules

    MessageQueue JS Thread UITouch [ ‘EventEmitter’, ‘receiveTouches’, [‘topTouchStart’, {‘target’: 12, ’x’: 42, ‘y’: 106}] ]
  26. Data Flow Touch Event Handle Event Main Thread Native Modules

    MessageQueue JS Thread UITouch setState(), render()
  27. Data Flow Touch Event Handle Event Main Thread Native Modules

    MessageQueue JS Thread UITouch UIManager.u pdate…. setState(), render() [ ‘UIManager’, ‘update’, [viewID, attributes] ]
  28. Data Flow Touch Event Handle Event Main Thread Native Modules

    MessageQueue JS Thread UITouch UIManager.u pdate…. setState(), render() [ ‘UIManager’, ‘update’, [viewID, attributes] ]
  29. Data Flow Touch Event Handle Event Main Thread Native Modules

    MessageQueue JS Thread UITouch UIManager.u pdate…. setState(), render() [ ‘UIManager’, ‘update’, [8, {‘text’: ‘6’}] ]
  30. Data Flow Touch Event Handle Event Main Thread Native Modules

    Event Queue JS Thread UITouch UIManager.u pdate…. setState(), render() Update UI dispatch_async()
  31. Data Flow Touch Event Handle Event Main Thread Native Modules

    Event Queue JS Thread UITouch UIManager.u pdate…. setState(), render() Update UI [UIManager updateView: @8, props:@{@“text”: @“6”}]
  32. ຊ೔ͷ಺༰ • React Native ʹ͍ͭͯ • ͳͥ React Native ͳͷ͔

    • React Native ΞʔΩςΫνϟ • σϞΞϓϦ঺հ
  33. • ײ૝ • Web ͷ React Λ࢖ͬͨ͜ͱ͕͋Ε͹͙͢ʹॻ͚Δ/Θ͔Δ • Web ʹ͍ۙײ֮Ͱͷ։ൃ

    • npm • Cmd-RͰϦϩʔυɺLive Reload, Hot Reloading DEMOΞϓϦ
  34. • ײ૝ଓ͖ • ը໘͸100%࡞Γ௚͠ • Web Ͱ࡞ͬͨίϯϙʔωϯτΛࢀߟʹͰ͖Δ • Web ൛Λ

    Flexbox Ͱ࡞͍ͬͯͳ͍ͱߟ͑Δ͜ͱ͕૿͑Δ • ڞ௨ԽͰ͖Δίʔυ͕ଟ͍ • Redux(Actions, Reducers)ɺBFF ͱͷ௨৴෦෼ • mono-repo Ͱߏ੒Λߟ͑Δͷ΋͋Γ DEMOΞϓϦ
  35. • ײ૝ଓ͖ • Objective-C(Swift),Java ͷ஌͕ࣝඞཁʹͳΔͱ͜Ζ͸͍·ͷͱ͜ Ζͳ͍ • React Native ͕ఏڙ͍ͯ͠ͳ͍APIΛ࢖͍͍ͨͱ͔ɺ

    Cocoapods Ͱಋೖͨ͠ϥΠϒϥϦͱ Bridge ͍ͨ͠ͱ͔ͳ͚Ε ͹ɾɾɾ • ࠔͬͨͱ͖ͷͨΊʹνʔϜʹ1ਓ͸ Native ʹৄ͍͠ਓ͸ཉ͍͠ DEMOΞϓϦ