Skip to content

Commit

Permalink
Use ref instead of ReactDOM.findDOMNode()
Browse files Browse the repository at this point in the history
ReactDOM.findDOMNode() is discouraged, so we use refs instead.
This allows us to use this in our server-side render correctly
  • Loading branch information
gwu committed Feb 28, 2017
1 parent f94ce24 commit 7f01c51
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Hammer.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var HammerComponent = React.createClass({
},

componentDidMount: function () {
this.hammer = new Hammer(ReactDOM.findDOMNode(this));
this.hammer = new Hammer(this.domElement);
updateHammer(this.hammer, this.props);
},

Expand All @@ -126,6 +126,14 @@ var HammerComponent = React.createClass({
}
}, this);

var self = this;
props.ref = function(domElement) {
if (self.props.ref) {

This comment has been minimized.

Copy link
@paranoidjk

paranoidjk May 16, 2017

@silentcloud 是不是这里影响了你给 Hammer 设 ref ?

This comment has been minimized.

Copy link
@silentcloud

silentcloud May 16, 2017

Contributor

就是这个问题,把我 children 设置的 ref 覆盖了

This comment has been minimized.

Copy link
@paranoidjk

paranoidjk May 16, 2017

@silentcloud 这里要求 Hammer 的 props 一定要是个 function, https://facebook.github.io/react/docs/refs-and-the-dom.html#exposing-dom-refs-to-parent-components, 因为你给 Hammer假的 this.refs.content 其实是要获取chuildren 的 dom node

This comment has been minimized.

Copy link
@silentcloud
self.props.ref(domElement);
}
self.domElement = domElement;
};

// Reuse the child provided
// This makes it flexible to use whatever element is wanted (div, ul, etc)
return React.cloneElement(React.Children.only(this.props.children), props);
Expand Down

1 comment on commit 7f01c51

@ibc
Copy link

@ibc ibc commented on 7f01c51 Apr 15, 2018

Choose a reason for hiding this comment

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

Please sign in to comment.