Skip to content

Commit e7895b1

Browse files
committed
Add useHttp.js Custom Hook
1 parent 6c73e68 commit e7895b1

File tree

5 files changed

+227
-6
lines changed

5 files changed

+227
-6
lines changed

package-lock.json

Lines changed: 184 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "react-complete-guide",
33
"version": "0.1.0",
4+
"homepage": "https://gitname.github.io/react-gh-pages",
45
"private": true,
56
"dependencies": {
67
"@testing-library/jest-dom": "^5.11.6",
@@ -12,6 +13,8 @@
1213
"web-vitals": "^0.2.4"
1314
},
1415
"scripts": {
16+
"predeploy": "npm run build",
17+
"deploy": "gh-pages -d build",
1518
"start": "react-scripts start",
1619
"build": "react-scripts build",
1720
"test": "react-scripts test",
@@ -34,5 +37,8 @@
3437
"last 1 firefox version",
3538
"last 1 safari version"
3639
]
40+
},
41+
"devDependencies": {
42+
"gh-pages": "^6.1.0"
3743
}
3844
}

src/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function App() {
1313
setError(null);
1414
try {
1515
const response = await fetch(
16-
'https://react-http-89353-default-rtdb.europe-west1.firebasedatabase.app//tasks.json'
16+
'https://react-http-89353-default-rtdb.europe-west1.firebasedatabase.app/tasks.json'
1717
);
1818

1919
if (!response.ok) {

src/components/NewTask/NewTask.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const NewTask = (props) => {
1212
setError(null);
1313
try {
1414
const response = await fetch(
15-
'https://react-http-6b4a6.firebaseio.com/tasks.json',
15+
'https://react-http-89353-default-rtdb.europe-west1.firebasedatabase.app/tasks.json',
1616
{
1717
method: 'POST',
1818
body: JSON.stringify({ text: taskText }),

src/hooks/useHttp.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { useState } from 'react';
2+
3+
const useHttp = (requestConfig, applyData) => {
4+
const [isLoading, setIsLoading] = useState(false);
5+
const [error, setError] = useState(null);
6+
7+
const sendRequest = async () => {
8+
setIsLoading(true);
9+
setError(null);
10+
try {
11+
const response = await fetch(requestConfig.url, {
12+
method: requestConfig.method,
13+
headers: requestConfig.headers,
14+
body: JSON.stringify(requestConfig.body),
15+
});
16+
17+
if (!response.ok) {
18+
throw new Error('Request failed!');
19+
}
20+
21+
const data = await response.json();
22+
applyData(data);
23+
} catch (err) {
24+
setError(err.message || 'Something went wrong!');
25+
}
26+
setIsLoading(false);
27+
};
28+
29+
return {
30+
isLoading,
31+
error,
32+
sendRequest,
33+
};
34+
};
35+
export default useHttp;

0 commit comments

Comments
 (0)