File tree Expand file tree Collapse file tree 5 files changed +227
-6
lines changed Expand file tree Collapse file tree 5 files changed +227
-6
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " react-complete-guide" ,
3
3
"version" : " 0.1.0" ,
4
+ "homepage" : " https://gitname.github.io/react-gh-pages" ,
4
5
"private" : true ,
5
6
"dependencies" : {
6
7
"@testing-library/jest-dom" : " ^5.11.6" ,
12
13
"web-vitals" : " ^0.2.4"
13
14
},
14
15
"scripts" : {
16
+ "predeploy" : " npm run build" ,
17
+ "deploy" : " gh-pages -d build" ,
15
18
"start" : " react-scripts start" ,
16
19
"build" : " react-scripts build" ,
17
20
"test" : " react-scripts test" ,
34
37
" last 1 firefox version" ,
35
38
" last 1 safari version"
36
39
]
40
+ },
41
+ "devDependencies" : {
42
+ "gh-pages" : " ^6.1.0"
37
43
}
38
44
}
Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ function App() {
13
13
setError ( null ) ;
14
14
try {
15
15
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'
17
17
) ;
18
18
19
19
if ( ! response . ok ) {
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ const NewTask = (props) => {
12
12
setError ( null ) ;
13
13
try {
14
14
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' ,
16
16
{
17
17
method : 'POST' ,
18
18
body : JSON . stringify ( { text : taskText } ) ,
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments