-
Notifications
You must be signed in to change notification settings - Fork 186
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
InView always forces a React Tree Reconciliation #195
Comments
The If you added multiple thresholds you'll want to to be notified whenever it crosses a threshold - so it needs to update the |
My observation is that the component fires a callback as soon as the observer is instantiated (see https://stackoverflow.com/questions/53214116/intersectionobserver-callback-firing-immediately-on-page-load). A component in the viewport will have two callbacks fired - one where |
This is the intended behavior. Are you seeing any actual performance issues because of this? To ignore the first callback would require extra logic, and if I recall correctly, not all (older) browsers trigger it. |
I think the problem I'm seeing is that the initial callback kicks off |
Looking at what happens in the Storybook, it only triggers a single Even if |
To clarify what I mean, here's an example (I probably should have led with this): () => {
console.log('render');
return (
<>
<div className="container">
<div className="grid">
{[...Array(40)].map((_, i) => (
<InView key={i}>
{({ inView, ref }) => {
console.log(inView);
return <div ref={ref} />;
}}
</InView>
))}
</div>
<style jsx>{`
.container {
padding: 0 16px 16px;
width: 100%;
}
.grid {
display: grid;
grid-gap: 16px;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}
`}</style>
</div>
</>
);
}; Rendering the above gives me the following: This is what I would expect and lines up with the built-in behavior for What is unexpected, however, are the following stack traces: The first observation: The second observation: In this case the extra work done by React to reconcile a state update of |
One of the reasons you might want to read the If you did try to render a heavy component, would using I have no way of knowing how this library is actually consumed, so maybe it wouldn't be a problem to change the behaviour. |
I went ahead and implemented this check - Let's see if it causes any actual issues. Properly not! |
Oh, great! Thank you for the fix! |
This also seems to happen to the hook still, unless the update hasn't been pushed to npm yet! |
The hook does an extra render when the ref is set. Is this what you're seeing? This is how hooks are designed. |
Sorry for the delay! I'm really sorry if this is just me misunderstanding intersection observer, but it seems like if I set |
That sounds like the intended behavior. It will trigger whenever it crosses the threshold, and when it doesn't match a threshold. You should look at the entry you also get back, to determine which threshold it just crossed |
Thanks again, your most recent update fixed this for me! I was using |
The
handleChange
callback always callssetState
regardless of whether or not the value forstate.inView
will change. This forces a React Tree Reconciliation for each observed component when the tree is mounted (IO calls the callback once for each observed component).I think an optimization could be made to https://github.com/thebuilder/react-intersection-observer/blob/master/src/InView.tsx#L96:
This would allow the child to receive a new value for
entry
when in view (where it counts), but would prevent the re-render/reconciliation that happens at the very beginning.What do you think? Am I missing some context here?
The text was updated successfully, but these errors were encountered: