├── .gitignore ├── README.md ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── src ├── App.js ├── AsyncAwait.js ├── ButtonClick.js ├── ErrorAsyncAwait.js ├── ErrorThen.js ├── Fetcher.js ├── LoadingText.js ├── PassParam.js ├── SearchUser.js ├── UsingAxios.js ├── UsingCustomHook.js ├── UsingFetch.js ├── UsingHoc.js ├── UsingRenderProps.js ├── index.css ├── index.js ├── useFetch.js └── withFetching.js └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Getting Started with Create React App 2 | 3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 4 | 5 | ## Available Scripts 6 | 7 | In the project directory, you can run: 8 | 9 | ### `yarn start` 10 | 11 | Runs the app in the development mode.\ 12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 13 | 14 | The page will reload if you make edits.\ 15 | You will also see any lint errors in the console. 16 | 17 | ### `yarn 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 | ### `yarn 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 | ### `yarn 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 | ### `yarn 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) 71 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-fetch-data", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.11.4", 7 | "@testing-library/react": "^11.1.0", 8 | "@testing-library/user-event": "^12.1.10", 9 | "axios": "^0.24.0", 10 | "react": "^17.0.2", 11 | "react-dom": "^17.0.2", 12 | "react-router-dom": "^5.3.0", 13 | "react-scripts": "4.0.3", 14 | "web-vitals": "^1.0.1" 15 | }, 16 | "scripts": { 17 | "start": "react-scripts start", 18 | "build": "react-scripts build", 19 | "test": "react-scripts test", 20 | "eject": "react-scripts eject" 21 | }, 22 | "eslintConfig": { 23 | "extends": [ 24 | "react-app", 25 | "react-app/jest" 26 | ] 27 | }, 28 | "browserslist": { 29 | "production": [ 30 | ">0.2%", 31 | "not dead", 32 | "not op_mini all" 33 | ], 34 | "development": [ 35 | "last 1 chrome version", 36 | "last 1 firefox version", 37 | "last 1 safari version" 38 | ] 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/collegewap/react-fetch-data/84fbdb6e25eb2c04939ad40517519b0380093bcf/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/collegewap/react-fetch-data/84fbdb6e25eb2c04939ad40517519b0380093bcf/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/collegewap/react-fetch-data/84fbdb6e25eb2c04939ad40517519b0380093bcf/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom"; 4 | 5 | import AsyncAwait from "./AsyncAwait"; 6 | import ButtonClick from "./ButtonClick"; 7 | import ErrorAsyncAwait from "./ErrorAsyncAwait"; 8 | import ErrorThen from "./ErrorThen"; 9 | import LoadingText from "./LoadingText"; 10 | import PassParam from "./PassParam"; 11 | import SearchUser from "./SearchUser"; 12 | import UsingAxios from "./UsingAxios"; 13 | import UsingCustomHook from "./UsingCustomHook"; 14 | import UsingFetch from "./UsingFetch"; 15 | import UsingHoc from "./UsingHoc"; 16 | import UsingRenderProps from "./UsingRenderProps"; 17 | 18 | const App = () => { 19 | return ( 20 |
21 | 22 |
23 | 63 |
64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 |
103 |
104 |
105 |
106 | ); 107 | }; 108 | 109 | export default App; 110 | -------------------------------------------------------------------------------- /src/AsyncAwait.js: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from "react"; 2 | 3 | const AsyncAwait = () => { 4 | const [users, setUsers] = useState([]); 5 | 6 | const fetchData = async () => { 7 | const response = await fetch("https://jsonplaceholder.typicode.com/users"); 8 | const data = await response.json(); 9 | setUsers(data); 10 | }; 11 | 12 | useEffect(() => { 13 | fetchData(); 14 | }, []); 15 | 16 | return ( 17 |
18 | {users.length > 0 && ( 19 | 24 | )} 25 |
26 | ); 27 | }; 28 | 29 | export default AsyncAwait; 30 | -------------------------------------------------------------------------------- /src/ButtonClick.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | 3 | const ButtonClick = () => { 4 | const [users, setUsers] = useState([]); 5 | 6 | const fetchData = () => { 7 | fetch("https://jsonplaceholder.typicode.com/users") 8 | .then((response) => { 9 | return response.json(); 10 | }) 11 | .then((data) => { 12 | setUsers(data); 13 | }); 14 | }; 15 | 16 | return ( 17 |
18 | 19 | {users.length > 0 && ( 20 | 25 | )} 26 |
27 | ); 28 | }; 29 | 30 | export default ButtonClick; 31 | -------------------------------------------------------------------------------- /src/ErrorAsyncAwait.js: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from "react"; 2 | 3 | const ErrorAsyncAwait = () => { 4 | const [users, setUsers] = useState([]); 5 | const [error, setError] = useState(""); 6 | 7 | const fetchData = async () => { 8 | setError(""); 9 | try { 10 | const response = await fetch("https://jsonplaceholder.typicode.com/404"); 11 | if (!response.ok) { 12 | // If the API responds meaningful error message, 13 | // then you can get it by calling response.statusText 14 | throw new Error("Sorry something went wrong"); 15 | } 16 | const data = await response.json(); 17 | setUsers(data); 18 | } catch (error) { 19 | // It is always recommended to define the error messages 20 | // in the client side rather than simply relying on the server messages, 21 | // since server messages might not make sense to end user most of the time. 22 | setError(error.message); 23 | } 24 | }; 25 | 26 | useEffect(() => { 27 | fetchData(); 28 | }, []); 29 | 30 | return ( 31 |
32 | {error &&

{error}

} 33 | {users.length > 0 && ( 34 | 39 | )} 40 |
41 | ); 42 | }; 43 | 44 | export default ErrorAsyncAwait; 45 | -------------------------------------------------------------------------------- /src/ErrorThen.js: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from "react"; 2 | 3 | const ErrorThen = () => { 4 | const [users, setUsers] = useState([]); 5 | const [error, setError] = useState(""); 6 | 7 | const fetchData = () => { 8 | setError(""); 9 | fetch("https://jsonplaceholder.typicode.com/404") 10 | .then((response) => { 11 | // If the HTTP response is 2xx then it response.ok will have a value of true 12 | if (response.ok) { 13 | return response.json(); 14 | } else { 15 | // If the API responds meaningful error message, 16 | // then you can get it by calling response.statusText 17 | throw new Error("Sorry something went wrong"); 18 | } 19 | }) 20 | .then((data) => { 21 | setUsers(data); 22 | }) 23 | .catch((error) => { 24 | // It is always recommended to define the error messages 25 | // in the client side rather than simply relying on the server messages, 26 | // since server messages might not make sense to end user most of the time. 27 | setError(error.message); 28 | }); 29 | }; 30 | 31 | useEffect(() => { 32 | fetchData(); 33 | }, []); 34 | 35 | return ( 36 |
37 | {error &&

{error}

} 38 | {users.length > 0 && ( 39 | 44 | )} 45 |
46 | ); 47 | }; 48 | 49 | export default ErrorThen; 50 | -------------------------------------------------------------------------------- /src/Fetcher.js: -------------------------------------------------------------------------------- 1 | import axios from "axios"; 2 | import { useEffect, useState } from "react"; 3 | 4 | const Fetcher = ({ url, children }) => { 5 | const [users, setUsers] = useState([]); 6 | const [error, setError] = useState(""); 7 | const [isLoading, setIsLoading] = useState(false); 8 | 9 | useEffect(() => { 10 | setIsLoading(true); 11 | axios 12 | .get(url) 13 | .then((response) => { 14 | setUsers(response.data); 15 | setIsLoading(false); 16 | }) 17 | .catch((error) => { 18 | setError("Sorry, something went wrong"); 19 | setIsLoading(false); 20 | }); 21 | }, [url]); 22 | 23 | return children({ users, error, isLoading }); 24 | }; 25 | 26 | export default Fetcher; 27 | -------------------------------------------------------------------------------- /src/LoadingText.js: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from "react"; 2 | 3 | const LoadingText = () => { 4 | const [users, setUsers] = useState([]); 5 | const [isLoading, setIsLoading] = useState(false); 6 | 7 | const fetchData = () => { 8 | setIsLoading(true); 9 | fetch("https://jsonplaceholder.typicode.com/users") 10 | .then((response) => { 11 | return response.json(); 12 | }) 13 | .then((data) => { 14 | setIsLoading(false); 15 | setUsers(data); 16 | }); 17 | }; 18 | 19 | useEffect(() => { 20 | fetchData(); 21 | }, []); 22 | 23 | return ( 24 |
25 | {isLoading &&

Loading...

} 26 | {users.length > 0 && ( 27 | 32 | )} 33 |
34 | ); 35 | }; 36 | 37 | export default LoadingText; 38 | -------------------------------------------------------------------------------- /src/PassParam.js: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from "react"; 2 | 3 | const PassParam = () => { 4 | const [user, setUser] = useState([]); 5 | const id = 1; 6 | 7 | const fetchData = () => { 8 | fetch(`https://jsonplaceholder.typicode.com/users?id=${id}`) 9 | .then((response) => { 10 | return response.json(); 11 | }) 12 | .then((data) => { 13 | setUser(data[0].name); 14 | }); 15 | }; 16 | 17 | useEffect(() => { 18 | fetchData(); 19 | }, []); 20 | 21 | return
Name: {user}
; 22 | }; 23 | 24 | export default PassParam; 25 | -------------------------------------------------------------------------------- /src/SearchUser.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | 3 | const SearchUser = () => { 4 | const [users, setUsers] = useState([]); 5 | 6 | const fetchData = (e) => { 7 | const query = e.target.value; 8 | fetch(`https://jsonplaceholder.typicode.com/users?q=${query}`) 9 | .then((response) => { 10 | return response.json(); 11 | }) 12 | .then((data) => { 13 | setUsers(data); 14 | }); 15 | }; 16 | 17 | return ( 18 |
19 | 20 | {users.length > 0 && ( 21 | 26 | )} 27 |
28 | ); 29 | }; 30 | 31 | export default SearchUser; 32 | -------------------------------------------------------------------------------- /src/UsingAxios.js: -------------------------------------------------------------------------------- 1 | import axios from "axios"; 2 | import React, { useEffect, useState } from "react"; 3 | 4 | const UsingAxios = () => { 5 | const [users, setUsers] = useState([]); 6 | 7 | const fetchData = () => { 8 | axios 9 | .get("https://jsonplaceholder.typicode.com/users") 10 | .then((response) => { 11 | setUsers(response.data); 12 | }) 13 | .catch((error) => { 14 | console.log({ error }); 15 | // Handle error 16 | }); 17 | }; 18 | 19 | useEffect(() => { 20 | fetchData(); 21 | }, []); 22 | 23 | return ( 24 |
25 | {users.length > 0 && ( 26 | 31 | )} 32 |
33 | ); 34 | }; 35 | 36 | export default UsingAxios; 37 | -------------------------------------------------------------------------------- /src/UsingCustomHook.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import useFetch from "./useFetch"; 3 | const url = "https://jsonplaceholder.typicode.com/users"; 4 | 5 | const UsingCustomHook = () => { 6 | const { users, error, isLoading } = useFetch(url); 7 | 8 | if (isLoading) { 9 | return
Loading..
; 10 | } 11 | if (error) { 12 | return
{error}
; 13 | } 14 | return ( 15 |
16 | {users.length > 0 && ( 17 | 22 | )} 23 |
24 | ); 25 | }; 26 | 27 | export default UsingCustomHook; 28 | -------------------------------------------------------------------------------- /src/UsingFetch.js: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from "react"; 2 | 3 | const UsingFetch = () => { 4 | const [users, setUsers] = useState([]); 5 | 6 | const fetchData = () => { 7 | fetch("https://jsonplaceholder.typicode.com/users") 8 | .then((response) => { 9 | return response.json(); 10 | }) 11 | .then((data) => { 12 | setUsers(data); 13 | }); 14 | }; 15 | 16 | useEffect(() => { 17 | fetchData(); 18 | }, []); 19 | 20 | return ( 21 |
22 | {users.length > 0 && ( 23 | 28 | )} 29 |
30 | ); 31 | }; 32 | 33 | export default UsingFetch; 34 | -------------------------------------------------------------------------------- /src/UsingHoc.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import withFetching from "./withFetching"; 3 | const url = "https://jsonplaceholder.typicode.com/users"; 4 | 5 | const UsingHoc = ({ isLoading, error, users }) => { 6 | if (isLoading) { 7 | return
Loading..
; 8 | } 9 | if (error) { 10 | return
{error}
; 11 | } 12 | return ( 13 |
14 | {users.length > 0 && ( 15 | 20 | )} 21 |
22 | ); 23 | }; 24 | 25 | export default withFetching(url)(UsingHoc); 26 | -------------------------------------------------------------------------------- /src/UsingRenderProps.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import Fetcher from "./Fetcher"; 3 | const url = "https://jsonplaceholder.typicode.com/users"; 4 | 5 | const UsingRenderProps = () => { 6 | return ( 7 | 8 | {({ isLoading, error, users }) => { 9 | if (isLoading) { 10 | return
Loading..
; 11 | } 12 | if (error) { 13 | return
{error}
; 14 | } 15 | return ( 16 |
17 | {users.length > 0 && ( 18 | 23 | )} 24 |
25 | ); 26 | }} 27 |
28 | ); 29 | }; 30 | 31 | export default UsingRenderProps; 32 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | display: flex; 3 | justify-content: center; 4 | } 5 | 6 | .content { 7 | max-width: 400px; 8 | margin: 0 auto; 9 | display: flex; 10 | justify-content: center; 11 | } 12 | 13 | .App button { 14 | width: 100px; 15 | margin: 0 auto; 16 | display: block; 17 | } 18 | 19 | .App input { 20 | margin: 0 auto; 21 | display: block; 22 | } 23 | 24 | nav ul li { 25 | padding: 0; 26 | margin: 0; 27 | } 28 | 29 | nav ul { 30 | list-style-type: none; 31 | display: flex; 32 | } 33 | 34 | nav ul a { 35 | text-decoration: none; 36 | } 37 | 38 | nav ul li { 39 | padding: 10px; 40 | border-top: 1px solid; 41 | border-bottom: 1px solid; 42 | border-left: 1px solid; 43 | } 44 | 45 | nav ul:nth-child(n) { 46 | border-right: 1px solid; 47 | } 48 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import "./index.css"; 4 | import App from "./App"; 5 | 6 | ReactDOM.render( 7 | 8 | 9 | , 10 | document.getElementById("root") 11 | ); 12 | -------------------------------------------------------------------------------- /src/useFetch.js: -------------------------------------------------------------------------------- 1 | import axios from "axios"; 2 | import { useEffect, useState } from "react"; 3 | 4 | const useFetch = (url) => { 5 | const [users, setUsers] = useState([]); 6 | const [error, setError] = useState(""); 7 | const [isLoading, setIsLoading] = useState(false); 8 | 9 | useEffect(() => { 10 | setIsLoading(true); 11 | axios 12 | .get(url) 13 | .then((response) => { 14 | setUsers(response.data); 15 | setIsLoading(false); 16 | }) 17 | .catch((error) => { 18 | setError("Sorry, something went wrong"); 19 | setIsLoading(false); 20 | }); 21 | }, [url]); 22 | 23 | return { users, error, isLoading }; 24 | }; 25 | 26 | export default useFetch; 27 | -------------------------------------------------------------------------------- /src/withFetching.js: -------------------------------------------------------------------------------- 1 | import axios from "axios"; 2 | import React, { useEffect, useState } from "react"; 3 | 4 | const withFetching = (url) => (Component) => { 5 | return () => { 6 | const [users, setUsers] = useState([]); 7 | const [error, setError] = useState(""); 8 | const [isLoading, setIsLoading] = useState(false); 9 | 10 | const fetchData = () => { 11 | setIsLoading(true); 12 | axios 13 | .get(url) 14 | .then((response) => { 15 | setUsers(response.data); 16 | setIsLoading(false); 17 | }) 18 | .catch((error) => { 19 | setError("Sorry, something went wrong"); 20 | setIsLoading(false); 21 | }); 22 | }; 23 | 24 | useEffect(() => { 25 | fetchData(); 26 | }, []); 27 | 28 | return ; 29 | }; 30 | }; 31 | 32 | export default withFetching; 33 | --------------------------------------------------------------------------------