[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'  
},  
})
相关推荐
MickeyCV21 分钟前
Nginx学习笔记:常用命令&端口占用报错解决&Nginx核心配置文件解读
前端·nginx
祈澈菇凉38 分钟前
webpack和grunt以及gulp有什么不同?
前端·webpack·gulp
十步杀一人_千里不留行41 分钟前
React Native 下拉选择组件首次点击失效问题的深入分析与解决
javascript·react native·react.js
zy0101011 小时前
HTML列表,表格和表单
前端·html
m0_748232641 小时前
鸿蒙NEXT(五):鸿蒙版React Native架构浅析
react native·架构·harmonyos
初辰ge1 小时前
【p-camera-h5】 一款开箱即用的H5相机插件,支持拍照、录像、动态水印与样式高度定制化。
前端·相机
HugeYLH1 小时前
解决npm问题:错误的代理设置
前端·npm·node.js
六个点2 小时前
DNS与获取页面白屏时间
前端·面试·dns
道不尽世间的沧桑2 小时前
第9篇:插槽(Slots)的使用
前端·javascript·vue.js
bin91532 小时前
DeepSeek 助力 Vue 开发:打造丝滑的滑块(Slider)
前端·javascript·vue.js·前端框架·ecmascript·deepseek