React Native 在外部单击时关闭模态

bsxbgnwa  于 2022-12-14  发布在  React
关注(0)|答案(2)|浏览(277)

我已经为模态编写了上述可重用组件,但当我在模态外单击时,我想关闭模态,为此我使用了TouchableWithoutFeedback,但onPress我添加了props,我不想使用props。我想在此代码本身中添加逻辑,这样无论在何处使用此组件,我都不必调用该props。请让我知道如何才能实现这一点

<Modal animationType={ANIMATIONS.SlideType} transparent={true} visible={props.visible}>
      <TouchableWithoutFeedback onPress={props.dismissModal}>
        <View style={Styles.opacityContainer}></View>
      </TouchableWithoutFeedback>
      <View style={Styles.container}>
        <View style={Styles.headerContainer}>
          <View style={[Styles.header, Styles.directionRow]}>
            <View>
              <Text style={Styles.headerText}>{bottomSheetStrings.addNew}</Text>
            </View>
            <View style={SheetStyles.iconContainer}>
              <IMIcon
                name={Icon.cancel}
                size={18}
                color={colors.grayish}
                iconsStyle={Styles.closeButton}
              />
            </View>
          </View>
        </View>
        <View style={Styles.viewContainer}>
          <SafeAreaView>
            <FlatList
              horizontal={false}
              data={props.navData}
              renderItem={renderActions}
              keyExtractor={(item) => item.id}
              numColumns={4}
            />
          </SafeAreaView>
        </View>
      </View>
    </Modal>
4nkexdtk

4nkexdtk1#

只需将Modal的内容 Package 到TouchableWithoutFeedback中,然后从那里设置可见性状态。
下面是完整的示例:Expo Snack

import React, { useState } from 'react';
import {
  Alert,
  Modal,
  StyleSheet,
  Text,
  Pressable,
  View,
  TouchableWithoutFeedback,
} 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.');
          this.setModalVisible(false);
        }}>
        <TouchableWithoutFeedback onPressOut={() => setModalVisible(false)}>
          <View style={styles.centeredView}>
            <TouchableWithoutFeedback>
              <View style={styles.modalView}>
                <Text style={styles.modalText}>Hello World!</Text>
                <Pressable
                  style={[styles.button, styles.buttonClose]}
                  onPress={() => setModalVisible(false)}>
                  <Text style={styles.textStyle}>Hide Modal</Text>
                </Pressable>
              </View>
            </TouchableWithoutFeedback>
          </View>
        </TouchableWithoutFeedback>
      </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: 100,
    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;
lg40wkob

lg40wkob2#

尝试使用https://github.com/react-native-modal/react-native-modal npm,它提供了比react原生内置模式更多的可访问性。
在这里我们可以简单地处理onBackdropPress事件来关闭模态。
例如:

<Modal isVisible={isVisible} onBackdropPress={() => setVisible(false)} >
    <View style={{ height: 100, alignItems: "center", justifyContent: "center" }}>
      <Text>I am the modal content!</Text>
    </View>
  </Modal>

相关问题