File tree Expand file tree Collapse file tree 3 files changed +44
-2
lines changed
advance_react/custom_hooks Expand file tree Collapse file tree 3 files changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -35,7 +35,10 @@ import "./index.css";
3535// import PropDrilling from "./advance_react/prop_drilling/prop_drilling";
3636
3737// ! /* 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" ;
3942
4043function App ( ) {
4144 return (
@@ -73,7 +76,10 @@ function App() {
7376 { /* <PropDrilling /> */ }
7477
7578 { /* contextAPI/useContext */ }
76- < ContextAPI />
79+ { /* <ContextAPI /> */ }
80+
81+ { /* Custom Hooks */ }
82+ < Example />
7783 </ div >
7884 </ React . Fragment >
7985 ) ;
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