[react-native] 05 组件 button + alert

导入 button + alert

import {Text, View, StyleSheet, Alert, Button} from 'react-native'

js 复制代码
import React, {Component} from 'react'  
import {Text, View, ScrollView, StyleSheet, Dimensions, Alert, Button} from 'react-native'  
  
export default class FlexDirection extends Component{  
    render(){  
        return (  
            <View style={[styles.container]}>  

                <Button title='alert'/>  

            </View>  
        )  
    }  
}  
  
const styles = StyleSheet.create({  
    container:{  
        justifyContent:'space-around',  
        alignItems:'center'  
    },  
})

添加alert属性

js 复制代码
<View style={[styles.container]}>  
  
<Button  
    title='alert'  
    onPress={() =>{alert('我是一个按钮')}}  
/>  
  
</View>

在这里 alert 是RN里面的一个函数,并不是组件。

js 复制代码
<Button  
title='Alert.alert'  
onPress={() =>{Alert.alert('我是一个按钮')}}  
color={'red'}  
/>

我们试一下 Alert.alert 的方法。 通过下面的效果图,我们很明显的能看到区别对吧。一个带标头,一个不带标头。

携带函数,内部再创建两个按钮

我们首先需要先定义一个箭头函数

js 复制代码
createTwoAlertButton = () => {  
    Alert.alert(  
        "警告标题⚠️",  
        "警告内容⚠️",  
        [  
            {  
                text:"取消",  
                onPress:() => console.log('Cancel!'),  
                style:'cancel'  
            },  
            {  
                text:"确认",  
                onPress:() => console.log('Confirm!'),  
                style:'default'  
            }  

        ]  
    )  
}

然后再重新写一个 button 组件

js 复制代码
<Button  
title='创建两个按钮'  
onPress={this.createTwoAlertButton}  
color={'green'}  
/>

这里需要注意这个语法:onPress={this.createTwoAlertButton}

然后我们从效果图中可以看到

随着按钮的点击,我们可以看到我们所绑定的逻辑

是可以成功运行输出的。

全部代码

js 复制代码
import React, {Component} from 'react'  
import {Text, View, ScrollView, StyleSheet, Dimensions, Alert, Button} from 'react-native'  
  
export default class FlexDirection extends Component{  
  
createTwoAlertButton = () => {  
Alert.alert(  
"警告标题⚠️",  
"警告内容⚠️",  
[  
{  
text:"取消",  
onPress:() => console.log('Cancel!'),  
style:'cancel'  
},  
{  
text:"确认",  
onPress:() => console.log('Confirm!'),  
style:'default'  
}  
  
]  
)  
}  
render(){  
return (  
<View style={[styles.container]}>  
  
<Button  
title='alert'  
onPress={() =>{alert('我是一个按钮')}}  
/>  
  
  
<Button  
title='Alert.alert'  
onPress={() =>{Alert.alert('我是一个按钮')}}  
color={'red'}  
/>  
  
  
<Button  
title='创建两个按钮'  
onPress={this.createTwoAlertButton}  
color={'green'}  
/>  
</View>  
)  
}  
}  
  
const styles = StyleSheet.create({  
container:{  
flex:1,  
justifyContent:'space-around',  
alignItems:'center'  
},  
})
相关推荐
用户059540174462 分钟前
把大模型记忆召回测试从 30 分钟手工核对压到 3 秒自动化,pytest + FAISS 这套组合救了我
前端·css
醉里博客3 分钟前
醉里起始页 Snavigation2.0:纯前端导航页的二开改造与性能优化实践
前端
雪芽蓝域zzs17 分钟前
新建前端pnpm(vue js ) 仿若依项目(一)
前端·javascript·vue.js
vipbic18 分钟前
网站升级了,我却有点舍不得
前端·vue.js·后端
IT_陈寒18 分钟前
Java Stream并行处理让我数据库崩了两次
前端·人工智能·后端
恋猫de小郭18 分钟前
Flutter 3.47 首坑,analysis_options 问题连环回归
android·前端·flutter
YWL40 分钟前
OpenLayers测距测面:完整测量工具
前端·javascript·vue.js·信息可视化·openlayers
zhanghaha13141 小时前
HTML系列教程:2_HTML 编辑器(零基础超详细讲解
前端·编辑器·html
半旧5181 小时前
【Java Web02】WEB服务器内部实现技术揭秘
java·服务器·前端
拿本唠嗑AI研究1 小时前
现代前端架构爬虫指南:从静态页面到 React/Vue 单页应用
前端·爬虫·架构