-
Notifications
You must be signed in to change notification settings - Fork 195
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
base: master
Are you sure you want to change the base?
Conversation
Hi @joshwnj could you have a look into this PR, please. It will be nice to not have this warning, when we are using the latest react versions. Thanks |
Thanks @donperi ! Just got back from vacation so I'm eager to get this merged now :) This looks good, my only hesitation is that I tried updating this demo to the new version and it no longer worked: https://codesandbox.io/s/p73kyx9zpm Not sure yet why, so if you have any ideas please let me know. |
Ok, I think I see why it's not working. In the old version we have a ref of the To get the demo above working, I need to also set the function MyComponent (props) {
return (
<VisibilitySensor>
- {({isVisible}) =>
- <div>I am {isVisible ? 'visible' : 'invisible'}</div>
+ {({ sensorRef, isVisible }) =>
+ <div ref={sensorRef}>I am {isVisible ? 'visible' : 'invisible'}</div>
}
</VisibilitySensor> I wonder if there's a way that doesn't require an upgrade step for user code? Or at very least we'll need a way to clearly inform anyone with the new version that hasn't added the ref prop. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really sure whether it would work, but could we just fallback to the React.findDOMNode
for the original usage to fix the issue you mentioned? Even somehow developers do not forward the sensorRef
to child, it should also work as expected.
And update the document of two usage, so that developers could choose using ref
in new version React (maybe concurrent mode?) or just let our sensor auto get the node by findDOMNode
?
@@ -83,7 +83,6 @@ export default class VisibilitySensor extends React.Component { | |||
} | |||
|
|||
componentDidMount() { | |||
this.node = ReactDOM.findDOMNode(this); |
There was a problem hiding this comment.
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);
@@ -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); |
There was a problem hiding this comment.
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);
@@ -325,12 +321,20 @@ export default class VisibilitySensor extends React.Component { | |||
}; | |||
|
|||
render() { | |||
const sensorRef = nodeRef => { | |||
this.node = nodeRef; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.node = nodeRef; | |
this.sensorNode = nodeRef; |
Resolves #141
Using callback refs format will give the package the most backward compatible change.
The new React.createRef is available from 16.3.
I added a breaking change.
From this on, callback children will hace the
sensorRef
that should be pass asref
prop to the root level element.