Skip to content

Commit fa535cd

Browse files
committedDec 2, 2021
react memoisation
0 parents  commit fa535cd

21 files changed

+16575
-0
lines changed
 

‎.gitignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
25+
.vercel

‎README.md

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
https://react-memoisation.vercel.app/
2+
3+
# ReactMemo
4+
5+
React.memo is a higher order component.
6+
7+
If your 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. This means that React will skip rendering the component, and reuse the last rendered result.
8+
9+
React.memo only checks for prop changes. If your function component wrapped in React.memo has a useState or useContext Hook in its implementation, it will still rerender when state or context change.
10+
11+
By default it will only shallowly compare complex objects in the props object. If you want control over the comparison, you can also provide a custom comparison function as the second argument.
12+
13+
```
14+
React.memo( Component, (prevProps,nextProps) => {
15+
return prevProps.propname === nextProps.propname
16+
} )
17+
```
18+
19+
```
20+
const prop = React.memo( () => compHeavyFn(val) , [val] )
21+
function MyComponent(props) {
22+
/* render using props */
23+
}
24+
25+
function areEqual(prevProps, nextProps) {
26+
/*
27+
return true if passing nextProps to render would return
28+
the same result as passing prevProps to render,
29+
otherwise return false
30+
_*/
31+
}
32+
```
33+
34+
```
35+
export default React.memo(MyComponent, areEqual);
36+
This method only exists as a performance optimization. Do not rely on it to “prevent” a render, as this can lead to bugs.
37+
```
38+
39+
`Note:`
40+
41+
```
42+
Unlike the shouldComponentUpdate() method on class components, the areEqual function returns true if the props are equal and false if the props are not equal. This is the inverse from shouldComponentUpdate.
43+
```
44+
45+
## Performance Optimization
46+
47+
## useCallback
48+
49+
```
50+
const fn = useCallback(()=>{
51+
// do something
52+
}, [deps])
53+
```
54+
55+
computation heavy math operations
56+
57+
Returns a memoized callback.
58+
59+
Pass an inline callback and an array of dependencies. useCallback will return a memoized version of the callback that only changes if one of the dependencies has changed. This is useful when passing callbacks to optimized child components that rely on reference equality to prevent unnecessary renders (e.g. shouldComponentUpdate).
60+
61+
## useMemo
62+
63+
```
64+
const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
65+
returns a memoized value.
66+
```
67+
68+
Pass a “create” function and an array of dependencies. useMemo will only recompute the memoized value when one of the dependencies has changed. This optimization helps to avoid expensive calculations on every render.
69+
70+
Remember that the function passed to useMemo runs during rendering. Don’t do anything there that you wouldn’t normally do while rendering. For example, side effects belong in useEffect, not useMemo.
71+
72+
If no array is provided, a new value will be computed on every render.
73+
74+
Note:
75+
76+
## useCallback(fn, deps) is equivalent to useMemo(() => fn, deps).
77+
78+
Other hooks:
79+
80+
## useRef
81+
82+
example
83+
84+
useRef returns a mutable ref object whose .current property is initialized to the passed argument (initialValue). The returned object will persist for the full lifetime of the component.
85+
86+
```
87+
function TextInputWithFocusButton() {
88+
const inputEl = useRef(null);
89+
const onButtonClick = () => {
90+
// `current` points to the mounted text input element
91+
inputEl.current.focus();
92+
};
93+
return (
94+
<>
95+
<input ref={inputEl} type="text" />
96+
<button onClick={onButtonClick}>Focus the input</button>
97+
</>
98+
);
99+
}
100+
```

0 commit comments

Comments
 (0)
Please sign in to comment.