Skip to content

Commit 1683cb1

Browse files
authored
Use use() in the Cache if available (#28793)
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.
1 parent 9defcd5 commit 1683cb1

File tree

1 file changed

+24
-13
lines changed
  • packages/react-devtools-shared/src/devtools

1 file changed

+24
-13
lines changed

packages/react-devtools-shared/src/devtools/cache.js

+24-13
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,30 @@ const Pending = 0;
5959
const Resolved = 1;
6060
const Rejected = 2;
6161

62-
const ReactSharedInternals = (React: any)
63-
.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE;
64-
65-
function readContext(Context: ReactContext<null>) {
66-
const dispatcher = ReactSharedInternals.H;
67-
if (dispatcher === null) {
68-
throw new Error(
69-
'react-cache: read and preload may only be called from within a ' +
70-
"component's render. They are not supported in event handlers or " +
71-
'lifecycle methods.',
72-
);
73-
}
74-
return dispatcher.readContext(Context);
62+
let readContext;
63+
if (typeof React.use === 'function') {
64+
readContext = function (Context: ReactContext<null>) {
65+
return React.use(Context);
66+
};
67+
} else if (
68+
typeof (React: any).__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED ===
69+
'object'
70+
) {
71+
const ReactCurrentDispatcher = (React: any)
72+
.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentDispatcher;
73+
readContext = function (Context: ReactContext<null>) {
74+
const dispatcher = ReactCurrentDispatcher.current;
75+
if (dispatcher === null) {
76+
throw new Error(
77+
'react-cache: read and preload may only be called from within a ' +
78+
"component's render. They are not supported in event handlers or " +
79+
'lifecycle methods.',
80+
);
81+
}
82+
return dispatcher.readContext(Context);
83+
};
84+
} else {
85+
throw new Error('react-cache: Unsupported React version');
7586
}
7687

7788
const CacheContext = createContext(null);

0 commit comments

Comments
 (0)