Skip to content

Commit f87f9ee

Browse files
committed
notes
1 parent 48045f6 commit f87f9ee

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

React.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
1. react component life cycle:
2+
class based components: https://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/
3+
a. constructor ::
4+
b. render
5+
c. componentDidMount
6+
d. setState/forceUpdate/props change
7+
e. shouldComponentUpdate
8+
f. componentDidUpdate
9+
g. componentWillUnmount
10+
function based components: Hooks : https://reactjs.org/docs/hooks-overview.html
11+
a. function body ::: constructor+render body
12+
b. useState ::: setState/forceUpdate
13+
b. useReducer ::: setState/forceUpdate + complexicities
14+
c. useEffect ::: componentDidUpdate(empty array passed)
15+
componentWillUnmount(call-back returns function)
16+
componentDidMount(no array passed)
17+
https://reactjs.org/docs/hooks-effect.html
18+
d. useRef ::: class instance's attribute/ createRef API
19+
e. useMemo ::: avoid re-computation
20+
21+
2. react API
22+
a. memo API: to avoid re-rendering
23+
b. context API: to avoid repetative props passing
24+
c. ref API: to controll DOM elements/ class based components
25+
d. lazy + suspend :

React_details.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
## General:
2+
1. React ensures that for each user-initiated event like a click or a keypress, the DOM is fully updated before the next event.
3+
4+
## Hooks:
5+
1. 2 rules: https://reactjs.org/docs/hooks-rules.html
6+
same order, and without any conditions order.
7+
otherwise: we get error, as order in which hooks are renderd is remembered each time to identify and execute them.
8+
Error:
9+
Rendered more hooks than during the previous render.
10+
2. Working of hooks:
11+
Each call to a Hook gets an independant state.(unlike class component, which has its own state)
12+
We can call hooks many times inside any function component body
13+
on each re render, hook is identified and maped by the order in which it is created,
14+
ex. ``` [a,setA] = useState(100)
15+
[b,setB] = useState(100)```
16+
hook creates 2 states with a = 100, b=100, identified by the order alone
17+
on a re-render, this a,b are going to get modified value, based on just the order in which hooks apear, if conditionally this order changes, i.e. b apears b4 a, b will get the value from state that was created by a & vice versa !
18+
3. Custom hooks: https://reactjs.org/docs/hooks-custom.html
19+
Custom Hooks are a mechanism to reuse stateful logic (such as setting up a subscription and remembering the current value)
20+
- its just a simple JS function which uses Hooks,
21+
- but can be called Only inside any functional component ,
22+
- from React’s point of view our component just calls useState and useEffect indirectly through a function.
23+
4. Automatic Batching of state updates to reduce re-renders:
24+
https://github.com/reactwg/react-18/discussions/21
25+
26+
## Hook: useReducer : https://reactjs.org/docs/hooks-reference.html#usereducer
27+
- useReducer is usually preferable over useState when you have complex state logic, noth are SIMILER
28+
- const [state, dispatch] = useReducer(reducer, initialArg, init);
29+
- state: state, re renders when change...
30+
- dispatch: call it `dispatch("ACTION1")` function that can call reducer to change state with different actions differently
31+
- reducer:
32+
function reducer(state, action) {
33+
switch (action.type) {
34+
case 'increment':
35+
return {...state, change1};
36+
case 'decrement':
37+
return {...state, change2};
38+
default:
39+
throw new Error();
40+
}
41+
}
42+
- init: funtion, The initial state will be set to init(initialArg)
43+
44+
## Hook: useMemo:
45+
- to avoid re-computing computationally high function, when its dependancies change
46+
- const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
47+
OR
48+
- const memoizedValue = useCallback(() => computeExpensiveValue(a, b), [a, b]);
49+
- when memoizedValue(a,b) is called, if a,b==initial a,b , it wont run again
50+
51+
## Context API: https://reactjs.org/docs/hooks-reference.html#usecontext
52+
- `const ThemeContext = React.createContext();` in App's body
53+
- export it and import it apropriately, to be used like a component
54+
- App returns:
55+
<ThemeContext.Provider value={{"any data":"to be passed down"}}>
56+
<GrandParent />
57+
</ThemeContext.Provider>
58+
- access `value` in any successor element of GrandParent as:
59+
const theme = useContext(ThemeContext);
60+
61+
62+
## Memo API
63+
0. let MemoComponent = React.memo(MyComponent, areEqual);
64+
areEqual gives true: will NOT re render
65+
1. for class components: https://codesandbox.io/s/memo-roj4si?file=/src/App.js
66+
API creates the additional wraper component aroundd the actual rendered component, so mostly we try to use shouldComponentUpdate instead.
67+
2. If your function component renders the same result given the same props, you can wrap it in a call to React. memo for a performance boost in some cases by memoizing the result.
68+
3. React.memo only checks for prop changes shallow comparison by default.
69+
4. custom comparison function can be passed as 2nd arg.: function areEqual(prevProps, nextProps) {..
70+
71+
## Ref API:
72+
#### not recomended to replace re-rendering based aproach to "make things hapen", only cases where we need css/dom animations, styling changes... & rare cases
73+
0. this.textInput = React.createRef(); // in constructor/body of fn based component
74+
pass attribute : `<someElement ref={this.textInput} />`
75+
and access someElement as: this.textInput.current
76+
1. provides referance to DOM elements OR mounted instance of class based child elements
77+
- for a functional child component, we can not controll it's instances on DOM via ref
78+
(as there are no "instances" for it, DOM has only what it returns, not any JS object as instance)
79+
2. parent element can call DOM APIs over a DOM elements, to make changes like animations or effects like focus...
80+
3. parent can controll mounted class based element via methods decleared inside that class, ( how we do in OOPS based methodology...)
81+
4. Forward ref:
82+
- Parent -> Child -> GrandChild
83+
if ref passed to Child is forwarded to GrandChild, it can be controlled/accessed by parent
84+
5. Other USE: https://codesandbox.io/s/class-vs-function-component-1bwlwb?file=/src/App.js
85+
to store data to functional element that is not affected by re-rendering, and dosnt triger re-rendering(as changing state causes re-rendering, as well as other let/var devleared inside get re-set to initial values on every re-render), similer to class elements class attributes
86+
87+
88+
## React.lazy() API
89+
0 lazy takes inn the promice, that returns React element, that renders after promice is fulfilled
90+
1. const SomeComponent = React.lazy(() => import('./SomeComponent'));
91+
2. return(
92+
<React.Suspense fallback={<h4>loading...</h4>}>
93+
<SomeComponent />
94+
</React.Suspense>
95+
)
96+
3. USE:
97+
in client side rendering: to achive code splitting, i.e. reduce bundle size that is passed over network, and achieve "lazy loading"
98+
99+

0 commit comments

Comments
 (0)