Skip to content

Commit debd0b9

Browse files
committed
Adapt preload for the React v16.6 context API changes.
Fixes jaydenseric/graphql-react#11.
1 parent b8095e5 commit debd0b9

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Patch
66

77
- Updated dependencies.
8+
- Adapt `preload` for the React v16.6 context API changes, fixing [#11](https://github.com/jaydenseric/graphql-react/issues/11).
89

910
## 3.0.0
1011

src/preload.mjs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/**
2+
* Whether or not the runtime environment supports Symbol.for. Although all
3+
* supported versions of Node.js (see package engines field) support Symbol,
4+
* the preload function may be also be used in the browser.
5+
* @kind constant
6+
* @name hasSymbol
7+
* @type {boolean}
8+
* @see [React source](https://github.com/facebook/react/blob/v16.6.0/packages/shared/ReactSymbols.js#L12).
9+
* @ignore
10+
*/
11+
const hasSymbol = typeof Symbol === 'function' && Symbol.for
12+
13+
/**
14+
* Symbol for React context consumer components. The bundle impact is too big to
15+
* import it the propper way from [`react-is`](https://npm.im/react-is). Also
16+
* [babel/babel#7998](https://github.com/babel/babel/issues/7998) would cause a
17+
* CJS runtime error.
18+
* @kind constant
19+
* @name REACT_CONTEXT_TYPE
20+
* @see [React source](https://github.com/facebook/react/blob/v16.6.0/packages/shared/ReactSymbols.js#L32).
21+
* @ignore
22+
*/
23+
const REACT_CONTEXT_TYPE = hasSymbol ? Symbol.for('react.context') : 0xeace
24+
125
/**
226
* Recursively preloads [`Query`]{@link Query} components that have the
327
* `loadOnMount` prop in a React element tree. Useful for server side rendering
@@ -64,15 +88,16 @@ export function preload(element) {
6488
// The element is not a childless string or number and…
6589
element.type &&
6690
// …It’s a context consumer or a functional/class component…
67-
(element.type.Consumer || typeof element.type === 'function')
91+
(element.type.$$typeof === REACT_CONTEXT_TYPE ||
92+
typeof element.type === 'function')
6893
) {
6994
// Determine the component props.
7095
const props = { ...element.type.defaultProps, ...element.props }
7196

72-
if (element.type.Consumer)
97+
if (element.type.$$typeof === REACT_CONTEXT_TYPE)
7398
// Context consumer element.
7499
recurse(
75-
element.props.children(element.type.currentValue),
100+
element.props.children(element.type._context.currentValue),
76101
legacyContext
77102
)
78103
else if (

0 commit comments

Comments
 (0)