From 147adf26e38291a388e0437382101d1920bf2072 Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Tue, 9 Apr 2024 12:32:12 -0400 Subject: [PATCH] Use use() in the Cache if available Revert to use the old readContext detection if not to support older React. --- .../src/devtools/cache.js | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/packages/react-devtools-shared/src/devtools/cache.js b/packages/react-devtools-shared/src/devtools/cache.js index 7d0cb7af03311..eda30ed56ae36 100644 --- a/packages/react-devtools-shared/src/devtools/cache.js +++ b/packages/react-devtools-shared/src/devtools/cache.js @@ -59,19 +59,30 @@ const Pending = 0; const Resolved = 1; const Rejected = 2; -const ReactSharedInternals = (React: any) - .__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE; - -function readContext(Context: ReactContext) { - const dispatcher = ReactSharedInternals.H; - if (dispatcher === null) { - throw new Error( - 'react-cache: read and preload may only be called from within a ' + - "component's render. They are not supported in event handlers or " + - 'lifecycle methods.', - ); - } - return dispatcher.readContext(Context); +let readContext; +if (typeof React.use === 'function') { + readContext = function (Context: ReactContext) { + return React.use(Context); + }; +} else if ( + typeof (React: any).__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED === + 'object' +) { + const ReactCurrentDispatcher = (React: any) + .__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentDispatcher; + readContext = function (Context: ReactContext) { + const dispatcher = ReactCurrentDispatcher.current; + if (dispatcher === null) { + throw new Error( + 'react-cache: read and preload may only be called from within a ' + + "component's render. They are not supported in event handlers or " + + 'lifecycle methods.', + ); + } + return dispatcher.readContext(Context); + }; +} else { + throw new Error('react-cache: Unsupported React version'); } const CacheContext = createContext(null);