-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotes.txt
More file actions
104 lines (73 loc) · 4.23 KB
/
Notes.txt
File metadata and controls
104 lines (73 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
# React hook = special fxn that allows functional components
to use React features without writing class components (React v16.8)
(useState, useEffect, useContext, useReducer, useCallback, and more...)
# useState() = React hook that allows the creation of a stateful variable
AND a setter function to update its value in the Virtual DOM.
[name, setwame]
# useEffect() = React Hook that tells react to DO SOME CODE WHEN (pick one):
This component Re-renders
This component Mounts
The state of a value changes
SYNTAX:
useEffect(() => {
// This code runs after every render
return () => {
// Cleanup code here
// This code runs on component unmount or before a re-run of effect
};
}, [dependencies]);
useEffect(function, [dependencies])
1. useEffect(() => {func}) // Runs after every Re-render
2. useEffect(() => {func}, []) // Runs only on Mount
3. useEffect(() => {func}, [value]) // Runs on Mount + when value changes
USES:
#1 Event Listeners
#2 DOM Manipulations
#3 Subscriptions (real-time updates)
#4 Fetching Data from an API
#5 Clean up when a component unmounts
# useContext() = React Hook that allows you to share values between multiple levels of components without
passing props through each level (it is used to solve 'prop drilling')
PROVIDER COMPONENT:
1. import { createContext } from 'react';
2. export const MyContext = createContext();
3. <MyContext.Provider value={value}>
<Child />
</MyContext.Provider>
CONSUMER COMPONENT:
1. import { useContext } from 'react';
import { MyContext } from './ComponentA';
2. const value = useContext( MyContext );
# useRef() = "Use Reference" does not cause re-renders when its value change.
When you want some component to "remember" some information,
but you don't want that information to trigger new re-renders.
1. Accessing / interacting with the DOM elements.
2. Handling focus, Animations, and Transitions.
3. Managing Timers and Intervals.
# Updater function = A function passed as an argument to setState() (usually)
for ex. setYear(year + 1) // wont work when used more than once in a function
setYear(y => y + 1) // here y is the previous state of year
small names like 'y' can only be used in updater fxns
Allow for safe updates based on the previous state
Used with multiple state updates and asynchronous functions
Good practice to use updater functions
# Spread operator (...) = used to include all values of the object or array at the place
ex: ...car
# Object Destrcturing = Allows you to extract properties from objects and bind them to
variables in a more concise and readable way.
ex:
const { firstName, lastName, age, city } = person;
const { firstName: fName, lastName: lName } = person; ---> Renaming variables
const { firstName, lastName, age, city, country = 'USA' } = person; ---> Default values
const { name, position, company: { name: companyName, location } } = employee; ---> Destructuring nested objects
REACT SNIPPETS :
imrse - import useState with useEffect
rafce - React arrow function export component
rafcp - React arrow function export component with prototypes
cc - class component
ccc - class component with constructor
imrr - import browser router with route and navlink
imbrc - import router setup
uef - useEffect Hook
ucb - useCallback Hook
usf - useState Hook