Skip to content

Commit 34842c7

Browse files
author
Brian Vaughn
committed
Added rudimentary support for Cache to DevTools
1 parent 50393dc commit 34842c7

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

packages/react-devtools-shared/src/__tests__/__snapshots__/store-test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,4 +685,5 @@ exports[`Store should show the right display names for special component types 1
685685
<Baz> [withFoo][withBar]
686686
<Baz> [Memo][withFoo][withBar]
687687
<Baz> [ForwardRef][withFoo][withBar]
688+
<Cache>
688689
`;

packages/react-devtools-shared/src/__tests__/store-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,7 @@ describe('Store', () => {
888888
<FakeHigherOrderComponent />
889889
<MemoizedFakeHigherOrderComponent />
890890
<ForwardRefFakeHigherOrderComponent />
891+
<React.unstable_Cache />
891892
</React.Fragment>
892893
);
893894

packages/react-devtools-shared/src/backend/renderer.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export function getInternalReactConstants(
176176
// Currently the version in Git is 17.0.2 (but that version has not been/may not end up being released).
177177
if (gt(version, '17.0.1')) {
178178
ReactTypeOfWork = {
179+
CacheComponent: 24, // Experimental
179180
ClassComponent: 1,
180181
ContextConsumer: 9,
181182
ContextProvider: 10,
@@ -205,6 +206,7 @@ export function getInternalReactConstants(
205206
};
206207
} else if (gte(version, '17.0.0-alpha')) {
207208
ReactTypeOfWork = {
209+
CacheComponent: -1, // Doens't exist yet
208210
ClassComponent: 1,
209211
ContextConsumer: 9,
210212
ContextProvider: 10,
@@ -234,6 +236,7 @@ export function getInternalReactConstants(
234236
};
235237
} else if (gte(version, '16.6.0-beta.0')) {
236238
ReactTypeOfWork = {
239+
CacheComponent: -1, // Doens't exist yet
237240
ClassComponent: 1,
238241
ContextConsumer: 9,
239242
ContextProvider: 10,
@@ -263,6 +266,7 @@ export function getInternalReactConstants(
263266
};
264267
} else if (gte(version, '16.4.3-alpha')) {
265268
ReactTypeOfWork = {
269+
CacheComponent: -1, // Doens't exist yet
266270
ClassComponent: 2,
267271
ContextConsumer: 11,
268272
ContextProvider: 12,
@@ -292,6 +296,7 @@ export function getInternalReactConstants(
292296
};
293297
} else {
294298
ReactTypeOfWork = {
299+
CacheComponent: -1, // Doens't exist yet
295300
ClassComponent: 2,
296301
ContextConsumer: 12,
297302
ContextProvider: 13,
@@ -335,6 +340,7 @@ export function getInternalReactConstants(
335340
}
336341

337342
const {
343+
CacheComponent,
338344
ClassComponent,
339345
IncompleteClassComponent,
340346
FunctionComponent,
@@ -382,6 +388,8 @@ export function getInternalReactConstants(
382388
let resolvedContext: any = null;
383389

384390
switch (tag) {
391+
case CacheComponent:
392+
return 'Cache';
385393
case ClassComponent:
386394
case IncompleteClassComponent:
387395
return getDisplayName(resolvedType);
@@ -486,12 +494,13 @@ export function attach(
486494
} = getInternalReactConstants(renderer.version);
487495
const {Incomplete, NoFlags, PerformedWork, Placement} = ReactTypeOfSideEffect;
488496
const {
489-
FunctionComponent,
497+
CacheComponent,
490498
ClassComponent,
491499
ContextConsumer,
492500
DehydratedSuspenseComponent,
493-
Fragment,
494501
ForwardRef,
502+
Fragment,
503+
FunctionComponent,
495504
HostRoot,
496505
HostPortal,
497506
HostComponent,
@@ -2518,6 +2527,10 @@ export function attach(
25182527
tag === ForwardRef) &&
25192528
(!!memoizedState || !!dependencies);
25202529

2530+
// TODO Show custom UI for Cache like we do for Suspense
2531+
// For now, just hide state data entirely since it's not meant to be inspected.
2532+
const showState = !usesHooks && tag !== CacheComponent;
2533+
25212534
const typeSymbol = getTypeSymbol(type);
25222535

25232536
let canViewSource = false;
@@ -2687,7 +2700,7 @@ export function attach(
26872700
context,
26882701
hooks,
26892702
props: memoizedProps,
2690-
state: usesHooks ? null : memoizedState,
2703+
state: showState ? memoizedState : null,
26912704
errors: Array.from(errors.entries()),
26922705
warnings: Array.from(warnings.entries()),
26932706

packages/react-devtools-shared/src/backend/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type WorkFlags = number;
2626
export type ExpirationTime = number;
2727

2828
export type WorkTagMap = {|
29+
CacheComponent: WorkTag,
2930
ClassComponent: WorkTag,
3031
ContextConsumer: WorkTag,
3132
ContextProvider: WorkTag,

packages/react-devtools-shell/src/app/ElementTypes/index.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
Profiler,
2020
StrictMode,
2121
Suspense,
22+
unstable_Cache as Cache,
2223
} from 'react';
2324

2425
const Context = createContext('abc');
@@ -61,15 +62,17 @@ export default function ElementTypes() {
6162
<Context.Consumer>{value => null}</Context.Consumer>
6263
</Context.Provider>
6364
<StrictMode>
64-
<Suspense fallback={<div>Loading...</div>}>
65-
<ClassComponent />
66-
<FunctionComponent />
67-
<MemoFunctionComponent />
68-
<ForwardRefComponent />
69-
<ForwardRefComponentWithAnonymousFunction />
70-
<ForwardRefComponentWithCustomDisplayName />
71-
<LazyComponent />
72-
</Suspense>
65+
<Cache>
66+
<Suspense fallback={<div>Loading...</div>}>
67+
<ClassComponent />
68+
<FunctionComponent />
69+
<MemoFunctionComponent />
70+
<ForwardRefComponent />
71+
<ForwardRefComponentWithAnonymousFunction />
72+
<ForwardRefComponentWithCustomDisplayName />
73+
<LazyComponent />
74+
</Suspense>
75+
</Cache>
7376
</StrictMode>
7477
</Fragment>
7578
</Profiler>

0 commit comments

Comments
 (0)