일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- react-hook-form
- React
- js
- 코드숨
- 자바스크립트
- 리액트네이티브
- Flipper
- 프로그래머스
- sort( )
- 유니온타입
- Typescript
- ios
- 배열
- react-native
- 제네릭타입
- react-native-camera-roll
- react-native-vision-camera
- 자료구조와알고리즘
- slice
- 리액트
- 크라코
- react-native-image-picker
- 파스에러
- craco
- Android
- 모던자바스크립트
- javascript
- 타입스크립트
- 리액트쿼리
- reactnative
Archives
- Today
- Total
KassyLog
자주 사용하는 react-native 태그 알아보기 -Modal 본문
리액트에서는 모달을 만들기위해서 따로 컴포넌트를 만들거나 css의 (나름) 공수를 들였어야 했는데 리액트네이티브에서는 자체적으로 제공하는 모달이 있어서 아주 편리하게 모달을 만들 수 있다.
개인적으로 react-native를 활용하면서 가장 편하다고 생각하는 부분이다.
Modal · React Native
The Modal component is a basic way to present content above an enclosing view.
reactnative.dev
import React, { useState } from "react";
import { Alert, Modal, StyleSheet, Text, Pressable, View } from "react-native";
const App = () => {
const [modalVisible, setModalVisible] = useState(false);
return (
<View style={styles.centeredView}>
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={() => {
Alert.alert("Modal has been closed.");
setModalVisible(!modalVisible);
}}
>
<View style={styles.centeredView}>
<View style={styles.modalView}>
<Text style={styles.modalText}>Hello World!</Text>
<Pressable
style={[styles.button, styles.buttonClose]}
onPress={() => setModalVisible(!modalVisible)}
>
<Text style={styles.textStyle}>Hide Modal</Text>
</Pressable>
</View>
</View>
</Modal>
<Pressable
style={[styles.button, styles.buttonOpen]}
onPress={() => setModalVisible(true)}
>
<Text style={styles.textStyle}>Show Modal</Text>
</Pressable>
</View>
);
};
const styles = StyleSheet.create({
centeredView: {
flex: 1,
justifyContent: "center",
alignItems: "center",
marginTop: 22
},
modalView: {
margin: 20,
backgroundColor: "white",
borderRadius: 20,
padding: 35,
alignItems: "center",
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 2
},
shadowOpacity: 0.25,
shadowRadius: 4,
elevation: 5
},
button: {
borderRadius: 20,
padding: 10,
elevation: 2
},
buttonOpen: {
backgroundColor: "#F194FF",
},
buttonClose: {
backgroundColor: "#2196F3",
},
textStyle: {
color: "white",
fontWeight: "bold",
textAlign: "center"
},
modalText: {
marginBottom: 15,
textAlign: "center"
}
});
export default App;
위 코드의 예시를 살펴보면
centeredView는 모달의 뒷배경이 되는 가장 상위 컴포넌트이며
그 아래의 Modal 태그를 활용하여 위 그림과 같은 모달 모양을 만들 수 있다.
애니메이션 타입은 slide, fade, none 총 3가지로 구성되어있다.
'react-native' 카테고리의 다른 글
자주 사용하는 react-native 태그 알아보기 -Alert (0) | 2023.01.05 |
---|---|
react-native 로 카메라 기능 (0) | 2023.01.04 |
자주 사용하는 react-native 태그 알아보기 -Button (0) | 2023.01.03 |
React와 React native의 CSS 차이점 (0) | 2022.12.15 |
자주 사용하는 react-native 태그 알아보기 -TextInput(iOS) (0) | 2022.12.10 |