@samitouri / QOS-React / commits / 1683cb186c

Use use() in the Cache if available (#28793)

This is a follow up to https://github.com/facebook/react/pull/28789#discussion_r1557232202 Revert to use the old readContext detection if not to support older React. I haven't actually tested this. Just opening as a suggestion.

Sebastian Markbåge committed Apr 15, 2024 at 08:03 UTC 1683cb186c17dd610ecf83675aded1348a9bb3ff
1 file changed +24 -13
packages/react-devtools-shared/src/devtools/cache.js
+24 -13
@@ -59,19 +59,30 @@ const Pending = 0;
59 const Resolved = 1;
60 const Rejected = 2;
61
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');
86 }
87
88 const CacheContext = createContext(null);