EN
React - Axios - PATCH request
3 points
In this article, we would like to show you how to make Axios PATCH requests in React.
Note: it is required to add external library to run below example, so use:
xxxxxxxxxx
1npm install axios
In the example below, we use async-await
, but you might as well do it on promises.
Note: we are making a request to jsonplaceholder, so the example will work when you copy it.
xxxxxxxxxx
1
import React from 'react'
2
import axios from 'axios'
3
4
const App = () => {
5
const userToPatch = {
6
title: 'foo',
7
};
8
9
const handleClick = async () => {
10
const response = await axios
11
.patch('https://jsonplaceholder.typicode.com/posts/1', userToPatch)
12
.catch((error) => console.log('Error: ', error));
13
if (response && response.data) {
14
console.log(response);
15
console.log(response.data);
16
}
17
};
18
return (
19
<div>
20
<button onClick={handleClick}>Click to send PATCH request</button>
21
</div>
22
);
23
};
24
25
export default App;
Online runnable example: