React-native-router-flux: ๋ชจ๋‹ฌ ์‚ฌ์šฉ๋ฒ•

์— ๋งŒ๋“  2017๋…„ 08์›” 03์ผ  ยท  16์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: aksonov/react-native-router-flux

๋ฒ„์ „

  • ๋ฐ˜์‘ ๋„ค์ดํ‹ฐ๋ธŒ ๋ผ์šฐํ„ฐ ํ”Œ๋Ÿญ์Šค v4b15
  • ๋ฐ˜์‘ ๋„ค์ดํ‹ฐ๋ธŒ v46

๋‚˜๋Š” ๋ชจ๋‹ฌ์— ๋Œ€ํ•œ ๋ฌธ์„œ๊ฐ€ ์—†๊ณ  ์˜ˆ์ œ์— ํŠน์ • ์žฅ๋ฉด์ด ๋ชจ๋‹ฌ๋กœ ํ‘œ์‹œ๋˜๊ณ  ๋‹ค๋ฅธ ์žฅ๋ฉด์ด ํ‘œ์‹œ๋˜์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์„ค๋ช…์ด๋‚˜ ํ‘œ์‹œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋‹ฌ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ ์ •๋ง ์–ด๋ ค์›€์„ ๊ฒช๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ ๋‚ด ์ฝ”๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

<Router>
  <Scene key="root" hideNavBar hideTabBar>
    <Scene key="tabBar" tabs>
      <Scene key="listings" component={Listings} title="Listings" initial right={MapButton} />
      <Scene key="favorites" component={Favorites} title="Favorites" />
      <Scene key="messages" component={Messages} title="Messages" />
      <Scene key="profile" component={Profile} title="Profile" />
    </Scene>
    <Scene key="modal" modal>
      <Scene key="map" component={Map} title="Map" hideNavBar />
    </Scene>
    <Scene key="listingDetail" component={ListingDetail} title="Listing Detail" clone />
  </Scene>
</Router>;

Map ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ๋ชจ๋‹ฌ๋กœ ํ‘œ์‹œ๋˜๋„๋ก ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

MapButton ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

const MapButton = () =>
  <TouchableWithoutFeedback onPress={Actions.map}>
    <Text>Map</Text>
  </TouchableWithoutFeedback>;

rightText๊ฐ€ ๋งค์šฐ ๋ฒ„๊ทธ๊ฐ€ ๋งŽ๊ณ  ์ ˆ๋ฐ˜์˜ ์‹œ๊ฐ„์„ ๋ Œ๋”๋งํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

๋•๋ถ„์— ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ด€๋ จ ๋ฌธ์„œ๊ฐ€ ์—†๋Š”๋ฐ ๋‚ด๊ฐ€ ์–ด๋–ป๊ฒŒ ์ดํ•ดํ•˜๊ธฐ๋ฅผ ๊ธฐ๋Œ€ํ•ฉ๋‹ˆ๊นŒ? ์ด ๋ชจ๋‹ฌ ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ๋ฌธ์ œ๋ฅผ ์ œ๊ธฐํ•ด ์™”์œผ๋ฉฐ ์•„๋งˆ๋„ ๋ฌธ์„œ์˜ ํ•„์š”์„ฑ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ชจ๋“  16 ๋Œ“๊ธ€

root ์žฅ๋ฉด์„ ๋ชจ๋‹ฌ ์•ˆ์— ๋„ฃ์–ด๋ณด์‹ญ์‹œ์˜ค.
์ด์™€ ๊ฐ™์ด:

<Router>
    <Scene key="modal" modal>
       <Scene key="root" hideNavBar hideTabBar>
            <Scene key="tabBar" tabs>
              <Scene key="listings" component={Listings} title="Listings" initial right={MapButton} />
              <Scene key="favorites" component={Favorites} title="Favorites" />
              <Scene key="messages" component={Messages} title="Messages" />
              <Scene key="profile" component={Profile} title="Profile" />
            </Scene>
        </Scene>
        <Scene key="map" component={Map} title="Map" hideNavBar />

@luco ๊ฐ€ ์ œ์•ˆํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ฃจํŠธ ์žฅ๋ฉด์„ modal ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ ํ”„๋กœ์ ํŠธ๋ฅผ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค. ๋กœ๊ทธ์ธ์€ ๋ชจ๋‹ฌ๋กœ ๊ตฌํ˜„๋ฉ๋‹ˆ๋‹ค.

์ด๊ฒƒ์€ ์—ฌ์ „ํžˆ โ€‹โ€‹์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

<Router>
    <Scene key="modal" modal>
      <Scene key="root" hideNavBar hideTabBar>
        <Scene key="tabBar" tabs>
          <Scene key="listings" component={Listings} title="Listings" initial right={MapButton} />
          <Scene key="favorites" component={Favorites} title="Favorites" />
          <Scene key="messages" component={Messages} title="Messages" />
          <Scene key="profile" component={Profile} title="Profile" />
        </Scene>
        <Scene key="map" component={Map} title="Map" hideNavBar />
        <Scene key="listingDetail" component={ListingDetail} title="Listing Detail" clone />
      </Scene>
    </Scene>
  </Router>

๋‚˜๋จธ์ง€(ListingDetail ๊ฐ™์€)๊ฐ€ ๋ชจ๋‹ฌ์ด ๋˜๋Š” ๊ฒƒ์„ ์›ํ•˜์ง€ ์•Š์„ ๋•Œ ๋ชจ๋‹ฌ ์žฅ๋ฉด์˜ ๋ชจ๋“  ๊ฒƒ์„ ๋ž˜ํ•‘ํ•˜๋ฉด ํ•˜๋‚˜์˜ ํŠน์ • ์žฅ๋ฉด ์ด ๋ชจ๋‹ฌ์ด ๋œ๋‹ค๋Š” ๊ฒƒ์€ ์˜๋ฏธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. Actions.map ์— ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ํŠน์ • ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๊นŒ?

๋ถˆํ–‰ํžˆ๋„ ๋ฐ˜์‘ ํƒ์ƒ‰์˜ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์Šคํƒ ์ž์‹์— ๋Œ€ํ•ด ๋‹ค๋ฅธ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‚˜๋Š” ๋‹น์‹ ์˜ ๋ฌธ์ œ๋ฅผ ์žฌํ˜„ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค(์ฝ”๋“œ๋Š” ๊ดœ์ฐฎ์•„ ๋ณด์ž…๋‹ˆ๋‹ค). ๊ทธ๋ž˜์„œ ๋ชจ๋“  ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋˜๊ธฐ๋ฅผ ์›ํ•˜๋Š” ๊ฒฝ์šฐ ์ด ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ์˜ˆ์ œ๋ฅผ ๋ถ„๊ธฐํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋„๋ก ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ์˜ ๋กœ๊ทธ์ธ ๋ชจ๋‹ฌ์€ ๋ณด์‹œ๋‹ค์‹œํ”ผ ์ž˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

๋ชจ๋‹ฌ iOS ์ „ํ™˜์„ ์ง€์›ํ•  ๋ฐฉ๋ฒ•์ด ์—†๋‹ค๋Š” ๋ง์”€์ด์‹ ๊ฐ€์š”? ์ฆ‰, ์•„๋ž˜์—์„œ ๋ฏธ๋„๋Ÿฌ์ง€๋Š” ํ•˜๋‚˜?

๋ฌด์Šจ ๋œป์ด์—์š”? ๋‚˜๋Š” Example์— ๋ชจ๋‹ฌ ๋กœ๊ทธ์ธ ํ™”๋ฉด์ด ์žˆ๋‹ค๊ณ  ๋‘ ๋ฒˆ ๋งํ–ˆ์Šต๋‹ˆ๋‹ค(ํ•˜๋‹จ์—์„œ ์Šฌ๋ผ์ด๋“œ)

์ข‹์•„, ํ•˜์ง€๋งŒ ๋‹น์‹ ์€ ๋˜ํ•œ ๋‚ด ์ฝ”๋“œ๊ฐ€ ๊ดœ์ฐฎ์•„ ๋ณด์ธ๋‹ค๊ณ  ๋งํ–ˆ๋‹ค....

๋‚˜๋Š” ์˜ˆ์ œ๋ฅผ ๋”ฐ๋ฅด๊ธฐ ์œ„ํ•ด ์ตœ์„ ์„ ๋‹คํ–ˆ์ง€๋งŒ ๋ฌธ์„œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ๊ฒƒ์ด ๊ฑฐ๊ธฐ์— ์žˆ๋Š” ์ด์œ ๋ฅผ ๋งํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ถ„๋ช…ํžˆ ๋‚ด ๊ฒฝ๋กœ๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ ๋ถ™์—ฌ ๋„ฃ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๊ท€ํ•˜์˜ ์ฝ”๋“œ์— ๋Œ€ํ•ด ๋ง์”€๋“œ๋ฆด ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒด ํ”„๋กœ์ ํŠธ์™€ ๋ชจ๋“  ์„ธ๋ถ€์‚ฌํ•ญ์„ ๋ณผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Actions.map ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ปดํŒŒ์ผํ•˜๋Š” ๋™์•ˆ ์ƒ์„ฑ๋˜์ง€ ์•Š๊ณ  null์ด ๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•„์š”ํ•œ ๊ฒƒ์ด ๋ฌด์—‡์ธ์ง€ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๋” ๋งŽ์€ ์ฝ”๋“œ ์ƒ˜ํ”Œ์„ ์ œ๊ณตํ•ด ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. Actions.map ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์ชฝ์—์„œ ์˜ˆ์ƒํ•˜๋Š” ๊ฒƒ์˜ ์•ˆ์ชฝ์—์„œ ๊ทธ๋ƒฅ ์ „ํ™˜๋ฉ๋‹ˆ๋‹ค.

๋™์ผํ•œ ์•ฑ์—์„œ push ๋ฐ modal ์ „ํ™˜์„ ๋ชจ๋‘ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค๋Š” ๋ง์”€์ด์‹ ๊ฐ€์š”?

์–ด๋จธ๋‚˜. ์•ฑ์ด ์•„๋‹ˆ๋ผ ํ•˜๋‚˜์˜ ์ปจํ…Œ์ด๋„ˆ์— ์žˆ์Šต๋‹ˆ๋‹ค. ๋” ์ž์„ธํžˆ ์‚ดํŽด๋ณด๋ฉด ์ฝ”๋“œ์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 'map'์€ ์—ฌ์ „ํžˆ โ€‹โ€‹๋ชจ๋‹ฌ(key='modal')์ด ์•„๋‹Œ '์ผ๋ฐ˜' ์ปจํ…Œ์ด๋„ˆ(key=root)์˜ ์ž์‹์ž…๋‹ˆ๋‹ค. ๋‹น์‹ ์ดํ•˜๋Š” ์ผ์„ ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์–ด์ฉŒ๋ฉด ์ด ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ๋‹น์‹ ์—๊ฒŒ ๋„ˆ๋ฌด ๋ณต์žกํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค...

์ด ์‹œ๋„:

<Router>
    <Scene key="modal" modal>
      <Scene key="root" hideNavBar hideTabBar>
        <Scene key="tabBar" tabs>
          <Scene key="listings" component={Listings} title="Listings" initial right={MapButton} />
          <Scene key="favorites" component={Favorites} title="Favorites" />
          <Scene key="messages" component={Messages} title="Messages" />
          <Scene key="profile" component={Profile} title="Profile" />
        </Scene>
      </Scene>
        <Scene key="map" component={Map} title="Map" hideNavBar />
        <Scene key="listingDetail" component={ListingDetail} title="Listing Detail" clone />
    </Scene>
  </Router>

๋•๋ถ„์— ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ด€๋ จ ๋ฌธ์„œ๊ฐ€ ์—†๋Š”๋ฐ ๋‚ด๊ฐ€ ์–ด๋–ป๊ฒŒ ์ดํ•ดํ•˜๊ธฐ๋ฅผ ๊ธฐ๋Œ€ํ•ฉ๋‹ˆ๊นŒ? ์ด ๋ชจ๋‹ฌ ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ๋ฌธ์ œ๋ฅผ ์ œ๊ธฐํ•ด ์™”์œผ๋ฉฐ ์•„๋งˆ๋„ ๋ฌธ์„œ์˜ ํ•„์š”์„ฑ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์— ์žˆ๋Š” ์ตœ๊ทผ ๊ฒŒ์‹œ๋ฌผ์€ ๋ฌธ์„œ๊ฐ€ ์•„๋‹ˆ๋ผ JSX ๊ตฌ๋ฌธ ๋ฐ ํƒ์ƒ‰ ์ปจํ…Œ์ด๋„ˆ์— ๋Œ€ํ•œ ์ดํ•ด์— ๊ด€ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
๋ชจ๋‹ฌ์— ๋Œ€ํ•œ ์ถ”๊ฐ€ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

์•ˆ๋…•,

๋‚˜๋ฅผ ์œ„ํ•ด ์ผํ–ˆ๋‹ค

<Router navigationBarStyle={{backgroundColor: '#FF5722'}} titleStyle={{color: '#fff'}}>
        <Modal>
            <Stack key="root">
                <Scene key='login_screen' component={LoginScreen} title='login' hideNavBar={true} />
            </Stack>
            <Scene key='cadastro_screen' component={CadastroScreen} title='Cadastro' hideNavBar={false} />
        </Modal>
</Router>

๋ชจ๋‹ฌ ๋ž˜ํผ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ณด๊ธฐ ์ƒ๋‹จ์— 2๊ฐœ์˜ ํƒ์ƒ‰ ๋ชจ์Œ์ด ํ‘œ์‹œ๋˜๊ณ , hideNavBar๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋‘˜ ๋‹ค ์‚ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ? ๋ถ„๋ช…ํžˆ ํ•˜๋‚˜์˜ ํƒญ ๋ง‰๋Œ€๊ฐ€ ์•„๋‹Œ ๋‘ ๊ฐœ์˜ ํƒญ ๋ง‰๋Œ€๋ฅผ ์›ํ•ฉ๋‹ˆ๋‹ค... ?

<Modal hideNavBar> ์‚ฌ์šฉํ•˜๋ฉด $# <Modal> ์ž์ฒด ํƒ์ƒ‰ ๋ชจ์Œ์ด ์ถ”๊ฐ€๋˜๋ฏ€๋กœ ๋ฃจํŠธ ์žฅ๋ฉด์˜ ํ•˜๋‚˜์™€ ํ•จ๊ป˜ ๋‘ ๊ฐœ๋ฅผ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰