Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with composed HOC and forwarding Ref #44

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/lib/handleViewport.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
// HOC for handleViewport
import React, { useRef, forwardRef } from 'react';
import hoistNonReactStatic from 'hoist-non-react-statics';
import useInViewport from './useInViewport';

const noop = () => {};

function handleViewport(TargetComponent, options, config = { disconnectOnLeave: false }) {
const ForwardedRefComponent = forwardRef((props, ref) => {
return <TargetComponent {...props} forwardedRef={ref} />;
});

const InViewport = ({ onEnterViewport = noop, onLeaveViewport = noop, ...restProps }) => {
const node = useRef();
const InViewport = ({
onEnterViewport = noop,
onLeaveViewport = noop,
...restProps
}, ref) => {
const node = useRef(ref);
const { inViewport, enterCount, leaveCount } = useInViewport(
node,
options,
Expand All @@ -21,9 +20,9 @@ function handleViewport(TargetComponent, options, config = { disconnectOnLeave:
onLeaveViewport
}
);

return (
<ForwardedRefComponent
<TargetComponent
{...restProps}
inViewport={inViewport}
enterCount={enterCount}
Expand All @@ -33,7 +32,9 @@ function handleViewport(TargetComponent, options, config = { disconnectOnLeave:
);
};

return hoistNonReactStatic(InViewport, ForwardedRefComponent);
const name = TargetComponent.displayName || TargetComponent.name || 'Component';
InViewport.displayName = `handleViewport(${name})`;
return forwardRef(InViewport);
}

export default handleViewport;