The DELETE API in React Native is used to remove data from a backend server. It is commonly used to delete users, posts, products, or any resource identified by an ID.
fetch() or AxiosA DELETE request sends a request to the server without a request body in most cases. The server processes the request and removes the specified resource.
// Basic DELETE request syntax using fetch
fetch("https://example.com/api/users/5", {
method: "DELETE"
});
// Delete user by ID using fetch API
const deleteUser = async (userId) => {
try {
const response = await fetch(`https://example.com/api/users/${userId}`, {
method: "DELETE"
});
if (response.ok) {
console.log("Deleted successfully");
}
} catch (error) {
console.error(error);
}
};
When the DELETE request is successful, the server responds with a confirmation message such as "User deleted successfully".
Click the Delete button to simulate a React Native API call removing an item from a list.
? User List Simulation:
// Button triggered delete example in React Native
import { Button, View, Text } from "react-native";
const UserItem = ({ id, name, onDelete }) => (
<View>
<Text>{name}</Text>
<Button title="Delete" color="red" onPress={() => onDelete(id)} />
</View>
);