首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React Native 3按钮和3模式

React Native 3按钮和3模式
EN

Stack Overflow用户
提问于 2018-12-13 14:50:06
回答 1查看 395关注 0票数 1

例如,有3个按钮和3个模型,如何使模型相互特定?

这3个按钮是如何根据点击的按钮调出模型的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-13 15:24:58

看看这个。已将我的代码编辑为完整版

代码语言:javascript
复制
import React, {Component} from 'react';
import {StyleSheet, Text, View, SafeAreaView, Modal, TouchableOpacity} from 'react-native';

class App extends React.Component{
  constructor(props){
    super(props);

    this.state = {
      modal1: false,
      modal2: false,
      modal3: false,
    }
  }

  _toggleModal = () => {
    this.setState({
      modal1:false,
      modal2: false,
      modal3: false,
    })
  }

  render(){
    return(
      <SafeAreaView>
        {/*Modal 1*/}
        <Modal
          transparent={true}
          animationType={'none'}
          visible={this.state.modal1}
          onRequestClose={() => {console.log('close modal')}}>
          <View>
           <Text>Modal1</Text>
          </View>
          <TouchableOpacity onPress={() => this.setState({modal1:false})}>
           <Text>Hide Me!</Text>
          </TouchableOpacity>
        </Modal>
        {/*Modal 2*/}
        <Modal
          transparent={true}
          animationType={'none'}
          visible={this.state.modal2}
          onRequestClose={() => {console.log('close modal')}}>
          <View>
           <Text>Modal2</Text>
          </View>
          <TouchableOpacity onPress={() => this.setState({modal2:false})}>
           <Text>Hide Me!</Text>
          </TouchableOpacity>
        </Modal>
        {/*Modal 3*/}
        <Modal
          transparent={true}
          animationType={'none'}
          visible={this.state.modal3}
          onRequestClose={() => {console.log('close modal')}}>
          <View>
           <Text>Modal3</Text>
          </View>
          <TouchableOpacity onPress={() => this.setState({modal3:false})}>
           <Text>Hide Me!</Text>
          </TouchableOpacity>
        </Modal>

        {/*Front Page*/}
        <TouchableOpacity onPress={ () => this.setState({modal1:true})}>
          <Text>Click for Modal 1</Text>
        </TouchableOpacity>
        <TouchableOpacity onPress={ () => this.setState({modal2:true})}>
          <Text>Click for Modal 2</Text>
        </TouchableOpacity>
        <TouchableOpacity onPress={ () => this.setState({modal3:true})}>
          <Text>Click for Modal 3</Text>
        </TouchableOpacity>

      </SafeAreaView>
    )
  }

}

export default App
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53756416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档