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

Allow bypassing the observer #42

Closed

Conversation

GreenGremlin
Copy link

@GreenGremlin GreenGremlin commented Jul 21, 2020

...by not passing a ref option.

Putting up this PR early for feedback. Tests are still needed.

I would like to use use-resize-observer, without adding the polyfill. This change makes that possible by not initializing the observer, if no ref is passed.

@@ -103,14 +103,10 @@ function useResizeObserver<T>(
}
}
});
}, []);
});
Copy link
Author

@GreenGremlin GreenGremlin Jul 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the dependency array is necessary, in case ref is passed as null, then later passed as a valid value. This should not affect performance though, because the effect bails once the observer is created.

@GreenGremlin GreenGremlin force-pushed the graceful-degredation branch from ce6cf92 to 80331d4 Compare July 22, 2020 17:19
@GreenGremlin
Copy link
Author

I've added a test to assert that a ResizeObserver is not initialized, if no ref is passed.

@GreenGremlin
Copy link
Author

Is there anything I can do the help get this PR merged?

@ZeeCoder
Copy link
Owner

ZeeCoder commented Jul 28, 2020 via email

@GreenGremlin
Copy link
Author

@ZeeCoder I don't mean to be a pest, but is there any chance you might have some time to look at this PR any time soon?

@ZeeCoder
Copy link
Owner

There are a couple issues with the proposed code changes here:

  • The tests are failing in a number of places.
  • Checking the RO instance on each effect run would be fine, but does not actually matter, since the subscriber effect after it will only run when the ref object instance changes,
  • The added test does not actually cover the above case where you set the ref to null first, then to a valid ref to start measuring.
  • Passing in null is possible currently (the TS types do not seem to support this though at the moment), but properly allowing null in a way that doesn't create an RO instance means we have to opt out of the hook providing the default ref as well, which it currently does, whenever the ref is falsy. (this is a breaking change.)

I've created a new PR based on your needs.
Still a WIP, but will cover your use case and have the above points addressed as well:
#44

Essentially the hook would consider "null" as a special ref param, which would signal for the hook that you might pass a real ref in later for observation, but for the time being you don't need it, and so a ResizeObserver instance would not be necessary either until then.

@ZeeCoder
Copy link
Owner

ZeeCoder commented Oct 11, 2020

@ZeeCoder ZeeCoder closed this Oct 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants