-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUseMemo.tsx
55 lines (46 loc) · 1.58 KB
/
UseMemo.tsx
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
import { useMemo, useState } from 'react';
import { SubTitle } from '../app/components/Headline';
/*
const syncWait = (ms: number = 1000) => {
const end = Date.now() + ms;
while (Date.now() < end) continue;
};
*/
// Keeps track of all created values during the app's life
const values: Set<number> = new Set();
export default function UseMemoHook() {
const [counter, setCounter] = useState<number>(0);
const increment = () => setCounter((prevCounter: number) => prevCounter + 1);
// The value inside the "dependency list" never changes (is the same), so no need
// to re-run the "expensive" function. React uses the memorized result (store in the
// variable `memorizedResult`).
const memorizedResult = useMemo<number>(() => {
console.log('[UseMemo] Calculate the expensive value (1)');
const result = 1 + 1;
return result;
}, []);
// The value inside the "dependency list" changes on very click, so we've to
// re-run the "expensive" function. React calculates a new memorized result and stores
// it nn the variable `memorizedResult2`).
const memorizedResult2 = useMemo<number>(() => {
console.log('[UseMemo] Calculate the expensive value (2)');
//syncWait();
const result = counter + 1;
return result;
}, [counter]);
// Register the values so we can count them
values.add(memorizedResult);
values.add(memorizedResult2);
return (
<>
<SubTitle>useMemo</SubTitle>
<div>Counter is {counter}</div>
<br />
<div>
<button onClick={increment}>Increment UseMemo-Counter</button>
</div>
<br />
<div>Newly created values: {values.size} </div>
</>
);
}