Skip to content

Commit

Permalink
Add support for React.forwardRef
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlangelaan committed Aug 10, 2018
1 parent c04e8c1 commit 7cd93e0
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ const isClassComponent = Comp =>
Comp.prototype.isReactComponent ||
Comp.prototype.isPureReactComponent)

const isForwardRef = Comp =>
Comp.type &&
Comp.type.$$typeof === Symbol.for('react.forward_ref') &&
typeof Comp.type.render === 'function'

const providesChildContext = instance => !!instance.getChildContext

// Recurse a React Element tree, running the provided visitor against each element.
Expand Down Expand Up @@ -176,7 +181,10 @@ export default function reactTreeWalker(
})
.catch(reject)

if (typeof getType(currentElement) === 'function') {
if (
typeof getType(currentElement) === 'function' ||
isForwardRef(currentElement)
) {
const Component = getType(currentElement)
const props = Object.assign(
{},
Expand All @@ -188,8 +196,14 @@ export default function reactTreeWalker(
children: getChildren(currentElement),
},
)

if (isClassComponent(Component)) {
if (isForwardRef(currentElement)) {
visitCurrentElement(
() => currentElement.type.render(props),
null,
currentContext,
currentContext,
).then(innerResolve)
} else if (isClassComponent(Component)) {
// Class component
const instance = new Component(props, currentContext)

Expand Down

0 comments on commit 7cd93e0

Please sign in to comment.