【问题标题】:How to show a modal from anoter js file to current file in react-native?如何在 react-native 中显示从另一个 js 文件到当前文件的模式?
【发布时间】:2018-10-18 09:56:34
【问题描述】:

我想显示从另一个 js 类到当前类文件的模态。我可以在当前 js 文件中将模态可见性设置为真,因此我的模态代码位于另一个类中。我想在实际注册类中使用此代码.这是我的代码:-

CountryCodePicker.js

  import React, { Component } from 'react';
  import { StyleSheet,  Text,  View} from 'react-native'
  import { showMessage } from '../utils/GeneralFunctions';
  import { countryPicker } from './CountryPickerModal';

  export default class CountryCodePicker extends Component {    
      constructor(props) {
      super(props)

      }

    render() {
      return (
          <View style={styles.container}>
              <Text
                  onPress={() => {
                    <countryPicker modalVisible={true}/>
                  }} 
                  style={styles.welcome}>Open Modal</Text>
          </View>
      );
    }
  }


  const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center'
      },
      welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
        padding:20
      }
    })

CountryPickerModal:这是我想在国家选择器类中显示的模式代码。

  import React from 'react';
  import {Modal,View,Text} from 'react-native';
  import { showMessage } from '../utils/GeneralFunctions';

  export const countryPicker = (props) => {
      const {modalVisible = false} = props;

      state = {
          modalVisible:modalVisible
      }

      showModal = () => {
          // if(modalVisible == false){
          //     modalVisible = true;
          // }else{
          //     modalVisible=false;
          // }

          this.setState({modalVisible:true});
      }

      return(
          <Modal
              visible={modalVisible}
              animationType='slide'
              transparent={false}
              onRequestClose={() => {showMessage('Closed')}}
          >
          <View>
              <Text 
                  style={{fontSize:24,fontWeight:'900',padding:16}}
                  onPress={() => {showMessage("Country Picker 
                          Modal")}}
              >
                  Close Modal
              </Text>
          </View>
          </Modal>
      )
  }

【问题讨论】:

    标签: javascript node.js react-native ecmascript-6


    【解决方案1】:

    render 中定义它并传递可见或隐藏标志。

    render() {
      return (
          <View style={styles.container}>
              <Text
                  onPress={() => {
                    this.setState({modalVisible:true})
                  }} 
                  style={styles.welcome}>Open Modal</Text>
              <countryPicker modalVisible={this.state.modalVisible}/>          
          </View>
      );
    }
    

    【讨论】:

      猜你喜欢
      • 2022-11-28
      • 1970-01-01
      • 2021-04-26
      • 2020-07-20
      • 2020-07-17
      • 2015-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多