Skip to main content

DELETE Request

  • Dynamic Routing: You can pass id dynamically in the request or configure it globally when initializing the hook.
  • Error Handling: Handle server errors gracefully with the error property.
  • Global State: Use getLatestData for automatic updates to global or shared data after a delete.

DELETE Request Using Params

import { phantomDelete } from "phantom-request";

function App() {
const { deleteRequest, loading, error, response, latestData } = phantomDelete({
baseURL: "http://localhost:3000/",
route: "driver/delete", // Base route
id: "673d19b1017652a1564ff2ca", // ID to delete
getLatestData: "driver", // Optional route to fetch the latest data after delete
});

const handleDelete = () => {
deleteRequest(); // ID from the hook configuration is used
};

return (
<div>
<button onClick={handleDelete}>Delete Driver</button>
{loading && <p>Deleting...</p>}
{error && <p>Error: {error.message}</p>}
{response && <pre>{JSON.stringify(latestData, null, 2)}</pre>}
</div>
);
}

export default App;

DELETE Request Using Body

import { phantomDelete } from "phantom-request";

function App() {
const { deleteRequest, loading, error, response, latestData } = phantomDelete({
baseURL: "http://localhost:3000/",
route: "driver/delete",
getLatestData: "driver", // Optional route to fetch updated data
});

const handleDelete = () => {
deleteRequest({
body: { id: "673d292c8b1d5b094f5eb2df" }, // Send ID in the body
});
};

return (
<div>
<button onClick={handleDelete}>Delete Driver</button>
{loading && <p>Deleting...</p>}
{error && <p>Error: {error.message}</p>}
{response && <pre>{JSON.stringify(latestData, null, 2)}</pre>}
</div>
);
}

export default App;

phantomDelete

The phantomDelete hook provides an interface for handling DELETE requests. It supports dynamic routing, token-based authentication, custom headers, and additional options.

Hook Parameters

ParameterTypeDefaultDescription
baseURLstringRequiredThe base URL for the API.
routestringRequiredThe endpoint for the DELETE request.
idstringundefinedDefault ID for the request (used for dynamic routes).
tokenstringundefinedToken for Authorization header (Bearer token).
onUnauthorized() => void() => {}Callback triggered when a 401 Unauthorized error occurs.
initialStateanynullInitial state for the response data.
headersRecord<string, string>{}Additional headers for the request.
axiosOptionsAxiosRequestConfig{}Custom Axios configuration for advanced use cases.
getLatestDatastringundefinedEndpoint to fetch updated data after a successful delete.

Return Values

The hook returns the following properties:

PropertyTypeDescription
deleteRequest(options?: { id?: string; body?: Record<string, any>; }) => voidFunction to trigger the DELETE request.
loadingbooleanIndicates if the request is in progress.
erroranyError object from the request (if any).
responseanyResponse data from the DELETE request.
resanyResponse from the DELETE request.
latestDataanyData from the getLatestData route (if configured).