Home Tutorial React Native React Native – Bài 22: Alert

React Native – Bài 22: Alert

1 min read
0
135

Cũng như ứng dụng web, react native cung cấp một component dùng hiển thị alert (cảnh báo) .

Bước 1: App.js

import React from 'react'
import AlertExample from './alert_example.js'

const App = () => {
   return (
      <AlertExample />
   )
}
export default App

Bước 2: alert_example.js

Tạo một nút để kích hoạt chức năng showAlert .

import React from 'react'
import { Alert, Text, TouchableOpacity, StyleSheet } from 'react-native'

const AlertExample = () => {
   const showAlert = () =>{
      Alert.alert(
         'You need to...'
      )
   }
   return (
      <TouchableOpacity onPress = {showAlert} style = {styles.button}>
         <Text>Alert</Text>
      </TouchableOpacity>
   )
}
export default AlertExample

const styles = StyleSheet.create ({
   button: {
      backgroundColor: '#4ba37b',
      width: 100,
      borderRadius: 50,
      alignItems: 'center',
      marginTop: 100
   }
})

Kết quả

Phản ứng cảnh báo bản địa

Khi bạn nhấp vào nút, bạn sẽ thấy như sau :

Load More Related Articles
Load More By quangvu
Load More In React Native

Check Also

Tự làm web chatGPT đơn giản cho phép dùng nhiều OpenAI key

Trong thời đại công nghệ phát triển như hiện nay, chatbot đang dần trở thành một giải pháp…