[TIL] React Native-Basic

link717ยท2020๋…„ 11์›” 22์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
20/53
post-thumbnail

๐Ÿ˜Ž Component

*React Native๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด IOS, Android ๊ฐœ๋ฐœ์„ ๋ณ„๋„๋กœ ํ•˜์ง€ ์•Š๊ณ  App ๋ฐฐํฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ทธ๊ฒŒ ๊ฐ€๋Šฅํ•œ ์ด์œ ๋Š” IOS์™€ Android๋Š” tag๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š”๋ฐ RN์—์„œ๋Š” ๊ทธ๊ฒƒ์„ ํฌ๊ด„ํ•˜๋Š” Component๋ฅผ ์ œ๊ณตํ•˜๊ณ  App์ด ๋ฐฐํฌ๋  ๋•Œ ๊ฐ๊ฐ์˜ ์–ธ์–ด๋กœ conversion ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

+ TouchableOpacity: button tag๊ฐ€ ์žˆ์ง€๋งŒ custom์˜ ์–ด๋ ค์›€ ๋•Œ๋ฌธ์— TouchableOpacity๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ˜‡ Styling

<Default ์„ค์ •>

import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';

const styles = StyleSheet.create({
  bigBlue: {
    color: 'blue',
    fontWeight: 'bold',
    fontSize: 30,
  },
  red: {
    color: 'red',
  },
});

1) display: flex; + flex-direction: column;
2) flex: 1; (width,height: 100%, 100vw, 100vh ๋“ฑ)

๐Ÿ™‚ React Library

1) react-navigation/native: react-router-dom์˜ ๊ธฐ๋Šฅ์„ ํ•œ๋‹ค.

npm install @react-navigation/native

expo install react-native-gesture-handler react-native-reanimated react-native-screens react-native-safe-area-context @react-native-community/masked-view
์‚ฌ์šฉ๋ฒ•: ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ js file์— import ํ•˜๊ณ  NavigationContainer component๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

import 'react-native-gesture-handler';
import { NavigationContainer } from '@react-navigation/native';

 <NavigationContainer>
 </NavigationContainer>

2) react-navigation/stack: ์œ ์ €๊ฐ€ ์ด๋™ํ•œ ํŽ˜์ด์ง€์˜ ์œ„์น˜๋ฅผ ์ž๋™์œผ๋กœ ๊ธฐ์–ตํ•˜๊ณ  prevPage๋กœ ์ด๋™์„ ๋„์™€์ค€๋‹ค.

npm install @react-navigation/stack
์‚ฌ์šฉ๋ฒ•: ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ js file์— import ํ•˜๊ณ  Stack.Navigator๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

import { createStackNavigator } from '@react-navigation/stack';
const Stack = createStackNavigator();

<Stack.Navigator>

3) navigation.###

  • navigation.navigate: ์ด๋™ํ•˜๋ ค๋Š” page์˜ ๊ฒฝ๋กœ๊ฐ€ ๋ช…ํ™•ํ•œ ๊ฒฝ์šฐ, ์Œ“์ธ route๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ํ•ด๋‹น ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • navigation.popToTop: ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ ํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • navigation.push: ๋™์ผ page ๊ฒฝ๋กœ์—์„œ ๋‹ค๋ฅธ ์ •๋ณด๋“ค์„ ํ‘œํ˜„ํ•  ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค.(push ํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด route๊ฐ€ ์Œ“์ธ๋‹ค === stack!)
  • navigation.goBack: ์ด์ „ route stack์œผ๋กœ ์ด๋™ํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ‘’ emulator reload ๋ฐฉ๋ฒ•

. window : ctrl + M
. mac: command + D

profile
Turtle Never stop

0๊ฐœ์˜ ๋Œ“๊ธ€