Skip to content

Commit

Permalink
[Devtools] Look for a ReactMemoCacheSentinel on state
Browse files Browse the repository at this point in the history
The polyfill doesn't have access to the fiber, and it simply uses state,
which makes it not work with the devtools.

With this PR, devtools will look for this special sentinel on very first
hook's state for this sentinel and display the Forget badge if present.

The poylfill will add this sentinel to it's state (the cache array).
  • Loading branch information
gsathya committed Apr 15, 2024
1 parent d012a32 commit a1e6f9c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/react-devtools-shared/src/backend/ReactSymbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ export const SUSPENSE_LIST_SYMBOL_STRING = 'Symbol(react.suspense_list)';

export const SERVER_CONTEXT_DEFAULT_VALUE_NOT_LOADED_SYMBOL_STRING =
'Symbol(react.server_context.defaultValue)';

export const REACT_MEMO_CACHE_SENTINEL: symbol = Symbol.for(
'react.memo_cache_sentinel',
);
9 changes: 7 additions & 2 deletions packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import {
STRICT_MODE_SYMBOL_STRING,
PROFILER_NUMBER,
PROFILER_SYMBOL_STRING,
REACT_MEMO_CACHE_SENTINEL,
SCOPE_NUMBER,
SCOPE_SYMBOL_STRING,
FORWARD_REF_NUMBER,
Expand Down Expand Up @@ -474,8 +475,12 @@ export function getInternalReactConstants(version: string): {
}

let resolvedContext: any = null;
// $FlowFixMe[incompatible-type] fiber.updateQueue is mixed
if (!shouldSkipForgetCheck && fiber.updateQueue?.memoCache != null) {
if (
!shouldSkipForgetCheck &&
// $FlowFixMe[incompatible-type] fiber.updateQueue is mixed
(fiber.updateQueue?.memoCache != null ||
fiber.memoizedState?.memoizedState?.[REACT_MEMO_CACHE_SENTINEL])
) {
const displayNameWithoutForgetWrapper = getDisplayNameForFiber(
fiber,
true,
Expand Down

0 comments on commit a1e6f9c

Please sign in to comment.