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

Conversation

donperi
Copy link

@donperi donperi commented Nov 3, 2018

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 as ref prop to the root level element.

@ValGeorgiev
Copy link

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

@joshwnj
Copy link
Owner

joshwnj commented Dec 11, 2018

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.

@joshwnj
Copy link
Owner

joshwnj commented Dec 12, 2018

Ok, I think I see why it's not working. In the old version we have a ref of the VisibilitySensor component itself. But with this change we are passing the callback ref to the children.

To get the demo above working, I need to also set the ref prop, so this means there will be an upgrade step required for anyone who gets the new version:

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.

Copy link

@realdennis realdennis left a comment

Choose a reason for hiding this comment

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

@joshwnj

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);

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);

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;

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants