Skip to content

Commit 93efd1a

Browse files
PrasoonPrasoon
Prasoon
authored and
Prasoon
committed
React custom hooks
1 parent ffa9c74 commit 93efd1a

11 files changed

+85
-158
lines changed

README.md

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,15 @@
1-
# Getting Started with Create React App
1+
## Custom Hooks in React - useFetch
22

3-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
3+
### This repo shows how to create your very own useFetch custom hook in React to fetch Data in your React Components
44

5-
## Available Scripts
5+
### Youtube Tutorial Link - https://youtu.be/b6dnwttsdTo
66

7-
In the project directory, you can run:
7+
#### 📚 Materials/References:
88

9-
### `npm start`
9+
- Create react app: https://reactjs.org/docs/create-a-new-react-app.html
1010

11-
Runs the app in the development mode.\
12-
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11+
- Random User data API: https://random-data-api.com/api/users/random_user
1312

14-
The page will reload if you make edits.\
15-
You will also see any lint errors in the console.
13+
- Random Address data API: https://random-data-api.com/api/address/random_address
1614

17-
### `npm test`
18-
19-
Launches the test runner in the interactive watch mode.\
20-
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21-
22-
### `npm run build`
23-
24-
Builds the app for production to the `build` folder.\
25-
It correctly bundles React in production mode and optimizes the build for the best performance.
26-
27-
The build is minified and the filenames include the hashes.\
28-
Your app is ready to be deployed!
29-
30-
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31-
32-
### `npm run eject`
33-
34-
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
35-
36-
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37-
38-
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
39-
40-
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
41-
42-
## Learn More
43-
44-
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45-
46-
To learn React, check out the [React documentation](https://reactjs.org/).
47-
48-
### Code Splitting
49-
50-
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51-
52-
### Analyzing the Bundle Size
53-
54-
This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55-
56-
### Making a Progressive Web App
57-
58-
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59-
60-
### Advanced Configuration
61-
62-
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63-
64-
### Deployment
65-
66-
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67-
68-
### `npm run build` fails to minify
69-
70-
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
15+
- GitHub Code for the project: https://github.com/FullstackSimplified/react-useFetch-custom-hook

src/Address.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { useEffect, useState } from "react";
2+
import useFetch from "./useFetch";
3+
const Address = () => {
4+
const url = "https://random-data-api.com/api/address/random_address";
5+
const {
6+
loading: addressLoading,
7+
data: addressData,
8+
error,
9+
} = useFetch(url, null);
10+
return (
11+
<div style={{ margin: 40 }}>
12+
<h1>Address Component</h1>
13+
{addressLoading && <p>Loading address....</p>}
14+
{addressData && <p>Random Address: {addressData?.full_address}</p>}
15+
{error && <p>Opps something went wrong</p>}
16+
</div>
17+
);
18+
};
19+
20+
export default Address;

src/App.css

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/App.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
1-
import logo from './logo.svg';
2-
import './App.css';
1+
import Address from "./Address";
2+
import User from "./User";
33

4-
function App() {
4+
const App = () => {
55
return (
6-
<div className="App">
7-
<header className="App-header">
8-
<img src={logo} className="App-logo" alt="logo" />
9-
<p>
10-
Edit <code>src/App.js</code> and save to reload.
11-
</p>
12-
<a
13-
className="App-link"
14-
href="https://reactjs.org"
15-
target="_blank"
16-
rel="noopener noreferrer"
17-
>
18-
Learn React
19-
</a>
20-
</header>
6+
<div>
7+
<User />
8+
<Address />
219
</div>
2210
);
23-
}
11+
};
2412

2513
export default App;

src/App.test.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/User.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useEffect, useState } from "react";
2+
import useFetch from "./useFetch";
3+
const User = () => {
4+
const url = "https://random-data-api.com/api/users/random_user";
5+
const { loading: userLoading, data: userData, error } = useFetch(url, null);
6+
7+
return (
8+
<div style={{ margin: 40 }}>
9+
<h1>User Component</h1>
10+
{userLoading && <p>Loading user....</p>}
11+
{userData && (
12+
<p>
13+
Random user: {userData?.first_name} {userData?.last_name}
14+
</p>
15+
)}
16+
{error && <p>Opps something went wrong</p>}
17+
</div>
18+
);
19+
};
20+
21+
export default User;

src/index.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
import React from 'react';
2-
import ReactDOM from 'react-dom';
3-
import './index.css';
4-
import App from './App';
5-
import reportWebVitals from './reportWebVitals';
1+
import React from "react";
2+
import ReactDOM from "react-dom";
3+
import "./index.css";
4+
import App from "./App";
65

76
ReactDOM.render(
87
<React.StrictMode>
98
<App />
109
</React.StrictMode>,
11-
document.getElementById('root')
10+
document.getElementById("root")
1211
);
13-
14-
// If you want to start measuring performance in your app, pass a function
15-
// to log results (for example: reportWebVitals(console.log))
16-
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
17-
reportWebVitals();

src/logo.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/reportWebVitals.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/setupTests.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/useFetch.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { useState, useEffect } from "react";
2+
3+
const useFetch = (url, initalData) => {
4+
const [data, setData] = useState(initalData);
5+
const [loading, setLoading] = useState(false);
6+
const [error, setError] = useState(false);
7+
8+
useEffect(() => {
9+
setLoading(true);
10+
fetch(url)
11+
.then((res) => res.json())
12+
.then((res) => {
13+
if (res.error) setError(true);
14+
else setData(res);
15+
})
16+
.catch((err) => {
17+
setError(true);
18+
})
19+
.finally(() => setLoading(false));
20+
}, []);
21+
22+
return { data, loading, error };
23+
};
24+
export default useFetch;

0 commit comments

Comments
 (0)