@@ -55,7 +55,7 @@ npm install react-intersection-observer --save
5555#### ` useInView `
5656
5757``` js
58- const [ref , inView , entry ] = useInView (options)
58+ const [ref , inView , entry ] = useInView (options);
5959```
6060
6161React Hooks make it easy to monitor the ` inView ` state of your components. Call
@@ -66,21 +66,21 @@ Assign the `ref` to the DOM element you want to monitor, and the hook will
6666report the status.
6767
6868``` jsx
69- import React from ' react'
70- import { useInView } from ' react-intersection-observer'
69+ import React from ' react' ;
70+ import { useInView } from ' react-intersection-observer' ;
7171
7272const Component = () => {
7373 const [ref , inView , entry ] = useInView ({
7474 /* Optional options */
7575 threshold: 0 ,
76- })
76+ });
7777
7878 return (
7979 < div ref= {ref}>
8080 < h2> {` Header inside viewport ${ inView} .` }< / h2>
8181 < / div>
82- )
83- }
82+ );
83+ };
8484```
8585
8686[ ![ Edit react-intersection-observer] ( https://codesandbox.io/static/img/play-codesandbox.svg )] ( https://codesandbox.io/s/react-intersection-observer-ud2vo?fontsize=14&hidenavigation=1&theme=dark )
@@ -99,7 +99,7 @@ on `entry`, giving you access to all the details about the current intersection
9999state.
100100
101101``` jsx
102- import { InView } from ' react-intersection-observer'
102+ import { InView } from ' react-intersection-observer' ;
103103
104104const Component = () => (
105105 < InView>
@@ -109,9 +109,9 @@ const Component = () => (
109109 < / div>
110110 )}
111111 < / InView>
112- )
112+ );
113113
114- export default Component
114+ export default Component ;
115115```
116116
117117### Plain children
@@ -122,15 +122,15 @@ state in your own component. Any extra props you add to `<InView>` will be
122122passed to the HTML element, allowing you set the ` className ` , ` style ` , etc.
123123
124124``` jsx
125- import { InView } from ' react-intersection-observer'
125+ import { InView } from ' react-intersection-observer' ;
126126
127127const Component = () => (
128128 < InView as= " div" onChange= {(inView , entry ) => console .log (' Inview:' , inView)}>
129129 < h2> Plain children are always rendered . Use onChange to monitor state.< / h2>
130130 < / InView>
131- )
131+ );
132132
133- export default Component
133+ export default Component ;
134134```
135135
136136> ⚠️ When rendering a plain child, make sure you keep your HTML output semantic.
@@ -157,15 +157,15 @@ argument for the hooks.
157157> avoid the component re-rendering too often. For example:
158158
159159``` js
160- const THRESHOLD = [0.25 , 0.5 , 0.75 ] // Store multiple thresholds in a constant
160+ const THRESHOLD = [0.25 , 0.5 , 0.75 ]; // Store multiple thresholds in a constant
161161const MyComponent = () => {
162- const [ref , inView , entry ] = useInView ({ threshold: THRESHOLD })
162+ const [ref , inView , entry ] = useInView ({ threshold: THRESHOLD });
163163 return (
164164 < div ref= {ref}>
165165 Triggered at intersection ratio {entry .intersectionRatio }
166166 < / div>
167- )
168- }
167+ );
168+ };
169169```
170170
171171### InView Props
@@ -195,25 +195,25 @@ few ideas for how you can use it.
195195You can wrap multiple ` ref ` assignments in a single ` useCallback ` :
196196
197197``` js
198- import React , { useRef } from ' react'
199- import { useInView } from ' react-intersection-observer'
198+ import React , { useRef } from ' react' ;
199+ import { useInView } from ' react-intersection-observer' ;
200200
201201function Component (props ) {
202- const ref = useRef ()
203- const [inViewRef , inView ] = useInView ()
202+ const ref = useRef ();
203+ const [inViewRef , inView ] = useInView ();
204204
205205 // Use `useCallback` so we don't recreate the function on each render - Could result in infinite loop
206206 const setRefs = useCallback (
207207 (node ) => {
208208 // Ref's from useRef needs to have the node assigned to `current`
209- ref .current = node
209+ ref .current = node;
210210 // Callback refs, like the one from `useInView`, is a function that takes the node as an argument
211- inViewRef (node)
211+ inViewRef (node);
212212 },
213213 [inViewRef],
214- )
214+ );
215215
216- return < div ref= {setRefs}> Shared ref is visible: {inView}< / div>
216+ return < div ref= {setRefs}> Shared ref is visible: {inView}< / div> ;
217217}
218218```
219219
@@ -258,23 +258,23 @@ Call the `intersectionMockInstance` method with an element, to get the (mocked)
258258### Test Example
259259
260260``` js
261- import React from ' react'
262- import { render } from ' react-testing-library'
263- import { useInView } from ' react-intersection-observer'
264- import { mockAllIsIntersecting } from ' react-intersection-observer/test-utils'
261+ import React from ' react' ;
262+ import { render } from ' react-testing-library' ;
263+ import { useInView } from ' react-intersection-observer' ;
264+ import { mockAllIsIntersecting } from ' react-intersection-observer/test-utils' ;
265265
266266const HookComponent = ({ options }) => {
267- const [ref , inView ] = useInView (options)
268- return < div ref= {ref}> {inView .toString ()}< / div>
269- }
267+ const [ref , inView ] = useInView (options);
268+ return < div ref= {ref}> {inView .toString ()}< / div> ;
269+ };
270270
271271test (' should create a hook inView' , () => {
272- const { getByText } = render (< HookComponent / > )
272+ const { getByText } = render (< HookComponent / > );
273273
274274 // This causes all (existing) IntersectionObservers to be set as intersecting
275- mockAllIsIntersecting (true )
276- getByText (' true' )
277- })
275+ mockAllIsIntersecting (true );
276+ getByText (' true' );
277+ });
278278```
279279
280280## Intersection Observer
@@ -301,7 +301,7 @@ yarn add intersection-observer
301301Then import it in your app:
302302
303303``` js
304- import ' intersection-observer'
304+ import ' intersection-observer' ;
305305```
306306
307307If you are using Webpack (or similar) you could use
@@ -315,7 +315,7 @@ this:
315315 **/
316316async function loadPolyfills () {
317317 if (typeof window .IntersectionObserver === ' undefined' ) {
318- await import (' intersection-observer' )
318+ await import (' intersection-observer' );
319319 }
320320}
321321```
0 commit comments