React Native에서 모달 창이 작동하는 방식 설명

PHPz
풀어 주다: 2023-08-24 15:01:01
앞으로
796명이 탐색했습니다.

모달 구성 요소는 UI 콘텐츠 위에 콘텐츠 보기를 표시하는 데 도움이 됩니다.

기본 모달 컴포넌트는 다음과 같습니다. -

<Modal animationType="slide" transparent={true} visible={modalVisible} onRequestClose={() => { Alert.alert("Modal has been closed."); }}> Your Content Here</Modal>
로그인 후 복사

모달 컴포넌트를 사용하려면 아래와 같이 먼저 Import를 해야 합니다. -

import { Modal } from "react-native";
로그인 후 복사
로그인 후 복사

모달 창의 기본 속성은 다음과 같습니다. -

Sr. 아니요 Props & Description
1< strong>animationType

이 속성은 표시된 애니메이션을 처리합니다. 모달 창. 세 개의 값을 갖는 열거형입니다. 슬라이드, 페이드 및 없음.

2onDismiss

이 속성은 호출될 함수를 허용합니다. 모달 창이 닫힐 때.

3onOrientationChange

기기 시작 시 호출되는 콜백 함수 모달 창의 방향이 바뀔 때 보여주다.

4onShow

함수가 호출되는 prop 값으로 전달됩니다. 모달 창을 표시할 때.

5presentationStyle

이 속성은 모달 상자의 표시를 처리합니다. 창문. 사용 가능한 값은 전체 화면이며, pageSheet, formSheet 및 overFullScreen.

6Transparent

이 소품은 투명한 보기 또는 채우기를 제공할지 결정합니다. 모달 창의 전체 모습입니다.

7visibile

< p>이 속성은 모달 창이 다음과 같은지 여부를 결정합니다. 보이거나 보이지 않습니다.

예 1: 모달 창 표시 표시

모달 구성 요소를 사용하려면 아래와 같이 먼저 가져와야 합니다. -

import { Modal } from "react-native";
로그인 후 복사
로그인 후 복사

모달 창을 표시하려면 애니메이션에 표시할 내용을 결정할 수 있습니다. 옵션은 슬라이드, 페이드 및 없음입니다. 다음 예에서는 아래와 같이 텍스트와 버튼이 있는 간단한 모달 창을 표시하려고 합니다.

<Modal
      animationType="slide"
      transparent={true}
      visible={isVisible}
   >
   <View style={styles.centeredView}>
      <View style={styles.myModal}>
         <Text style={styles.modalText}>Modal Window Testing!</Text>
            <Button style={styles.modalButton} title="Close" onPress={() => {setModalVisiblility(false); }}/>
      </View>
   </View>
</Modal>
로그인 후 복사

isVisible 변수는 visible 속성에 할당됩니다. 기본값은 false입니다. 즉, 기본적으로 모달 창이 표시되지 않습니다. isVisible 속성의 초기화는 다음과 같습니다.

const [isVisible, setModalVisiblility] = useState(false);
로그인 후 복사

setModalVisiblility는 isVisible 변수를 false에서 true로 또는 그 반대로 업데이트합니다.

구성 요소에 정의된 닫기 버튼은 setModalVisiblility(false)를 호출하여 isVisible을 false로 만들고 모달 창이 사라집니다.

모달 창을 표시하려면 아래와 같이 setModalVisiblility(true)를 호출하는 구성 요소 외부에 버튼이 있어야 합니다. -

<View style={styles.centeredView}>
   <Modal
      animationType="slide"
      transparent={true}
      visible={isVisible}
   >
   <View style={styles.centeredView}>
      <View style={styles.myModal}>
         <Text style={styles.modalText}>Modal Window Testing!</Text>
            <Button style={styles.modalButton} title="Close" onPress={() =>{setModalVisiblility(false); }}/>
            </View>
         </View>
      </Modal>
      <Button title="Click Me" onPress={() => {
         setModalVisiblility(true);
      }}
   />
</View>
로그인 후 복사

이것은 모달 창을 표시하거나 숨기는 작업 코드입니다.

import React, { useState } from "react";
import { Button, Alert, Modal, StyleSheet, Text, View } from "react-native";
const App = () => {
   const [isVisible, setModalVisiblility] = useState(false);
   return (
      <View style={styles.centeredView}>
         <Modal
            animationType="slide"
            transparent={true}
            visible={isVisible}
         >
         <View style={styles.centeredView}>
            <View style={styles.myModal}>
               <Text style={styles.modalText}>Modal Window Testing!</Text>
                  <Button style={styles.modalButton} title="Close" onPress={() =>{setModalVisiblility(false); }}/>
                  </View>
               </View>
            </Modal>
            <Button title="Click Me" onPress={() => {
               setModalVisiblility(true);
            }}
         />
      </View>
   );
};
const styles = StyleSheet.create({
   centeredView: {
      flex: 1,
      justifyContent: "center",
      alignItems: "center",
      marginTop: 22
   },
   myModal: {
      width:200,
      height:200,
      margin: 20,
      backgroundColor: "white",
      borderRadius: 20,
      padding: 35,
      alignItems: "center",
      shadowColor: "#000",
      shadowOffset: {
         width: 0,
         height: 2
      },
      shadowOpacity: 0.30,
      shadowRadius: 4,
      elevation: 5
   },
   modalText: {
      marginBottom: 20,
      textAlign: "center"
   },
   modalButton: {
      marginBottom: 50,
   }
});
export default App;
로그인 후 복사

출력

解释 React Native 中模态窗口的工作原理

위 내용은 React Native에서 모달 창이 작동하는 방식 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:tutorialspoint.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!