File tree Expand file tree Collapse file tree 3 files changed +40
-7
lines changed
docs/components/NativeObserver Expand file tree Collapse file tree 3 files changed +40
-7
lines changed Original file line number Diff line number Diff line change @@ -155,9 +155,19 @@ Here's how to create an **element monitoring** component:
155155
156156``` jsx
157157import React , { Component } from ' react' ;
158+ import PropTypes from ' prop-types' ;
158159import Observer from ' @researchgate/react-intersection-observer' ;
159160
160161export default class ViewableMonitor extends Component {
162+ static propTypes = {
163+ tag: PropTypes .node ,
164+ children: PropTypes .func .isRequired ,
165+ }
166+
167+ static defaultProps = {
168+ tag: ' div' ,
169+ }
170+
161171 state = {
162172 isIntersecting: false ,
163173 }
@@ -167,16 +177,13 @@ export default class ViewableMonitor extends Component {
167177 };
168178
169179 render () {
170- const { children , mount: Tag , ... rest } = this .props ;
171- let element = children (this .state .isIntersecting );
172-
173- if (Tag) {
174- element = < Tag> {element}< / Tag> ;
175- }
180+ const { tag: Tag , children , ... rest } = this .props ;
176181
177182 return (
178183 < Observer {... rest} onChange= {this .handleChange }>
179- {element}
184+ < Tag>
185+ {children (this .state .isIntersecting )}
186+ < / Tag>
180187 < / Observer>
181188 );
182189 }
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ common situations that can be approached with ReactIntersectionObserver:
2020* [ Higher Order Component] ( docs/components/HigherOrderComponent/ )
2121* [ Tracking Ad Impressions] ( docs/components/ImpressionTracking/ )
2222* [ Viewable Monitor] ( docs/components/ViewableMonitor/ )
23+ * [ Graceful Degradation] ( docs/components/NativeObserver/ )
2324
2425### Can I submit a new recipe?
2526
Original file line number Diff line number Diff line change 1+ ## Gracefully handling environments without IntersectionObserver support
2+
3+ This module assumes ` IntersectionObserver ` is available as a global - the polyfill is necessary. However, there may be
4+ situations where we want to do progressive enhancement and only observe elements if it's available.
5+
6+ To prevent an exception in such cases, let's create a component that will gracefully fail if ` IntersectionObserver `
7+ doesn't exist, and instead render the passed children naturally.
8+
9+ ### Source
10+
11+ ``` jsx
12+ import React from ' react' ;
13+ import Observer from ' @researchgate/react-intersection-observer' ;
14+
15+ // Exits early if all IntersectionObserver and IntersectionObserverEntry
16+ // features are natively supported.
17+ const hasNativeSupport =
18+ ' IntersectionObserver' in window &&
19+ ' IntersectionObserverEntry' in window &&
20+ ' intersectionRatio' in window .IntersectionObserverEntry .prototype ;
21+
22+ export default function NativeObserver (props ) {
23+ return hasNativeSupport ? < Observer {... props} / > : props .children ;
24+ }
25+ ```
You can’t perform that action at this time.
0 commit comments