Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Tìm hiểu Modal Window trong React Native


Modal là component hiển thị nội dung đè lên giao diện chính (overlay). Dùng cho dialog, bottom sheet, form nhập liệu, xác nhận hành động quan trọng.

Import

import { Modal, View, Text, Button, StyleSheet } from 'react-native';

Cú pháp cơ bản

<Modal
  animationType="slide"      // 'slide' | 'fade' | 'none'
  transparent={true}         // true: nền trong suốt, false: nền trắng
  visible={isVisible}        // boolean: hiển thị/ẩn
  onRequestClose={() => {}}  // Android: bắt buộc khi transparent=true
  onShow={() => {}}          // Callback khi hiện
  onDismiss={() => {}}       // Callback khi tắt
  presentationStyle="pageSheet" // iOS: 'fullScreen' | 'pageSheet' | 'formSheet' | 'overFullScreen'
>
  Nội dung Modal
</Modal>

Các props quan trọng

PropKiểuMô tả
visiblebooleanKiểm soát hiển thị/ẩn. Bắt buộc.
animationTypeenumslide (từ dưới), fade (mờ dần), none.
transparentbooleantrue: cho phép nền trong suốt, tùy biến UI hoàn toàn.
presentationStyleenum (iOS)Kiểu hiển thị: fullScreen, pageSheet (mặc định), formSheet (nhỏ giữa), overFullScreen.
onRequestClosefunc**Bắt buộc** trên Android khi transparent={true}. Gọi khi bấm back button.
onShow / onDismissfuncCallback khi modal hiện/biến mất (sau animation).
supportedOrientationsarrayiOS: giới hạn xoay màn hình khi modal mở.

Ví dụ cơ bản: Modal slide từ dưới

import React, { useState } from 'react';
import { Modal, Button, View, Text, StyleSheet } from 'react-native';

const App = () => {
  const [isVisible, setModalVisible] = useState(false);

  return (
    
       setModalVisible(false)}
      >
        
          
            Modal Window Testing!
            

Ví dụ nâng cao: Bottom Sheet (trượt từ dưới lên)

import React, { useState, useRef } from 'react';
import { Modal, View, Text, StyleSheet, TouchableOpacity, Animated } from 'react-native';

const BottomSheet = ({ visible, onClose, children }) => {
  const translateY = useRef(new Animated.Value(0)).current;

  React.useEffect(() => {
    Animated.timing(translateY, {
      toValue: visible ? 0 : 1,
      duration: 300,
      useNativeDriver: true,
    }).start();
  }, [visible, translateY]);

  const animatedStyle = {
    transform: [{ translateY: translateY.interpolate({
      inputRange: [0, 1],
      outputRange: ['0%', '100%'],
    })}],
  };

  if (!visible) return null;

  return (
    
      
      
        {children}
      
    
  );
};

const styles = StyleSheet.create({
  overlay: { flex: 1, backgroundColor: 'rgba(0,0,0,0.5)' },
  sheet: {
    position: 'absolute',
    bottom: 0,
    left: 0,
    right: 0,
    backgroundColor: 'white',
    borderTopLeftRadius: 20,
    borderTopRightRadius: 20,
    padding: 20,
    maxHeight: '80%',
  },
});

// Sử dụng
const App = () => {
  const [show, setShow] = useState(false);
  return (
    
      

Best Practices

  • Luôn dùng useState điều khiển visible.
  • Trên Android, onRequestClose **bắt buộc** khi transparent={true}.
  • Dùng KeyboardAvoidingView bên trong Modal nếu có input text.
  • Cho UI phức tạp, cân nhắc react-native-modal, react-navigation (modal screen), hoặc @gorhom/bottom-sheet.