Published: April 23 2021

React + Axios - HTTP DELETE Request Examples

Below is a quick set of examples to show how to send HTTP DELETE requests from React to a backend API using the axios HTTP client which is available on npm.

Other HTTP examples available:


Installing axios from npm

With the npm CLI: npm install axios

With the yarn CLI: yarn add axios


Simple DELETE request with axios

This sends an HTTP DELETE request to the Reqres api which is a fake online REST api that includes a /api/posts/:id route that responds to DELETE requests with a HTTP 204 response. When the response is received the React component displays the status message 'Delete successful'.

componentDidMount() {
    // Simple DELETE request with axios
    axios.delete('https://reqres.in/api/posts/1')
        .then(() => this.setState({ status: 'Delete successful' }));
}

Example React component at https://stackblitz.com/edit/react-http-delete-request-examples-axios?file=App/DeleteRequest.jsx


DELETE request using axios with React hooks

This sends the same DELETE request from React using axios, but this version uses React hooks from a function component instead of lifecycle methods from a traditional React class component. The useEffect React hook replaces the componentDidMount lifecycle method to make the HTTP DELETE request when the component loads.

The second parameter to the useEffect React hook is an array of dependencies that determines when the hook is run, passing an empty array causes the hook to only be run once when the component first loads, like the componentDidMount lifecyle method in a class component. For more info on React hooks see https://reactjs.org/docs/hooks-intro.html.

useEffect(() => {
    // DELETE request using axios inside useEffect React hook
    axios.delete('https://reqres.in/api/posts/1')
        .then(() => setStatus('Delete successful'));

// empty dependency array means this effect will only run once (like componentDidMount in classes)
}, []);

Example React hooks component at https://stackblitz.com/edit/react-http-delete-request-examples-axios?file=App/DeleteRequestHooks.jsx


DELETE request using axios with async/await

This sends the same DELETE request from React using axios, but this version uses an async function and the await javascript expression to wait for the promises to return (instead of using the promise then() method as above).

useEffect(() => {
    // DELETE request using axios with async/await
    async function deletePost() {
        await axios.delete('https://reqres.in/api/posts/1');
        setStatus('Delete successful');
    }

    deletePost();
}, []);

Example React component at https://stackblitz.com/edit/react-http-delete-request-examples-axios?file=App/DeleteRequestAsyncAwait.jsx


DELETE request using axios with error handling

This sends a DELETE request from React using axios to an invalid url on the api then assigns the error message to the errorMessage component state property and logs the error to the console.

useEffect(() => {
    // DELETE request using axios with error handling
    axios.delete('https://reqres.in/invalid-url')
        .then(response => setStatus('Delete successful'))
        .catch(error => {
            setErrorMessage(error.message);
            console.error('There was an error!', error);
        });
}, []);

Example React component at https://stackblitz.com/edit/react-http-delete-request-examples-axios?file=App/DeleteRequestErrorHandling.jsx


DELETE request using axios with set HTTP headers

This sends the same DELETE request again from React using axios with a couple of headers set, the HTTP Authorization header and a custom header My-Custom-Header.

useEffect(() => {
    // DELETE request using axios with set headers
    const headers = { 
        'Authorization': 'Bearer my-token',
        'My-Custom-Header': 'foobar'
    };
    axios.delete('https://reqres.in/api/posts/1', { headers })
        .then(() => setStatus('Delete successful'));
}, []);

Example React component at https://stackblitz.com/edit/react-http-delete-request-examples-axios?file=App/DeleteRequestSetHeaders.jsx

 


Need Some React Help?

Search fiverr for freelance React developers.


Follow me for updates

On Twitter or RSS.


When I'm not coding...

Me and Tina are on a motorcycle adventure around Australia.
Come along for the ride!


Comments


Supported by