Skip to content

Commit f3c22d1

Browse files
authored
Fix findDOMNode deprecation warning
This PR fixes React `findDOMNode` deprecation warning. https://reactjs.org/docs/strict-mode.html#warning-about-deprecated-finddomnode-usage This makes sure the component is compatible with upcoming concurrent mode. It's a breaking change and requires a major release because it adds a node to the DOM, we could use `display: contents;` as suggested by React.
1 parent c7e7b73 commit f3c22d1

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

visibility-sensor.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ export default class VisibilitySensor extends React.Component {
7676
constructor(props) {
7777
super(props);
7878

79+
this.node = React.createRef();
80+
7981
this.state = {
8082
isVisible: null,
8183
visibilityRect: {}
8284
};
8385
}
8486

8587
componentDidMount() {
86-
this.node = ReactDOM.findDOMNode(this);
8788
if (this.props.active) {
8889
this.startWatching();
8990
}
@@ -94,9 +95,6 @@ export default class VisibilitySensor extends React.Component {
9495
}
9596

9697
componentDidUpdate(prevProps) {
97-
// re-register node in componentDidUpdate if children diffs [#103]
98-
this.node = ReactDOM.findDOMNode(this);
99-
10098
if (this.props.active && !prevProps.active) {
10199
this.setState({
102100
isVisible: null,
@@ -219,7 +217,7 @@ export default class VisibilitySensor extends React.Component {
219217
* Check if the element is within the visible viewport
220218
*/
221219
check = () => {
222-
const el = this.node;
220+
const el = this.node && this.node.current;
223221
let rect;
224222
let containmentRect;
225223

@@ -324,13 +322,17 @@ export default class VisibilitySensor extends React.Component {
324322
return state;
325323
};
326324

327-
render() {
325+
renderChildren = () => {
328326
if (this.props.children instanceof Function) {
329327
return this.props.children({
330328
isVisible: this.state.isVisible,
331329
visibilityRect: this.state.visibilityRect
332330
});
333331
}
334-
return React.Children.only(this.props.children);
332+
return React.Children.only(this.props.children)
333+
}
334+
335+
render() {
336+
return <div ref={this.node}>{this.renderChildren()}</div>;
335337
}
336338
}

0 commit comments

Comments
 (0)