-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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
Ref can not replace ReactDOM.findDOMNode #9217
Comments
It is discouraged in most cases (and people often use it unnecessarily), but it is useful exactly for the edge case you describe. |
Sorry,I am careless,the change log just say that it moved from |
@gaearon I notice that react-eslint has a rule and link to the previous discuss,you actually say that "We want to deprecate it eventually (not right now) ". So you mean that you change your idea now? |
You can also pass the class A extends React.Component {
render() {
return (
<div id="containerA">
<B bRef={ref => { this.containerB = ref; }}>
<span>some content...</span>
</B>
</div>
);
}
}
class B extends React.Component {
render() {
return (
<div id="containerB" ref={this.props.bRef}>
some cars....
{this.props.children}
</div>
);
}
} This is slightly better than defining a method on the component. The downside of this approach is having to pass the |
The thing is that when we try using some of the packages and their components which don't implement the flow @cPu1 described then we are left with two options. Recreate the actual component implementing the ref pass-down (which is NOT what you should do) or use the |
Well you can use |
I am glad I am not the only one worried about this feature being deprecated. I am currently using a 3rd party 'Webcam' component found here https://github.com/mozmorris/react-webcam. This component uses findDOMNode and I do not see how refs can be used to replace the functionality. Note: I do see that refs are being used here but if you look in the (very short) source code in src/react-webcam.js, it is also using findDOMNode. Here is the simple example of how you can use this component.
I think if you wanted to change this to use refs thenthe example code would require the user of this 3rd party library to understand the dom structure of the Webcam component which is using a video dom element and the getusermedia API. Requiring a 3rd party component user to understand the DOM structure of the component is not ideal to say the least. Please let me know if I am missing a simpler way to do this using refs. |
Where can one find documentation about which methods other than the |
Answering myself: turns out a <button ref={ref => this.button = ref}/> But in case of |
I'm looking for a
Is there any solution that meets all the points above? |
I notice that react will deprecated
ReactDOM.findDOMNode
and encourage developers to useref callback
instead.I agree with that in most cases it works fine.But think about something like this:For example,Now I want to know
containerB
'sclientLeft
,but I can't just add ref such as<B ref={ele => {this.containerB = ele;}}><span>some content...</span></B>
because theele
is instance ofB
not the containerB dom node.But
ReactDOM.findDOMNode
can do that,just useReactDOM.findDOMNode(this.containerB ).clientLeft
.Of course we can add
componentDidMout
in theB
and use<div id="containerB" ref={ele => {this.props.getContainerBClientLeft(ele.clientLeft)}}></div>
,and inA
,add a method calledgetContainerBClientLeft
and pass it toB
.But IMO, this make the app more complex.What's your thoughts?The text was updated successfully, but these errors were encountered: