React Native 删除提示的实现方法
import React, { useState } from 'react';
import { View, Text, Button, Alert } from 'react-native';
const DeleteConfirmation = ({ onDelete }) => {
const [isVisible, setIsVisible] = useState(false);
const showDeleteConfirmation = () => {
setIsVisible(true);
};
const hideDeleteConfirmation = () => {
setIsVisible(false);
};
const handleDelete = () => {
onDelete();
hideDeleteConfirmation();
};
return (
<View>
<Button title="Delete" onPress={showDeleteConfirmation} />
{isVisible && (
<Alert
title="Delete Item?"
message="Are you sure you want to delete this item?"
buttons={[
{ text: 'Cancel', onPress: hideDeleteConfirmation },
{ text: 'Delete', onPress: handleDelete },
]}
/>
)}
</View>
);
};
export default DeleteConfirmation;
这段代码使用了React Native的Alert
组件来实现一个简单的删除确认功能。用户点击按钮后会弹出一个警告框,询问用户是否确定要删除项目。如果用户确认删除,onDelete
回调函数会被调用,并且删除确认对话框会被关闭。这个例子展示了如何使用React Hooks和React Native组件来实现一个简单的交互逻辑。
评论已关闭