javascript - React Native : Modal closing functionality - Stack Overflow

I am currently following the modal example given here..htmlThe code works and it is indeed showing a mo

I am currently following the modal example given here.

.html

The code works and it is indeed showing a modal box.

But there is no "close" functionality except the one via TouchableHighlight onPress event.

Is it possible to have a "close modal" functionality via "X" on the corner ?

I checked out the props and I am not able to find any.

So does this mean that using only the TouchableHighlight's onPress event alone you will be able to control the modal's closing ?

App.js code

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

export default class ModalExample extends Component {

  state = {
    modalVisible: false,
  }

  setModalVisible(visible) {
    this.setState({modalVisible: visible});
  }

  render() {
    return (
      <View style={{marginTop: 22}}>
        <Modal
          animationType="slide"
          transparent={false}
          visible={this.state.modalVisible}
          onRequestClose={() => {alert("Modal has been closed.")}}
          >
         <View style={{marginTop: 22}}>
          <View>
            <Text>Hello World!</Text>

            <TouchableHighlight onPress={() => {
              this.setModalVisible(!this.state.modalVisible)
            }}>
              <Text>Hide Modal</Text>
            </TouchableHighlight>

          </View>
         </View>
        </Modal>

        <TouchableHighlight onPress={() => {
          this.setModalVisible(true)
        }}>
          <Text>Show Modal</Text>
        </TouchableHighlight>

      </View>
    );
  }
}

I am currently following the modal example given here.

https://facebook.github.io/react-native/docs/modal.html

The code works and it is indeed showing a modal box.

But there is no "close" functionality except the one via TouchableHighlight onPress event.

Is it possible to have a "close modal" functionality via "X" on the corner ?

I checked out the props and I am not able to find any.

So does this mean that using only the TouchableHighlight's onPress event alone you will be able to control the modal's closing ?

App.js code

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

export default class ModalExample extends Component {

  state = {
    modalVisible: false,
  }

  setModalVisible(visible) {
    this.setState({modalVisible: visible});
  }

  render() {
    return (
      <View style={{marginTop: 22}}>
        <Modal
          animationType="slide"
          transparent={false}
          visible={this.state.modalVisible}
          onRequestClose={() => {alert("Modal has been closed.")}}
          >
         <View style={{marginTop: 22}}>
          <View>
            <Text>Hello World!</Text>

            <TouchableHighlight onPress={() => {
              this.setModalVisible(!this.state.modalVisible)
            }}>
              <Text>Hide Modal</Text>
            </TouchableHighlight>

          </View>
         </View>
        </Modal>

        <TouchableHighlight onPress={() => {
          this.setModalVisible(true)
        }}>
          <Text>Show Modal</Text>
        </TouchableHighlight>

      </View>
    );
  }
}
Share edited Nov 11, 2023 at 22:23 benomatis 5,6537 gold badges39 silver badges60 bronze badges asked Nov 27, 2017 at 4:41 VPYVPY 4631 gold badge9 silver badges24 bronze badges 1
  • I've recently used (in production) and found this library to be super useful for modal. It extends on top of the native Modal class, giving you additional functionality such as swipe-gestures. Do check it out. – Kunal Chawla Commented Nov 27, 2017 at 5:01
Add a ment  | 

1 Answer 1

Reset to default 4
import React, { Component } from 'react';
import { Modal, Text, TouchableHighlight, View } from 'react-native';

export default class ModalExample extends Component {

  state = {
    modalVisible: false,
  }

  setModalVisible(visible) {
    this.setState({modalVisible: visible});
  }

  closeModal = () => {
      this.setState({modalVisible: false})
  }

  render() {
    return (
      <View style={{marginTop: 22}}>
        <Modal
          animationType="slide"
          transparent={false}
          visible={this.state.modalVisible}
          onRequestClose={() => {alert("Modal has been closed.")}}
          >
         <View style={{marginTop: 22}}>

          <View>
            <Text>Hello World!</Text>

            <TouchableHighlight onPress={() => {
              this.setModalVisible(!this.state.modalVisible)
            }}>
              <Text>Hide Modal</Text>
            </TouchableHighlight>
          </View>
         <CloseButton onPress={this.closeModal} /> // Create your 'X' button with your preferred styling
         </View>
        </Modal>

        <TouchableHighlight onPress={() => {
          this.setModalVisible(true)
        }}>
          <Text>Show Modal</Text>
        </TouchableHighlight>

      </View>
    );
  }
}

You just need to add a button to the modal with your preferred styling and on the button's onPress listener set the state variable modalVisible to false for hiding the modal.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744882640a4598929.html

相关推荐

  • javascript - React Native : Modal closing functionality - Stack Overflow

    I am currently following the modal example given here..htmlThe code works and it is indeed showing a mo

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信