Skip to content

Commit 22ea9b3

Browse files
author
Brian Vaughn
committed
Added rudimentary support for Cache to DevTools
1 parent 0581bdf commit 22ea9b3

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
@@ -886,6 +886,7 @@ describe('Store', () => {
886886
<FakeHigherOrderComponent />
887887
<MemoizedFakeHigherOrderComponent />
888888
<ForwardRefFakeHigherOrderComponent />
889+
<React.unstable_Cache />
889890
</React.Fragment>
890891
);
891892

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ export function getInternalReactConstants(
171171
// Currently the version in Git is 17.0.2 (but that version has not been/may not end up being released).
172172
if (gt(version, '17.0.1')) {
173173
ReactTypeOfWork = {
174+
CacheComponent: 24, // Experimental
174175
ClassComponent: 1,
175176
ContextConsumer: 9,
176177
ContextProvider: 10,
@@ -200,6 +201,7 @@ export function getInternalReactConstants(
200201
};
201202
} else if (gte(version, '17.0.0-alpha')) {
202203
ReactTypeOfWork = {
204+
CacheComponent: -1, // Doens't exist yet
203205
ClassComponent: 1,
204206
ContextConsumer: 9,
205207
ContextProvider: 10,
@@ -229,6 +231,7 @@ export function getInternalReactConstants(
229231
};
230232
} else if (gte(version, '16.6.0-beta.0')) {
231233
ReactTypeOfWork = {
234+
CacheComponent: -1, // Doens't exist yet
232235
ClassComponent: 1,
233236
ContextConsumer: 9,
234237
ContextProvider: 10,
@@ -258,6 +261,7 @@ export function getInternalReactConstants(
258261
};
259262
} else if (gte(version, '16.4.3-alpha')) {
260263
ReactTypeOfWork = {
264+
CacheComponent: -1, // Doens't exist yet
261265
ClassComponent: 2,
262266
ContextConsumer: 11,
263267
ContextProvider: 12,
@@ -287,6 +291,7 @@ export function getInternalReactConstants(
287291
};
288292
} else {
289293
ReactTypeOfWork = {
294+
CacheComponent: -1, // Doens't exist yet
290295
ClassComponent: 2,
291296
ContextConsumer: 12,
292297
ContextProvider: 13,
@@ -330,6 +335,7 @@ export function getInternalReactConstants(
330335
}
331336

332337
const {
338+
CacheComponent,
333339
ClassComponent,
334340
IncompleteClassComponent,
335341
FunctionComponent,
@@ -377,6 +383,8 @@ export function getInternalReactConstants(
377383
let resolvedContext: any = null;
378384

379385
switch (tag) {
386+
case CacheComponent:
387+
return 'Cache';
380388
case ClassComponent:
381389
case IncompleteClassComponent:
382390
return getDisplayName(resolvedType);
@@ -481,12 +489,13 @@ export function attach(
481489
} = getInternalReactConstants(renderer.version);
482490
const {NoFlags, PerformedWork, Placement} = ReactTypeOfSideEffect;
483491
const {
484-
FunctionComponent,
492+
CacheComponent,
485493
ClassComponent,
486494
ContextConsumer,
487495
DehydratedSuspenseComponent,
488-
Fragment,
489496
ForwardRef,
497+
Fragment,
498+
FunctionComponent,
490499
HostRoot,
491500
HostPortal,
492501
HostComponent,
@@ -2330,6 +2339,10 @@ export function attach(
23302339
tag === ForwardRef) &&
23312340
(!!memoizedState || !!dependencies);
23322341

2342+
// TODO Show custom UI for Cache like we do for Suspense
2343+
// For now, just hide state data entirely since it's not meant to be inspected.
2344+
const showState = !usesHooks && tag !== CacheComponent;
2345+
23332346
const typeSymbol = getTypeSymbol(type);
23342347

23352348
let canViewSource = false;
@@ -2496,7 +2509,7 @@ export function attach(
24962509
context,
24972510
hooks,
24982511
props: memoizedProps,
2499-
state: usesHooks ? null : memoizedState,
2512+
state: showState ? memoizedState : null,
25002513

25012514
// List of owners
25022515
owners,

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)