File tree 3 files changed +44
-2
lines changed
advance_react/custom_hooks
3 files changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -35,7 +35,10 @@ import "./index.css";
35
35
// import PropDrilling from "./advance_react/prop_drilling/prop_drilling";
36
36
37
37
// ! /* ContextAPI/useContext */
38
- import ContextAPI from "./advance_react/useContext/useContext" ;
38
+ // import ContextAPI from "./advance_react/useContext/useContext";
39
+
40
+ // ! /* Custom Hooks */
41
+ import Example from "./advance_react/custom_hooks/customHookExample" ;
39
42
40
43
function App ( ) {
41
44
return (
@@ -73,7 +76,10 @@ function App() {
73
76
{ /* <PropDrilling /> */ }
74
77
75
78
{ /* contextAPI/useContext */ }
76
- < ContextAPI />
79
+ { /* <ContextAPI /> */ }
80
+
81
+ { /* Custom Hooks */ }
82
+ < Example />
77
83
</ div >
78
84
</ React . Fragment >
79
85
) ;
Original file line number Diff line number Diff line change
1
+ import React from "react" ;
2
+
3
+ // importing custom hook
4
+ import { useFetch } from "./useFetch" ;
5
+
6
+ const url = "https://api.github.com/users" ;
7
+
8
+ function Example ( ) {
9
+ const { isLoading, data } = useFetch ( url ) ;
10
+ return < h1 > { isLoading ? "Loading..." : "Data is in :)" } </ h1 > ;
11
+ }
12
+
13
+ export default Example ;
Original file line number Diff line number Diff line change
1
+ import { useState , useEffect } from "react" ;
2
+
3
+ export function useFetch ( url ) {
4
+ const [ isLoading , setIsLoading ] = useState ( true ) ;
5
+ const [ data , setData ] = useState ( [ ] ) ;
6
+
7
+ const getData = async ( ) => {
8
+ try {
9
+ const response = await fetch ( url ) ;
10
+ const parseData = await response . json ( ) ;
11
+ setData ( parseData ) ;
12
+ setIsLoading ( false ) ;
13
+ } catch ( error ) {
14
+ console . error ( error ) ;
15
+ }
16
+ } ;
17
+
18
+ useEffect ( ( ) => {
19
+ getData ( ) ;
20
+ } , [ url ] ) ;
21
+
22
+ return { isLoading, data } ;
23
+ }
You can’t perform that action at this time.
0 commit comments