简体   繁体   English

如何在React Native中单击按钮以打开React Native模式?

[英]How to open a react native modal on button click in react native?

I am new to react native and I am trying to open a modal on button click. 我是本机反应的新手,但我想在单击按钮时打开一个模式。 I am trying to use the following code to open the modal:- 我正在尝试使用以下代码打开模式:

  openHeaderModal = () => {
    <ModalDropdown
      options={["H1", "H2", "H3"]}
      dropdownStyle={{
        borderRadius: 6,
        backgroundColor: "#26344a",
        shadowColor: "rgba(0, 0, 0, 0.2)",
        shadowOffset: {
          width: 0,
          height: 5
        },
        shadowRadius: 20,
        shadowOpacity: 1,
        padding: 8
      }}
      dropdownTextStyle={{
        fontFamily: "poppins-500",
        fontSize: 16,
        fontStyle: "normal",
        letterSpacing: 0,
        textAlign: "left",
        color: "#ffffff",
        backgroundColor: "#26344a"
      }}
    >
    </ModalDropdown>
  }

I am using react-native-modal-dropdown for modal. 我正在使用react-native-modal-dropdown作为模态。 Following is my jsx code for the buton:- 以下是我的按钮的jsx代码:-

  <Button onPress={() => this.openHeaderModal()} vertical>
     <Image
       style={{ marginTop: 20 }}
       source={require("../assets/heading.png")}
     />
  </Button>

Any help and support is appreciated. 任何帮助和支持,表示赞赏。 Thank you. 谢谢。

I think open a Modal in react-native very easy, simple example: 我认为在react-native中打开Modal非常简单,简单的示例:

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

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>
    );
  }
}

Link: https://facebook.github.io/react-native/docs/modal.html#docsNav 链接: https//facebook.github.io/react-native/docs/modal.html#docsNav

If you want to use library: https://github.com/react-native-community/react-native-modal 如果要使用库: https : //github.com/react-native-community/react-native-modal

I followed the example given by @Issac at https://github.com/sohobloo/react-native-modal-dropdown/blob/master/example/index.js and solved the problem. 我按照https://github.com/sohobloo/react-native-modal-dropdown/blob/master/example/index.js上 @Issac给出的示例进行操作,并解决了该问题。 Following is the code to inflate the Modal drop down on a button click:- 以下是在单击按钮时增加Modal下拉菜单的代码:-

 <ModalDropdown
              style={{ marginLeft: 50 }}
              ref={el => this._dropdown_5 = el}
              defaultValue=''
              dropdownStyle={{
                borderRadius: 6,
                backgroundColor: "#26344a",
                shadowColor: "rgba(0, 0, 0, 0.2)",
                shadowOffset: {
                  width: 0,
                  height: 5
                },
                shadowRadius: 20,
                shadowOpacity: 1,
                padding: 8
              }}
              dropdownTextStyle={{
                fontFamily: "poppins-500",
                fontSize: 16,
                fontStyle: "normal",
                letterSpacing: 0,
                textAlign: "left",
                color: "#ffffff",
                backgroundColor: "#26344a"
              }}
              options={['H1', `H2`, 'H3']}
              onDropdownWillShow={this._dropdown_5_willShow.bind(this)}

            />

code for the onPress of the button:- 按钮onPress的代码:-

<Button onPress={this._dropdown_5_show.bind(this)} vertical >
                  <Image style={{ marginTop: 20 }} style={{}} source={require("../assets/heading.png")} />
                </Button>

Please find below link for modal demo 请在下面的链接中找到模态演示
https://reactnativecode.com/react-native-modal-ios-android/ https://reactnativecode.com/react-native-modal-ios-android/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM