Skip to content

Commit a5c6ea1

Browse files
committed
fix[devtools/useMemoCache]: useMemoCache as a non-inspectable stub for DevTools
1 parent 16619f1 commit a5c6ea1

File tree

4 files changed

+18
-47
lines changed

4 files changed

+18
-47
lines changed

packages/react-debug-tools/src/ReactDebugHooks.js

+7-41
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,9 @@ type Dispatch<A> = A => void;
4848

4949
let primitiveStackCache: null | Map<string, Array<any>> = null;
5050

51-
type MemoCache = {
52-
data: Array<Array<any>>,
53-
index: number,
54-
};
55-
56-
type FunctionComponentUpdateQueue = {
57-
memoCache?: MemoCache | null,
58-
};
59-
6051
type Hook = {
6152
memoizedState: any,
6253
next: Hook | null,
63-
updateQueue: FunctionComponentUpdateQueue | null,
6454
};
6555

6656
function getPrimitiveStackCache(): Map<string, Array<any>> {
@@ -327,36 +317,12 @@ function useId(): string {
327317
return id;
328318
}
329319

320+
// useMemoCache is an implementation detail of Forget's memoization
321+
// it should not be called directly in user-generated code
322+
// we keep it as a stub for dispatcher
330323
function useMemoCache(size: number): Array<any> {
331-
const hook = nextHook();
332-
let memoCache: MemoCache;
333-
if (
334-
hook !== null &&
335-
hook.updateQueue !== null &&
336-
hook.updateQueue.memoCache != null
337-
) {
338-
memoCache = hook.updateQueue.memoCache;
339-
} else {
340-
memoCache = {
341-
data: [],
342-
index: 0,
343-
};
344-
}
345-
346-
let data = memoCache.data[memoCache.index];
347-
if (data === undefined) {
348-
const MEMO_CACHE_SENTINEL = Symbol.for('react.memo_cache_sentinel');
349-
data = new Array(size);
350-
for (let i = 0; i < size; i++) {
351-
data[i] = MEMO_CACHE_SENTINEL;
352-
}
353-
}
354-
hookLog.push({
355-
primitive: 'MemoCache',
356-
stackError: new Error(),
357-
value: data,
358-
});
359-
return data;
324+
nextHook();
325+
return [];
360326
}
361327

362328
const Dispatcher: DispatcherType = {
@@ -725,7 +691,7 @@ export function inspectHooks<Props>(
725691
renderFunction: Props => React$Node,
726692
props: Props,
727693
currentDispatcher: ?CurrentDispatcherRef,
728-
includeHooksSource?: boolean = false,
694+
includeHooksSource: boolean = false,
729695
): HooksTree {
730696
// DevTools will pass the current renderer's injected dispatcher.
731697
// Other apps might compile debug hooks as part of their app though.
@@ -816,7 +782,7 @@ function resolveDefaultProps(Component: any, baseProps: any) {
816782
export function inspectHooksOfFiber(
817783
fiber: Fiber,
818784
currentDispatcher: ?CurrentDispatcherRef,
819-
includeHooksSource?: boolean = false,
785+
includeHooksSource: boolean = false,
820786
): HooksTree {
821787
// DevTools will pass the current renderer's injected dispatcher.
822788
// Other apps might compile debug hooks as part of their app though.

packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ describe('ReactHooksInspectionIntegration', () => {
636636
});
637637

638638
// @gate enableUseMemoCacheHook
639-
it('should support useMemoCache hook', () => {
639+
it('useMemoCache should not be inspectable', () => {
640640
function Foo() {
641641
const $ = useMemoCache(1);
642642
let t0;
@@ -655,11 +655,7 @@ describe('ReactHooksInspectionIntegration', () => {
655655
const childFiber = renderer.root.findByType(Foo)._currentFiber();
656656
const tree = ReactDebugTools.inspectHooksOfFiber(childFiber);
657657

658-
expect(tree.length).toEqual(1);
659-
expect(tree[0].isStateEditable).toBe(false);
660-
expect(tree[0].name).toBe('MemoCache');
661-
expect(tree[0].value).toHaveLength(1);
662-
expect(tree[0].value[0]).toEqual(<div>{1}</div>);
658+
expect(tree.length).toEqual(0);
663659
});
664660

665661
describe('useDebugValue', () => {

packages/react-devtools-shell/src/app/InspectableElements/InspectableElements.js

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import EdgeCaseObjects from './EdgeCaseObjects.js';
1818
import NestedProps from './NestedProps';
1919
import SimpleValues from './SimpleValues';
2020
import SymbolKeys from './SymbolKeys';
21+
import UseMemoCache from './UseMemoCache';
2122

2223
// TODO Add Immutable JS example
2324

@@ -34,6 +35,7 @@ export default function InspectableElements(): React.Node {
3435
<EdgeCaseObjects />
3536
<CircularReferences />
3637
<SymbolKeys />
38+
<UseMemoCache />
3739
</Fragment>
3840
);
3941
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as React from 'react';
2+
3+
export default function UseMemoCache(): React.Node {
4+
React.unstable_useMemoCache(1);
5+
6+
return null;
7+
}

0 commit comments

Comments
 (0)