Skip to content

Commit 08f8700

Browse files
Added Custom Hooks
1 parent f28e180 commit 08f8700

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/App.js

+8-2
Original file line numberDiff line numberDiff 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

4043
function 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 numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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;
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}

0 commit comments

Comments
 (0)