├── public ├── robots.txt ├── favicon.ico ├── logo192.png ├── logo512.png ├── manifest.json └── index.html ├── README.md ├── src ├── UseCallback │ ├── Child.js │ └── CallbackTutorial.js ├── UseContext │ ├── User.js │ ├── Login.js │ └── ContextTutorial.js ├── reportWebVitals.js ├── UseRef │ └── RefTutorial.js ├── UseState │ └── StateTutorial.js ├── UseImperativeHandle │ ├── Button.js │ └── ImperativeHandle.js ├── index.js ├── UseLayoutEffect │ └── LayoutEffectTutorial.js ├── App.js ├── App.css ├── UseEffect │ └── EffectTutorial.js ├── UseReducer │ └── ReducerTutorial.js └── UseMemo │ └── MemoTutorial.js ├── .gitignore └── package.json /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/machadop1407/react-hooks-course/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/machadop1407/react-hooks-course/HEAD/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/machadop1407/react-hooks-course/HEAD/public/logo512.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # This Is a Repo With examples for each React Hook 2 | 3 | ## This was made in the following youtube tutorial: [React Hooks Explained](https://youtu.be/LlvBzyy-558) 4 | 5 | -------------------------------------------------------------------------------- /src/UseCallback/Child.js: -------------------------------------------------------------------------------- 1 | import React, { useEffect } from "react"; 2 | 3 | function Child({ returnComment }) { 4 | useEffect(() => { 5 | console.log("FUNCTION WAS CALLED"); 6 | }, [returnComment]); 7 | 8 | return
{returnComment("Pedro")}
; 9 | } 10 | 11 | export default Child; 12 | -------------------------------------------------------------------------------- /src/UseContext/User.js: -------------------------------------------------------------------------------- 1 | import React, { useContext } from "react"; 2 | import { AppContext } from "./ContextTutorial"; 3 | 4 | function User() { 5 | const { username } = useContext(AppContext); 6 | 7 | return ( 8 |
9 |

User: {username}

10 |
11 | ); 12 | } 13 | 14 | export default User; 15 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /src/UseContext/Login.js: -------------------------------------------------------------------------------- 1 | import React, { useContext } from "react"; 2 | import { AppContext } from "./ContextTutorial"; 3 | 4 | function Login() { 5 | const { setUsername } = useContext(AppContext); 6 | 7 | return ( 8 |
9 | { 11 | setUsername(event.target.value); 12 | }} 13 | /> 14 |
15 | ); 16 | } 17 | 18 | export default Login; 19 | -------------------------------------------------------------------------------- /src/reportWebVitals.js: -------------------------------------------------------------------------------- 1 | const reportWebVitals = onPerfEntry => { 2 | if (onPerfEntry && onPerfEntry instanceof Function) { 3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 4 | getCLS(onPerfEntry); 5 | getFID(onPerfEntry); 6 | getFCP(onPerfEntry); 7 | getLCP(onPerfEntry); 8 | getTTFB(onPerfEntry); 9 | }); 10 | } 11 | }; 12 | 13 | export default reportWebVitals; 14 | -------------------------------------------------------------------------------- /src/UseRef/RefTutorial.js: -------------------------------------------------------------------------------- 1 | import React, { useRef } from "react"; 2 | 3 | function RefTutorial() { 4 | const inputRef = useRef(null); 5 | 6 | const onClick = () => { 7 | inputRef.current.value = ""; 8 | }; 9 | return ( 10 |
11 |

Pedro

12 | 13 | 14 |
15 | ); 16 | } 17 | 18 | export default RefTutorial; 19 | -------------------------------------------------------------------------------- /src/UseContext/ContextTutorial.js: -------------------------------------------------------------------------------- 1 | import React, { useState, createContext } from "react"; 2 | import Login from "./Login"; 3 | import User from "./User"; 4 | 5 | export const AppContext = createContext(null); 6 | 7 | function ContextTutorial() { 8 | const [username, setUsername] = useState(""); 9 | 10 | return ( 11 | 12 | 13 | 14 | ); 15 | } 16 | 17 | export default ContextTutorial; 18 | -------------------------------------------------------------------------------- /src/UseState/StateTutorial.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | 3 | const StateTutorial = () => { 4 | const [inputValue, setInputValue] = useState("Pedro"); 5 | 6 | let onChange = (event) => { 7 | const newValue = event.target.value; 8 | setInputValue(newValue); 9 | }; 10 | 11 | return ( 12 |
13 | 14 | {inputValue} 15 |
16 | ); 17 | }; 18 | 19 | export default StateTutorial; 20 | -------------------------------------------------------------------------------- /src/UseImperativeHandle/Button.js: -------------------------------------------------------------------------------- 1 | import React, { forwardRef, useImperativeHandle, useState } from "react"; 2 | 3 | const Button = forwardRef((props, ref) => { 4 | const [toggle, setToggle] = useState(false); 5 | 6 | useImperativeHandle(ref, () => ({ 7 | alterToggle() { 8 | setToggle(!toggle); 9 | }, 10 | })); 11 | return ( 12 | <> 13 | 14 | {toggle && Toggle} 15 | 16 | ); 17 | }); 18 | 19 | export default Button; 20 | -------------------------------------------------------------------------------- /src/UseImperativeHandle/ImperativeHandle.js: -------------------------------------------------------------------------------- 1 | import React, { useRef } from "react"; 2 | import Button from "./Button"; 3 | 4 | function ImperativeHandle() { 5 | const buttonRef = useRef(null); 6 | return ( 7 |
8 | 15 |
17 | ); 18 | } 19 | 20 | export default ImperativeHandle; 21 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import App from "./App"; 4 | import reportWebVitals from "./reportWebVitals"; 5 | 6 | ReactDOM.render( 7 | 8 | 9 | , 10 | document.getElementById("root") 11 | ); 12 | 13 | // If you want to start measuring performance in your app, pass a function 14 | // to log results (for example: reportWebVitals(console.log)) 15 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 16 | reportWebVitals(); 17 | -------------------------------------------------------------------------------- /src/UseLayoutEffect/LayoutEffectTutorial.js: -------------------------------------------------------------------------------- 1 | import { useLayoutEffect, useEffect, useRef } from "react"; 2 | 3 | function LayoutEffectTutorial() { 4 | const inputRef = useRef(null); 5 | 6 | useLayoutEffect(() => { 7 | console.log(inputRef.current.value); 8 | }, []); 9 | 10 | useEffect(() => { 11 | inputRef.current.value = "HELLO"; 12 | }, []); 13 | 14 | return ( 15 |
16 | 17 |
18 | ); 19 | } 20 | 21 | export default LayoutEffectTutorial; 22 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import "./App.css"; 2 | import EffectTutorial from "./UseEffect/EffectTutorial"; 3 | import ReducerTutorial from "./UseReducer/ReducerTutorial"; 4 | import StateTutorial from "./UseState/StateTutorial"; 5 | import ContextTutorial from "./UseContext/ContextTutorial"; 6 | import ImperativeHandle from "./UseImperativeHandle/ImperativeHandle"; 7 | import RefTutorial from "./UseRef/RefTutorial"; 8 | import MemoTutorial from "./UseMemo/MemoTutorial"; 9 | import CallBackTutorial from "./UseCallback/CallbackTutorial"; 10 | 11 | function App() { 12 | return ( 13 |
14 | 15 |
16 | ); 17 | } 18 | 19 | export default App; 20 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | font-family: Arial, Helvetica, sans-serif; 4 | } 5 | 6 | .App-logo { 7 | height: 40vmin; 8 | pointer-events: none; 9 | } 10 | 11 | @media (prefers-reduced-motion: no-preference) { 12 | .App-logo { 13 | animation: App-logo-spin infinite 20s linear; 14 | } 15 | } 16 | 17 | .App-header { 18 | background-color: #282c34; 19 | min-height: 100vh; 20 | display: flex; 21 | flex-direction: column; 22 | align-items: center; 23 | justify-content: center; 24 | font-size: calc(10px + 2vmin); 25 | color: white; 26 | } 27 | 28 | .App-link { 29 | color: #61dafb; 30 | } 31 | 32 | @keyframes App-logo-spin { 33 | from { 34 | transform: rotate(0deg); 35 | } 36 | to { 37 | transform: rotate(360deg); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/UseCallback/CallbackTutorial.js: -------------------------------------------------------------------------------- 1 | import axios from "axios"; 2 | import { useCallback, useState } from "react"; 3 | import Child from "./Child"; 4 | 5 | export default function CallBackTutorial() { 6 | const [toggle, setToggle] = useState(false); 7 | const [data, setData] = useState("Yo, pls sub to the channel!"); 8 | 9 | const returnComment = useCallback( 10 | (name) => { 11 | return data + name; 12 | }, 13 | [data] 14 | ); 15 | 16 | return ( 17 |
18 | 19 | 20 | 28 | {toggle &&

toggle

} 29 |
30 | ); 31 | } 32 | -------------------------------------------------------------------------------- /src/UseEffect/EffectTutorial.js: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from "react"; 2 | import axios from "axios"; 3 | 4 | function EffectTutorial() { 5 | const [data, setData] = useState(""); 6 | const [count, setCount] = useState(0); 7 | 8 | useEffect(() => { 9 | axios 10 | .get("https://jsonplaceholder.typicode.com/comments") 11 | .then((response) => { 12 | setData(response.data[0].email); 13 | console.log("API WAS CALLED"); 14 | }); 15 | }, []); 16 | 17 | return ( 18 |
19 | Hello World 20 |

{data}

21 |

{count}

22 | 29 |
30 | ); 31 | } 32 | 33 | export default EffectTutorial; 34 | -------------------------------------------------------------------------------- /src/UseReducer/ReducerTutorial.js: -------------------------------------------------------------------------------- 1 | import React, { useReducer } from "react"; 2 | 3 | const reducer = (state, action) => { 4 | switch (action.type) { 5 | case "INCREMENT": 6 | return { count: state.count + 1, showText: state.showText }; 7 | case "toggleShowText": 8 | return { count: state.count, showText: !state.showText }; 9 | default: 10 | return state; 11 | } 12 | }; 13 | 14 | const ReducerTutorial = () => { 15 | const [state, dispatch] = useReducer(reducer, { count: 0, showText: true }); 16 | 17 | return ( 18 |
19 |

{state.count}

20 | 28 | 29 | {state.showText &&

This is a text

} 30 |
31 | ); 32 | }; 33 | 34 | export default ReducerTutorial; 35 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-hooks-course", 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.21.1", 10 | "react": "^17.0.2", 11 | "react-dom": "^17.0.2", 12 | "react-scripts": "4.0.3", 13 | "web-vitals": "^1.0.1" 14 | }, 15 | "scripts": { 16 | "start": "react-scripts start", 17 | "build": "react-scripts build", 18 | "test": "react-scripts test", 19 | "eject": "react-scripts eject" 20 | }, 21 | "eslintConfig": { 22 | "extends": [ 23 | "react-app", 24 | "react-app/jest" 25 | ] 26 | }, 27 | "browserslist": { 28 | "production": [ 29 | ">0.2%", 30 | "not dead", 31 | "not op_mini all" 32 | ], 33 | "development": [ 34 | "last 1 chrome version", 35 | "last 1 firefox version", 36 | "last 1 safari version" 37 | ] 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/UseMemo/MemoTutorial.js: -------------------------------------------------------------------------------- 1 | import axios from "axios"; 2 | import { useEffect, useState, useMemo } from "react"; 3 | 4 | export default function MemoTutorial() { 5 | const [data, setData] = useState(null); 6 | const [toggle, setToggle] = useState(false); 7 | 8 | useEffect(() => { 9 | axios 10 | .get("https://jsonplaceholder.typicode.com/comments") 11 | .then((response) => { 12 | setData(response.data); 13 | }); 14 | }, []); 15 | 16 | const findLongestName = (comments) => { 17 | if (!comments) return null; 18 | 19 | let longestName = ""; 20 | for (let i = 0; i < comments.length; i++) { 21 | let currentName = comments[i].name; 22 | if (currentName.length > longestName.length) { 23 | longestName = currentName; 24 | } 25 | } 26 | 27 | console.log("THIS WAS COMPUTED"); 28 | 29 | return longestName; 30 | }; 31 | 32 | const getLongestName = useMemo(() => findLongestName(data), [toggle]); 33 | 34 | return ( 35 |
36 |
{getLongestName}
37 | 38 | 46 | {toggle &&

toggle

} 47 |
48 | ); 49 | } 50 | -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------