Share
Embed
[社内LT]初見から実務でReact−NativeやったAndroidエンジニアの所感とRN概要
きりみん
March 30, 2018
Transcript
26 slides
-
લఏ • ࣗʹ͍ͭͯ • Android։ൃྺ6͘Β͍ • ϑϩϯτΤϯυझຯͰগ͠ᴩͬͨ͘Β͍ • ࣮Ͱϑϩϯτ։ൃܦݧͳ͠ •
JavaScript࣮Ͱ΄΅ະܦݧ • React-Nativeશʹॳݟ • ReactॳΊͯ • ReduxॳΊͯ -
લఏ • Ҋ݅ʹ͍ͭͯ • iOSΤϯδχΞ2ਓʴAndroidΤϯδχΞ1ਓ+֎෦ͷํͷ̐ਓମ੍ • ࠷ॳগࣝ͠ͷ͋Δ֎෦ͷํʹج൫෦ͷ࣮ΛͬͯΒͬͨ • ଞ3ਓRNॳݟͰɺUIܥIssueͳͲ͔ΒঃʑʹҊ݅ʹίϛοτ •
ΞϓϦͷنͦͦ͜͜େ͖͍+σβΠϯཁٻߴΊ • UIجຊϚςϦΞϧσβΠϯϕʔε + OSඪ४ • FlowʴReduxߏ -
JavaScriptʹ͍ͭͯ • ࠷৽ͷJavaScriptͷγϯλοΫε͕͑Δ • Promise, async/await, constʹΑΔείʔϓ੍ޚ, Ξϩʔؔ… • ࢥͬͨΑΓշదɻܕ·ΘΓҎ֎ͦΜͳʹετϨεΛײ͡ͳ
͍ • ͨͩ͠ඪ४ؔͷශऑ͞JVMҭͪʹΧϧνϟʔγϣοΫ • ࠷৽ͷJSֶशίετͦΕ΄Ͳߴ͘ͳ͍ -
Flowʹ͍ͭͯ • JSʹܕࢦఆΛՃग़དྷΔ • ؔͷҾΓʹܕΛ໌ࣔग़དྷΔͷ༗Γ͍ • nullableܕࢦఆͰ͖Δ • ͨͩ͠unwrapͳͲѻ͍͕ෆศͳͷͰ͋·Γ͍ͨ͘ͳ͍ҹ •
ಠࣗܕఆٛclassͰͳ͘Objectͷtype alias • ίʔυิ͕ޮ͘Α͏ʹͳΔͷͰศར • ͔Γ͖͍ͬͯΔnullνΣοΫͳܕఆٛͳͲશྗͰFlowʹ४ڌ͠Α͏ͱ ͢ΔͱΘΓͱμϧ͍ -
Reactʹ͍ͭͯ • JS্ͰHTMLͱAndroidͷViewͷதؒͷΑ͏ͳʮComponentʯΛ Έ߹ΘͤͯUIΛߏங͢Δ(JSX) • AndroidͰݴ͑layoutͷxmlʹJSίʔυΛॻ͘Α͏ͳΠϝʔ δ • ײతͩ͠ૉૣ࣮͘ग़དྷͯྑ͍ •
ͨͩ͠ViewͱUIϩδοΫ͕ີ݁߹͕ͪ͠(ޙड़͠·͢) • Reactࣗମͷֶशίετ͍ɻॳݟͰงғؾͰॻ͚Δ -
Redux • σʔλϑϩʔͷ͋ΓํΛఆٛͨ͠ύλʔϯɺٴͼͦͷϑϨʔϜϫʔΫ • શʹॳݟࡴ͠ • ղઆͳ͠ʹίʔυΛಡΜ͚ͩͩͰԿ͕ى͍ͬͯ͜Δͷ͔શ͔͘Βͳ͍ • ͨ͘͞Μͷղઆهࣄ͕͋Δ͕ಡΜͰ͙͢ʹཧղͰ͖ͳ͍ •
େྔʹղઆهࣄΛಡΈړ࣮ͬͯࡍʹ৮ͬͯΑ͏͘ཧղͰ͖Δͱ͍͏ײ͡ • ͔Ε·͊ͳΔ΄Ͳͱ͍͏ײ͡ • ͨͩ͠େྔͷϘΠϥʔςϯϓϨʔτ -
React-NativeͰͷUI࣮ • ϨΠΞτࣗମCSSͰstyleΛఆٛ͢Δ • flexboxϕʔεͰॻ͚Δ • CSSͷ͕ࣝ͋ΕϨΠΞτߏஙεϜʔζ • ٯʹCSS͕͔Βͳ͍ͱຖճάάΔ͜ͱʹͳΔ •
OSࠩComponentͰٵऩ͞ΕΔͷ͋Δ͕ɺͦ͏Ͱͳ͍ͷذରԠ marginLeft: Platform.OS === 'ios' ? -8 : -6, -
React-NativeͰͷUI࣮ import React, { Component } from 'react'; import {
Text, View } from 'react-native'; class WhyReactNativeIsSoGreat extends Component { render() { return ( <View> <Text> If you like React on the web, you'll like React Native. </Text> <Text> You just use native components like 'View' and 'Text', instead of web components like 'div' and 'span'. </Text> </View> ); } } ←ComponentΫϥεఆٛ ←render()ؔͰϨΠΞτΛߏஙͯ͠ฦ͢ ←Component(View)Λྻڍͯ͠UIߏங -
React-NativeͰͷUI࣮ import React, { Component } from 'react'; import {
AppRegistry, StyleSheet, Text, View } from 'react-native'; export default class LotsOfStyles extends Component { render() { return ( <View> <Text style={styles.red}>just red</Text> <Text style={styles.bigblue}>just bigblue</Text> <Text style={[styles.bigblue, styles.red]}>bigblue, then red</Text> <Text style={[styles.red, styles.bigblue]}>red, then bigblue</Text> </View> ); } } const styles = StyleSheet.create({ bigblue: { color: 'blue', fontWeight: 'bold', fontSize: 30, }, red: { color: 'red', }, }); ←Styleఆٛ(CSS) -
React-NativeͰͷUIϩδοΫ • શͯͷUIComponentͱ͍͏୯ҐͰߟ͑Δ • ը໘શମComponentɺUIύʔπComponent • PropsͱStateͱ͍͏ಛผͳঢ়ଶΛ࣋ͭ • PropsComponentੜ࣌ʹ֎෦͔Β͞ΕΔύϥϝʔλɺ ͞Εͨޙมߋ͠ͳ͍
• StateComponent෦ʹӅṭ͞ΕͨύϥϝʔλɺComponent ෦͔Β͔͠มߋ͠ͳ͍ -
React-NativeͰͷUIϩδοΫ • શͯͷUIComponentඳը࣌ʹݺΕΔrender()ؔͰߏங ͢Δ • AndroidͳͲͷΑ͏ʹଞͷComponentͷΠϯελϯε Λऔಘͯ͠Λૢ࡞ͨ͠Γ͠ͳ͍ • ͡Ό͋Ͳ͏ͬͯUIΛߋ৽͢Δͷʁ •
State͕มߋ͞ΕΔʹrender()͕ݺΕComponent͕࠶ ඳը͞ΕΔ -
React-NativeͰͷUIϩδοΫ • ྫɿϘλϯΛԡͨ͠ΒςΩετΛදࣔ͢Δ type State = { isPressed: boolean }
class WhyReactNativeIsSoGreat extends Component<State>{ constructor (props: Props) { super(props) this.state = { isPressed: false } } render() { const pressedTextView = this.state.isPressed ? (<Text>pressed</Text>) : null return ( <View> <Button onPress={() => { setState({isPressed: true}) }} /> {pressedTextView} </View> ); } } ←Stateఆٛ(Flow) ←StateॳظԽ(Flow) ↓StateͷʹΑͬͯඳըΛม͑Δ ←ϘλϯԡԼ࣌ʹsetStateͰঢ়ଶΛมߋ render()͕ؔ࠶ݺΕΔ -
React-NativeͰͷUIϩδοΫ • جຊతʹશͯͷඳըॲཧ͕render()ʹू͞ΕΔͷͰUIϩδοΫ, Viewͷ ঢ়ଶΛ͍͍͢ • render()ؔංେԽ͕ͪ͠ • ϨΠΞτͱUIϩδοΫ/υϝΠϯϩδοΫ͕ີ݁߹ʹͳΓ͕ͪ •
ComponentઃܭͱϞσϧઃܭΛͪΌΜͱΒͳ͍ͱਏͦ͏ • ڞ௨෦ͷComponentԽਖ਼͍͕͠ɺ্ख͘Βͳ͍ͱ͔͑ͬͯσ βΠϯมߋը໘ʹΑΔඍົͳࠩͳͲͷϏδωεཁ݅ʹऑ͘ͳΔ -
RN + Reduxͷ߹ • ΞϓϦͷશͯͷঢ়ଶStoreͱ͍͏ΦϒδΣΫτʹอ࣋͞ΕΔ(γϯάϧτϯ) • ReduxΛ͏߹setState()ͰStateΛߋ৽͢ΔมΘΓʹStoreͷঢ়ଶΛߋ৽ ͢ΔࣄͰComponentΛߋ৽͢Δ • Actionͱ͍͏ΠϕϯτʹΑͬͯStoreͷߋ৽ϦΫΤετΛૹΔˠStoreͷߋ
৽ʹΑ֤ͬͯComponentͷ࠶ඳը͕ߦΘΔˠStoreͷঢ়ଶΛݩʹUIΛඳը͢ ΔͨΊը໘͕ߋ৽͞ΕΔ • ReduxͰͷStoreͷߋ৽࣌ʹݺΕComponentͷPropsʹม͢Δͷ͕ mapStateToPropsؔɹ(͕͜͜ॳݟͰࠇຐज़ͬΆ͘ݟ͑Δ) -
RN + Reduxͷ߹ • ྫ • API /events͔ΒΠϕϯτใΛऔಘ͠ɺStoreʹอଘ͢ΔActionʮfetchEventsʯ͕͋Δ • Ұཡը໘ͰComponentੜ࣌ʹfetchEventsΛݺͿ
• APIϦΫΤετʹޭ͠Store͕ߋ৽͞ΕͨΒҰཡը໘ͷrender()͕ݺͼ͞ΕΠϕϯτ ใ͕දࣔ͞ΕΔ • ৄࡉը໘ͰΠϕϯτฤूޙʹfetchEventsΛݺͼΠϕϯτใΛߋ৽͢Δ • Storeͷߋ৽ʹΑΓৄࡉը໘ɺҰཡը໘ͦΕͧΕͰrender()͕ݺͼ͞ΕComponent͕ߋ৽ ͞ΕΔ • ը໘ʹStoreΛࢀর͢ΔComponent͕͍ͭ͋ͬͯ͘ಉ͡Α͏ʹ࠶ඳը͞ΕΔͨΊৗʹ Componetͱσʔλ͕ಉظ͞ΕΔ -
RN + Reduxॴײ • ReduxͰσʔλͷϑϩʔΛఆ͍ٛͯ͠Δ͚ͩͳͷͰɺViewϨΠ ϠʔυϝΠϯϨΠϠʔͷ͚ͳͲఆٛ͞Ε͍ͯͳ͍ • ʮReduxΛ࠾༻͢Δʯͱ͍͏ࣄ͚ͩΛܾΊͯ։ൃΛਐΊͯ͠·ͬ ͨ݁ՌɺStore͔ΒσʔλΛऔಘͨ͠ޙͷUIϩδοΫυϝΠϯ ϩδοΫͷઃܭʹҰ؏ੑ͕ͳ͘ΧΦεԽͯ͠͠·ͬͨ
• ReduxΛ͏͚ͩͰʮFat Activityʯղܾ͠ͳ͍ • ReduxΛ͏߹ͰAndroidͰͷCleanArchitectureͷΑ͏ͳϨ ΠϠʔυΞʔΩςΫνϟΛ࠾༻ͨ͠ํ͕Αͦ͞͏ -
։ൃڥ • Atom, Visual Studio Code, WebStorm(IntelliJ)ͳͲ • ࠷ॳVS CodeͰ్࣮͍͕ͯͨ͠த͔ΒIntelliJʹҠߦͨ͠
• IntelliJͷIDEαϙʔτѹతʹศར • ڧྗͳίʔυิɺimportิͳͲɺࢀরݕࡧɺrename ͳͲ • ωΠςΟϒ։ൃग़ऀͱͯ͠IntelliJඞਢͱ͍͏ҹ -
AndroidΤϯδχΞతͳRNײ • ϚϧνϓϥοτϑΥʔϜϑϨʔϜϫʔΫਖ਼͋·Γ৴༻͠ ͍ͯͳ͔͕ͬͨɺ૾Ҏ্ʹΑ͘ग़དྷ͍ͯΔ • JSɺReactͰͷ࣮ࢥͬͨΑΓਏ͘ͳ͍ • جຊϑϩϯτٕज़ελοΫͳͷͰɺRNΛֶश͢Δ͜ͱͰ Web։ൃεΩϧͷΩϟονΞοϓʹͳΔ •
ֶशίετΊɻଟAndroidΤϯδχΞ͕iOSΛֶͿΑΓ RNΛֶͿํ͕࣮ϨϕϧʹͳΔ·Ͱͷϋʔυϧ͍