Skip to content

Commit

Permalink
Adapt preload for the React v16.6 context API changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanWooldridge committed Oct 25, 2018
1 parent df994b1 commit a7ac187
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.md
Expand Up @@ -5,6 +5,7 @@
### Patch

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

## 3.0.0

Expand Down
31 changes: 28 additions & 3 deletions src/preload.mjs
@@ -1,3 +1,27 @@
/**
* Whether or not the runtime environment supports Symbol.for. Although all
* supported versions of Node.js (see package engines field) support Symbol,
* the preload function may be also be used in the browser.
* @kind constant
* @name hasSymbol
* @type {boolean}
* @see [React source](https://github.com/facebook/react/blob/v16.6.0/packages/shared/ReactSymbols.js#L12).
* @ignore
*/
const hasSymbol = typeof Symbol === 'function' && Symbol.for

/**
* Symbol for React context consumer components. The bundle impact is too big to
* import it the propper way from [`react-is`](https://npm.im/react-is). Also
* [babel/babel#7998](https://github.com/babel/babel/issues/7998) would cause a
* CJS runtime error.
* @kind constant
* @name REACT_CONTEXT_TYPE
* @see [React source](https://github.com/facebook/react/blob/v16.6.0/packages/shared/ReactSymbols.js#L32).
* @ignore
*/
const REACT_CONTEXT_TYPE = hasSymbol ? Symbol.for('react.context') : 0xeace

/**
* Recursively preloads [`Query`]{@link Query} components that have the
* `loadOnMount` prop in a React element tree. Useful for server side rendering
Expand Down Expand Up @@ -64,15 +88,16 @@ export function preload(element) {
// The element is not a childless string or number and…
element.type &&
// …It’s a context consumer or a functional/class component…
(element.type.Consumer || typeof element.type === 'function')
(element.type.$$typeof === REACT_CONTEXT_TYPE ||
typeof element.type === 'function')
) {
// Determine the component props.
const props = { ...element.type.defaultProps, ...element.props }

if (element.type.Consumer)
if (element.type.$$typeof === REACT_CONTEXT_TYPE)
// Context consumer element.
recurse(
element.props.children(element.type.currentValue),
element.props.children(element.type._context.currentValue),
legacyContext
)
else if (
Expand Down

0 comments on commit a7ac187

Please sign in to comment.