Skip to content

Commit 45fa7cf

Browse files
Merge pull request #2692 from preactjs/relay-support
Add React "secret or fired" shim for react-relay
2 parents 7eb87b7 + 1052866 commit 45fa7cf

File tree

8 files changed

+68
-4
lines changed

8 files changed

+68
-4
lines changed

compat/mangle.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"properties": {
99
"regex": "^_[^_]",
1010
"reserved": [
11+
"__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED",
1112
"__REACT_DEVTOOLS_GLOBAL_HOOK__",
1213
"__PREACT_DEVTOOLS__",
1314
"_renderers",

compat/src/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ import { Children } from './Children';
2626
import { Suspense, lazy } from './suspense';
2727
import { SuspenseList } from './suspense-list';
2828
import { createPortal } from './portals';
29-
import { hydrate, render, REACT_ELEMENT_TYPE } from './render';
29+
import {
30+
hydrate,
31+
render,
32+
REACT_ELEMENT_TYPE,
33+
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
34+
} from './render';
3035

3136
const version = '16.8.0'; // trick libraries to think we are react
3237

@@ -126,7 +131,8 @@ export {
126131
StrictMode,
127132
Suspense,
128133
SuspenseList,
129-
lazy
134+
lazy,
135+
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
130136
};
131137

132138
// React copies the named exports to the default one.
@@ -163,5 +169,6 @@ export default {
163169
StrictMode,
164170
Suspense,
165171
SuspenseList,
166-
lazy
172+
lazy,
173+
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
167174
};

compat/src/render.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,27 @@ options.vnode = vnode => {
173173

174174
if (oldVNodeHook) oldVNodeHook(vnode);
175175
};
176+
177+
// Only needed for react-relay
178+
let currentComponent;
179+
const oldBeforeRender = options._render;
180+
options._render = function(vnode) {
181+
if (oldBeforeRender) {
182+
oldBeforeRender(vnode);
183+
}
184+
currentComponent = vnode._component;
185+
};
186+
187+
// This is a very very private internal function for React it
188+
// is used to sort-of do runtime dependency injection. So far
189+
// only `react-relay` makes use of it. It uses it to read the
190+
// context value.
191+
export const __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = {
192+
ReactCurrentDispatcher: {
193+
current: {
194+
readContext(context) {
195+
return currentComponent._globalContext[context._id].props.value;
196+
}
197+
}
198+
}
199+
};

compat/test/browser/render.test.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import React, {
22
createElement,
33
render,
44
Component,
5-
hydrate
5+
hydrate,
6+
createContext
67
} from 'preact/compat';
78
import { setupRerender, act } from 'preact/test-utils';
89
import {
@@ -312,4 +313,31 @@ describe('compat render', () => {
312313
expect(mountSpy).to.be.calledOnce;
313314
expect(updateSpy).to.not.be.calledOnce;
314315
});
316+
317+
it("should support react-relay's usage of __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED", () => {
318+
const Ctx = createContext('foo');
319+
320+
// Simplified version of: https://github.com/facebook/relay/blob/fba79309977bf6b356ee77a5421ca5e6f306223b/packages/react-relay/readContext.js#L17-L28
321+
function readContext(Context) {
322+
const {
323+
ReactCurrentDispatcher
324+
} = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
325+
const dispatcher = ReactCurrentDispatcher.current;
326+
return dispatcher.readContext(Context);
327+
}
328+
329+
function Foo() {
330+
const value = readContext(Ctx);
331+
return <div>{value}</div>;
332+
}
333+
334+
React.render(
335+
<Ctx.Provider value="foo">
336+
<Foo />
337+
</Ctx.Provider>,
338+
scratch
339+
);
340+
341+
expect(scratch.textContent).to.equal('foo');
342+
});
315343
});

debug/mangle.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"properties": {
99
"regex": "^_[^_]",
1010
"reserved": [
11+
"__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED",
1112
"__REACT_DEVTOOLS_GLOBAL_HOOK__",
1213
"__PREACT_DEVTOOLS__",
1314
"_renderers",

devtools/mangle.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"properties": {
99
"regex": "^_[^_]",
1010
"reserved": [
11+
"__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED",
1112
"__REACT_DEVTOOLS_GLOBAL_HOOK__",
1213
"__PREACT_DEVTOOLS__",
1314
"_renderers",

hooks/mangle.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"properties": {
99
"regex": "^_[^_]",
1010
"reserved": [
11+
"__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED",
1112
"__REACT_DEVTOOLS_GLOBAL_HOOK__",
1213
"__PREACT_DEVTOOLS__",
1314
"_renderers",

mangle.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"properties": {
99
"regex": "^_[^_]",
1010
"reserved": [
11+
"__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED",
1112
"__REACT_DEVTOOLS_GLOBAL_HOOK__",
1213
"__PREACT_DEVTOOLS__",
1314
"_renderers",

0 commit comments

Comments
 (0)