@@ -58,16 +58,15 @@ npm install react-intersection-observer --save
5858const [ref , inView , entry ] = useInView (options)
5959```
6060
61- The new React Hooks make it easier than ever to monitor the ` inView ` state of
62- your components. Call the ` useInView ` hook with the (optional)
63- [ options] ( #options ) you need. It will return an array containing a ` ref ` , the
64- ` inView ` status and the current
61+ React Hooks make it easy to monitor the ` inView ` state of your components. Call
62+ the ` useInView ` hook with the (optional) [ options] ( #options ) you need. It will
63+ return an array containing a ` ref ` , the ` inView ` status and the current
6564[ ` IntersectionObserverEntry ` ] ( https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry ) .
6665Assign the ` ref ` to the DOM element you want to monitor, and the hook will
6766report the status.
6867
6968``` jsx
70- import React , { useRef } from ' react'
69+ import React from ' react'
7170import { useInView } from ' react-intersection-observer'
7271
7372const Component = () => {
@@ -195,17 +194,44 @@ few ideas for how you can use it.
195194You can wrap multiple ` ref ` assignments in a single ` useCallback ` :
196195
197196``` js
198- const setRefs = useCallback (
199- node => {
200- // Ref's from useRef needs to have the node assigned to `current`
201- ref .current = node
202- // Callback refs, like the one from `useInView`, is a function that takes the node as an argument
203- inViewRef (node)
204- },
205- [inViewRef],
206- )
197+ import React , { useRef } from ' react'
198+ import { useInView } from ' react-intersection-observer'
199+
200+ function Component (props ) {
201+ const ref = useRef ()
202+ const [inViewRef , inView ] = useInView ()
203+
204+ // Use `useCallback` so we don't recreate the function on each render - Could result in infinite loop
205+ const setRefs = useCallback (
206+ node => {
207+ // Ref's from useRef needs to have the node assigned to `current`
208+ ref .current = node
209+ // Callback refs, like the one from `useInView`, is a function that takes the node as an argument
210+ inViewRef (node)
211+ },
212+ [inViewRef],
213+ )
214+
215+ return < div ref= {setRefs}> Shared ref is visible: {inView}< / div>
216+ }
207217```
208218
219+ ### ` rootMargin ` isn't working as expected
220+
221+ When using ` rootMargin ` , the margin gets added to the current ` root ` - If your
222+ application is running inside a ` <iframe> ` , or you have defined a custom ` root `
223+ this will not be the current viewport.
224+
225+ You can read more about this on these links:
226+
227+ - [ Intersection Observer API] ( https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#The_intersection_root_and_root_margin )
228+ - [ w3c/IntersectionObserver: IntersectionObserver rootMargin ignored within iframe] ( https://github.com/w3c/IntersectionObserver/issues/283#issuecomment-507397917 )
229+ > If you want the root margin to apply to the iframe element's boundary, then
230+ > you'll have to use an observer with an explicit root, which should be
231+ > ` document.scrollingElement ` within the iframe. If you also need to compute
232+ > intersection with the top-level viewport (the implicit root), that will
233+ > require a second observer.
234+
209235## Testing
210236
211237In order to write meaningful tests, the ` IntersectionObserver ` needs to be
@@ -253,11 +279,12 @@ test('should create a hook inView', () => {
253279## Intersection Observer
254280
255281[ Intersection Observer] ( https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API )
256- is the API is used to determine if an element is inside the viewport or not.
257- [ Browser support is pretty good] ( http://caniuse.com/#feat=intersectionobserver ) -
282+ is the API used to determine if an element is inside the viewport or not.
283+ [ Browser support is really good] ( http://caniuse.com/#feat=intersectionobserver ) -
258284With
259285[ Safari adding support in 12.1] ( https://webkit.org/blog/8718/new-webkit-features-in-safari-12-1/ ) ,
260- all major browsers now support Intersection Observers natively.
286+ all major browsers now support Intersection Observers natively. Add the
287+ polyfill, so it doesn't break on older versions of iOS and IE11.
261288
262289### Polyfill
263290
0 commit comments