Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use refs to replace ReactDOM.findDOMNode #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ You can also pass a child function, which can be convenient if you don't need to
function MyComponent (props) {
return (
<VisibilitySensor>
{({isVisible}) =>
{({ sensorRef, isVisible }) =>
<div>I am {isVisible ? 'visible' : 'invisible'}</div>
}
</VisibilitySensor>
Expand Down
14 changes: 9 additions & 5 deletions visibility-sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export default class VisibilitySensor extends React.Component {
}

componentDidMount() {
this.node = ReactDOM.findDOMNode(this);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.node = this.sensorNode || ReactDOM.findDOMNode(this);

if (this.props.active) {
this.startWatching();
}
Expand All @@ -94,9 +93,6 @@ export default class VisibilitySensor extends React.Component {
}

componentDidUpdate(prevProps) {
// re-register node in componentDidUpdate if children diffs [#103]
this.node = ReactDOM.findDOMNode(this);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.node = this.sensorNode || ReactDOM.findDOMNode(this);


if (this.props.active && !prevProps.active) {
this.setState({
isVisible: null,
Expand Down Expand Up @@ -325,12 +321,20 @@ export default class VisibilitySensor extends React.Component {
};

render() {
const sensorRef = nodeRef => {
this.node = nodeRef;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.node = nodeRef;
this.sensorNode = nodeRef;

};

if (this.props.children instanceof Function) {
return this.props.children({
sensorRef,
isVisible: this.state.isVisible,
visibilityRect: this.state.visibilityRect
});
}
return React.Children.only(this.props.children);

return React.cloneElement(React.Children.only(this.props.children), {
ref: sensorRef
});
}
}