Skip to content

Commit d0907e6

Browse files
committed
Use refs to replace ReactDOM.findDOMNode
1 parent c7e7b73 commit d0907e6

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ You can also pass a child function, which can be convenient if you don't need to
6161
function MyComponent (props) {
6262
return (
6363
<VisibilitySensor>
64-
{({isVisible}) =>
64+
{({ sensorRef, isVisible }) =>
6565
<div>I am {isVisible ? 'visible' : 'invisible'}</div>
6666
}
6767
</VisibilitySensor>

Diff for: visibility-sensor.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ export default class VisibilitySensor extends React.Component {
8383
}
8484

8585
componentDidMount() {
86-
this.node = ReactDOM.findDOMNode(this);
8786
if (this.props.active) {
8887
this.startWatching();
8988
}
@@ -94,9 +93,6 @@ export default class VisibilitySensor extends React.Component {
9493
}
9594

9695
componentDidUpdate(prevProps) {
97-
// re-register node in componentDidUpdate if children diffs [#103]
98-
this.node = ReactDOM.findDOMNode(this);
99-
10096
if (this.props.active && !prevProps.active) {
10197
this.setState({
10298
isVisible: null,
@@ -325,12 +321,20 @@ export default class VisibilitySensor extends React.Component {
325321
};
326322

327323
render() {
324+
const sensorRef = nodeRef => {
325+
this.node = nodeRef;
326+
};
327+
328328
if (this.props.children instanceof Function) {
329329
return this.props.children({
330+
sensorRef,
330331
isVisible: this.state.isVisible,
331332
visibilityRect: this.state.visibilityRect
332333
});
333334
}
334-
return React.Children.only(this.props.children);
335+
336+
return React.cloneElement(React.Children.only(this.props.children), {
337+
ref: sensorRef
338+
});
335339
}
336340
}

0 commit comments

Comments
 (0)