Skip to content

Commit

Permalink
Use use() in the Cache if available (#28793)
Browse files Browse the repository at this point in the history
This is a follow up to
#28789 (comment)

Revert to use the old readContext detection if not to support older
React.

I haven't actually tested this. Just opening as a suggestion.
  • Loading branch information
sebmarkbage authored Apr 15, 2024
1 parent 9defcd5 commit 1683cb1
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions packages/react-devtools-shared/src/devtools/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<null>) {
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<null>) {
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<null>) {
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);
Expand Down

0 comments on commit 1683cb1

Please sign in to comment.