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

React NativeアプリにRecoilを導入した話

46kuro
October 02, 2021

React NativeアプリにRecoilを導入した話

46kuro

October 02, 2021
Tweet

More Decks by 46kuro

Other Decks in Technology

Transcript

  1. ൃද಺༰ʹ͍ͭͯ w ಺༰ w 3FDPJMʹ͍ͭͯͬ͘͟Γઆ໌ w 4UBUF.BOBHFNFOUʹ͍ͭͯͷৄࡉͳઆ໌΍ൺֱݕ౼͸ߦ͍·ͤΜ w 3FBDU/BUJWF։ൃΛ࢝Ίͯ൒೥͘Β͍ͳͷͰɺؒҧ͍౳͋Ε͹͝ࢦఠ͓ئ͍͠·͢ w

    ର৅ऀ w 4UBUF.BOBHFNFOU-JCSBSZΛ࢖ͬͨ͜ͱ͕ͳ͍ํ w 3FDPJMʹ͍ͭͯؾʹͳ͍ͬͯΔํ w 3FBDU/BUJWFͰ໰୊ͳ͘࢖͑Δ͔ؾʹͳ͍ͬͯΔํ
  2. 4UBUF.BOBHFNFOU-JCSBSZ 4UBS਺LҎ্ͷ4UBUF.BOBHFNFOU-JCSBSZ w 4UBS਺ɿL w ࠷΋ଟ͘࢖ΘΕ͍ͯΔ 3FEVY .PC9 w 4UBS਺ɿL

    w 4JNQMF 94UBUF w 4UBS਺ɿL w ঢ়ଶભҠɺՄࢹԽπʔϧ΋ଘࡏ w 4UBS਺ɿL w (SBQI2-Ͱ࢖༻ BQPMMPDMJFOU 3FDPJM w 4UBS਺ɿL w 4JNQMFɺ'$Ͱ࢖༻ ;VTUBOE w 4UBS਺ɿL w 'MVYϕʔε IUUQTHJUIVCDPNPMFHSKVNJOBXFTPNFSFBDUTUBUFNBOBHFNFOU
  3. 3FDPJM w 4UBUF.BOBHFNFOU-JCSBSZ w 'BDFCPPL͕։ൃ w &YQFSJNFOUBMͱ͍͏Ґஔ͚ͮ w 3FBDUͷ)PPLʹ΋ͱ͍ͮͨ*OUFSGBDF w

    IUUQTSFDPJMKTPSHEPDTJOUSPEVDUJPONPUJWBUJPO w +PUBJͳͲͷɺ3FDPJMʹӨڹΛड͚ͨ-JCSBSZ΋ଘࡏ
  4. 3FDPJM const TextState = atom({key: 'Text', default: ‘'}); const Content:

    React.FC = () => { const [text, setText] = useRecoilState<string>(TextState); return ( <TextInput value={text} onChange={(event) => { setText(event.nativeEvent.text) }} /> ) } w "UPNΛఆٛ w $PNQPOFOU಺෦Ͱ3FDPJM4UBUFΛఆٛ ˠVTF4UBUFͱಉ͡Α͏ͳ࢖͍ํͰ࢖༻Մೳ w ͦͷଞʹ΋4FMFDUPS΍"UPN4FMFDUPSΛα ϙʔτ͢ΔͨΊͷ"1*͕͋Δ
  5. $POUSPMMFE$PNQPOFOUT w ࣗ਎Ͱঢ়ଶΛอ࣋͢Δ$PNQPOFOUT w 3FBDUJOQVU UFYUBSFB TFMFDUͳͲ w 3FBDU/BUJWF5FYU*OQVU 4XJUDIͳͲ

    w 3FBDU/BUJWFͷ$POUSPMMFE$PNQPOFOUͷ4UBUFͱͯ͠3FDPJMͷ4UBUFΛ࢖༻ ͢Δͱɺෆ੔߹͕ൃੜ͢Δ IUUQTSFBDUKTPSHEPDTGPSNTIUNMDPOUSPMMFEDPNQPOFOUT
  6. ྫ5FYU*OQVU VTF4UBUFΛ࢖༻͢Δ const Content: React.FC = () => { const

    [text, setText] = useState<string>(''); return ( <TextInput value={text} onChange={(event) => { setText(event.nativeEvent.text) }} /> ) }
  7. ྫ5FYU*OQVU 3FDPJM4UBUFΛ࢖༻͢Δ const TextState = atom({key: 'Text', default: ‘'}); const

    Content: React.FC = () => { const [text, setText] = useRecoilState<string>(TextState); return ( <TextInput value={text} onChange={(event) => { setText(event.nativeEvent.text) }} /> ) }
  8. ݪҼ w ͓ͦΒ͘ TFU3FDPJM4UBUF಺෦Ͱඇಉظॲ ཧ͕૸͍ͬͯΔ w ͓ͦΒ͘ 5FYU*OQVU͸ಉظతʹ஋͕ߋ৽ ͞ΕΔ͜ͱΛલఏʹ$PNQPOFOU͕࡞ΒΕ ͍ͯΔ

    w Ұॠ͚ͩWBMVF͕ݩͷ஋ʹ໭ΔͨΊɺ͜ ͷΑ͏ͳࣄ৅͕ى͖Δ w 3FDPJMΛ࢖༻͠ͳͯ͘΋ɺӈͷΑ͏ͳίʔ υͩͱTFU3FDPJM4UBUFͱಉ݁͡ՌʹͳΔ const Content: React.FC = () => { const [text, setText] = useState<string>(''); return ( <TextInput value={text} onChange={(event) => { const text = event.nativeEvent.text; setTimeout(() => { setText(text) }, 0) }} style={styles.sectionContainer} /> ) }
  9. ରࡦ w جຊతʹ͸ɺը໘಺ͷΈͰߋ৽͞Ε ͨ஋Λ࢖༻͢Δ৔߹ʹ͸ɺ3FDPJMͷ 4UBUFΛ࢖༻ͤͣɺTFU4UBUFΛ༻͍ Δ w Ͳ͏ͯ͠΋3FDPJMΛ࢖͍͍ͨ৔߹ ͸ɺVTF4UBUFͰҰ࣌తʹ஋Λߋ৽͢ Δ͜ͱͰճආͰ͖Δ

    const TextState = atom({key: 'Text', default: ''}); const Content: React.FC = () => { const [text, setText] = useState<string>(''); const setGlobalText = useSetRecoilState(TextState); useEffect(() => { setGlobalText(text); }, [text]) return ( <TextInput value={text} onChange={(event) => { const text = event.nativeEvent.text; setText(text) }} style={styles.sectionContainer} /> ) }
  10. .FNPSZ-FBL w ͍͔ͭ͘*TTVFT͕͕͍͋ͬͯΔ w IUUQTHJUIVCDPNGBDFCPPLFYQFSJNFOUBM3FDPJMJTTVFT w IUUQTHJUIVCDPNGBDFCPPLFYQFSJNFOUBM3FDPJMJTTVFT w IUUQTHJUIVCDPNGBDFCPPLFYQFSJNFOUBM3FDPJMJTTVFT w

    IUUQTHJUIVCDPNGBDFCPPLFYQFSJNFOUBM3FDPJMJTTVFT w BUPN'BNJMZ΍TFMFDUPS'BNJMZͳͲͷ"1*Λ࢖༻͢Δͱى͖Δʁ w ͜ͷෆ۩߹Λཧ༝ʹ+PUBJΛ࢖༻͍ͯ͠Δͱ͍͏ίϝϯτ΋ݟ͔ͭͬͨ