-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseDataPatch.jsx
More file actions
37 lines (30 loc) · 926 Bytes
/
Copy pathuseDataPatch.jsx
File metadata and controls
37 lines (30 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* eslint-disable react-hooks/exhaustive-deps */
/* eslint-disable react-hooks/rules-of-hooks */
import { useState } from "react";
export default function useUpdateData(url,options) {
const opt = options ? options : {}
const [responseData, setresponseData] = useState(null);
const [error, setError] = useState(null);
const [isLoading, setLoading] = useState(false);
const patchData = async (patchedData) => {
setLoading(true);
try {
const response = await fetch(url, {
...opt,
method: 'PUT',
headers: {
'Content-Type': 'application/json',
...opt?.headers,
},
body: JSON.stringify(patchedData),
});
const result = await response.json();
setresponseData(result);
} catch (error) {
setError(error);
} finally {
setLoading(false);
}
};
return { responseData, error, isLoading, patchData };
}