diff --git a/src/IntersectionObserver.js b/src/IntersectionObserver.js index 98da1563..a33008af 100644 --- a/src/IntersectionObserver.js +++ b/src/IntersectionObserver.js @@ -183,9 +183,13 @@ class IntersectionObserver extends React.Component { } } -class GuardedIntersectionObserver extends React.Component { +class ErrorBoundary extends React.Component { static displayName = 'ErrorBoundary(IntersectionObserver)'; + static propTypes = { + forwardedRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]), + }; + componentDidCatch(error, info) { if (Config.errorReporter) { Config.errorReporter(error, info); @@ -193,10 +197,18 @@ class GuardedIntersectionObserver extends React.Component { } render() { - return ; + const { forwardedRef, ...props } = this.props; + + return ; } } +const GuardedIntersectionObserver = React.forwardRef((props, ref) => ( + +)); + +GuardedIntersectionObserver.displayName = 'IntersectionObserver'; + export { GuardedIntersectionObserver as default, IntersectionObserver, diff --git a/src/__tests__/IntersectionObserver.spec.js b/src/__tests__/IntersectionObserver.spec.js index 87e48e18..87665f01 100644 --- a/src/__tests__/IntersectionObserver.spec.js +++ b/src/__tests__/IntersectionObserver.spec.js @@ -141,6 +141,23 @@ test('reports errors by re-throwing trying observer children without a DOM node' Config.errorReporter = originalErrorReporter; }); +test('error boundary forwards ref', () => { + let observer; + renderer.create( + { + observer = instance; + }} + > +
+ , + { createNodeMock } + ); + + expect(observer instanceof IntersectionObserver).toBe(true); +}); + test('should not observe children that equal null or undefined', () => { const sizeBeforeObserving = observerElementsMap.size; renderer.create(