1
+
2
+## Input
3
+
4
+```javascript
5
+// @flow @validatePreserveExistingMemoizationGuarantees
6
+import {useFragment} from 'react-relay';
7
+import LogEvent from 'LogEvent';
8
+import {useCallback, useMemo} from 'react';
9
+
10
+component Component(id) {
11
+ const {data} = useFragment();
12
+ const items = data.items.edges;
13
+
14
+ const [prevId, setPrevId] = useState(id);
15
+ const [index, setIndex] = useState(0);
16
+
17
+ const logData = useMemo(() => {
18
+ const item = items[index];
19
+ return {
20
+ key: item.key ?? '',
21
+ };
22
+ }, [index, items]);
23
+
24
+ const setCurrentIndex = useCallback(
25
+ (index: number) => {
26
+ const object = {
27
+ tracking: logData.key,
28
+ };
29
+ // We infer that this may mutate `object`, which in turn aliases
30
+ // data from `logData`, such that `logData` may be mutated.
31
+ LogEvent.log(() => object);
32
+ setIndex(index);
33
+ },
34
+ [index, logData, items]
35
+ );
36
+
37
+ if (prevId !== id) {
38
+ setPrevId(id);
39
+ setCurrentIndex(0);
40
+ }
41
+
42
+ return (
43
+ <Foo
44
+ index={index}
45
+ items={items}
46
+ current={mediaList[index]}
47
+ setCurrentIndex={setCurrentIndex}
48
+ />
49
+ );
50
+}
51
+
52
+```
53
+
54
+
55
+## Error
56
+
57
+```
58
+ 19 |
59
+ 20 | const setCurrentIndex = useCallback(
60
+> 21 | (index: number) => {
61
+ | ^^^^^^^^^^^^^^^^^^^^
62
+> 22 | const object = {
63
+ | ^^^^^^^^^^^^^^^^^^^^^^
64
+> 23 | tracking: logData.key,
65
+ | ^^^^^^^^^^^^^^^^^^^^^^
66
+> 24 | };
67
+ | ^^^^^^^^^^^^^^^^^^^^^^
68
+> 25 | // We infer that this may mutate `object`, which in turn aliases
69
+ | ^^^^^^^^^^^^^^^^^^^^^^
70
+> 26 | // data from `logData`, such that `logData` may be mutated.
71
+ | ^^^^^^^^^^^^^^^^^^^^^^
72
+> 27 | LogEvent.log(() => object);
73
+ | ^^^^^^^^^^^^^^^^^^^^^^
74
+> 28 | setIndex(index);
75
+ | ^^^^^^^^^^^^^^^^^^^^^^
76
+> 29 | },
77
+ | ^^^^^^ CannotPreserveMemoization: React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. This value was memoized in source but not in compilation output. (21:29)
78
+ 30 | [index, logData, items]
79
+ 31 | );
80
+ 32 |
81
+```
82
+
83
+
\ No newline at end of file