[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'  
},  
})
相关推荐
苦瓜小生9 分钟前
【前端】|【js手撕】经典高频面试题:手写实现function.call、apply、bind
java·前端·javascript
天若有情67316 分钟前
前端HTML精讲03:页面性能优化+懒加载,搞定首屏加速
前端·性能优化·html
踩着两条虫28 分钟前
AI驱动的Vue3应用开发平台深入探究(十):物料系统之内置组件库
android·前端·vue.js·人工智能·低代码·系统架构·rxjava
swipe1 小时前
AI 应用里的 Memory,不是“保存聊天记录”,而是管理上下文预算
前端·llm·agent
慧一居士1 小时前
nuxt3 项目和nuxt4 项目区别和对比
前端·vue.js
威联通安全存储2 小时前
破除“重前端、轻底层”的数字幻象:如何夯实工业数据的物理底座
前端·python
inksci2 小时前
Js生成安全随机数
前端·微信小程序
吴声子夜歌2 小时前
TypeScript——泛型
前端·git·typescript
猩猩程序员3 小时前
Pretext:一个绕过 DOM 的纯 JavaScript 排版引擎
前端
竹林8183 小时前
从“连接失败”到丝滑登录:我用 ethers.js 连接 MetaMask 的完整踩坑实录
前端·javascript