- React Native 教程
- React Native - 主页
- 核心概念
- React Native - 概述
- React Native - 环境设置
- React Native - 应用程序
- React Native - 状态
- React Native - 道具
- React Native - 样式
- React Native - Flexbox
- React Native - 列表视图
- React Native - 文本输入
- React Native - 滚动视图
- React Native - 图像
- 反应本机 - HTTP
- React Native - 按钮
- React Native - 动画
- React Native - 调试
- React Native - 路由器
- React Native - 运行 IOS
- React Native - 运行 Android
- 组件和 API
- React Native - 查看
- React Native - WebView
- React Native - 模态
- React Native - 活动指示器
- React Native - 选择器
- React Native - 状态栏
- React Native - 开关
- React Native - 文本
- React Native - 警报
- React Native - 地理定位
- React Native - 异步存储
- React Native 有用资源
- React Native - 快速指南
- React Native - 有用的资源
- React Native - 讨论
React Native - 模态
在本章中,我们将向您展示如何在 React Native 中使用模态组件。
现在让我们创建一个新文件:ModalExample.js
我们将把逻辑放在ModalExample中。我们可以通过运行toggleModal来更新初始状态。
通过运行toggleModal更新初始状态后,我们将把visible属性设置为我们的模态。当状态改变时,这个 prop 将会更新。
Android设备需要onRequestClose 。
应用程序.js
import React, { Component } from 'react' import WebViewExample from './modal_example.js' const Home = () => { return ( <WebViewExample/> ) } export default Home;
modal_example.js
import React, { Component } from 'react'; import { Modal, Text, TouchableHighlight, View, StyleSheet} from 'react-native' class ModalExample extends Component { state = { modalVisible: false, } toggleModal(visible) { this.setState({ modalVisible: visible }); } render() { return ( <View style = {styles.container}> <Modal animationType = {"slide"} transparent = {false} visible = {this.state.modalVisible} onRequestClose = {() => { console.log("Modal has been closed.") } }> <View style = {styles.modal}> <Text style = {styles.text}>Modal is open!</Text> <TouchableHighlight onPress = {() => { this.toggleModal(!this.state.modalVisible)}}> <Text style = {styles.text}>Close Modal</Text> </TouchableHighlight> </View> </Modal> <TouchableHighlight onPress = {() => {this.toggleModal(true)}}> <Text style = {styles.text}>Open Modal</Text> </TouchableHighlight> </View> ) } } export default ModalExample const styles = StyleSheet.create ({ container: { alignItems: 'center', backgroundColor: '#ede3f2', padding: 100 }, modal: { flex: 1, alignItems: 'center', backgroundColor: '#f7021a', padding: 100 }, text: { color: '#3f2949', marginTop: 10 } })
我们的起始屏幕将如下所示 -
如果我们单击该按钮,模式将打开。