-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Examples using findDOMNode() #591
Comments
recommended way of doing it is by using refs, see: https://facebook.github.io/react/docs/refs-and-the-dom.html you can then access passed refs by calling |
The way we've fixed it on our end is as follows: In our draggable:
And inside of our
I'm not sure if there are better ways to go about this, maybe @gaearon can shine some light on this. That would be greatly appreciated. |
That's the recommended way from facebook docs. We are doing it in the same fashion |
If I have some time I'll look at this later. In the meantime feel free to cut a PR |
We're taking the ref approach, but Just for reference, our components are both draggable & droppable: export default flow(
DragSource('NavItem', navItemDragSource, dragCollect),
DropTarget('NavItem', navItemDropSource, dropCollect)
)(NavigationItem); Our class NavigationItem extends React.Component
render() {
return this.props.connectDropTarget(this.props.connectDragSource(
<section
ref={element => (this.navItemElement = element)}
</section>
}
} And in const navItemDropSource = {
hover: (props, monitor, component) => {
console.log(component.decoratedComponentInstance); // prints null
}
} Any idea what we're missing here? |
In our app it looks roughly like that:
in your example I see that you are assigning the element param to
|
Got is working! It turns out the trick was to call Our working version looks like this: class NavigationItem extends React.Component
render() {
return this.props.connectDragSource(this.props.connectDropTarget(
<section
ref={node => (this.node = node)}
>
{this.props.name}
</section>
));
}
} const navItemDropSource = {
hover: (props, monitor, component) => {
console.log(component.decoratedComponentInstance.node.getBoundingClientRect());
}
} Thanks for the help @beniutek |
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. |
For anyone who struggles with this issue and tries to use the solution of @ndelage and @beniutek, Here is what you need: class NavigationItem extends React.Component
constructor(props){
super(props);
this.node = React.createRef();
}
render() {
const { connectDragSource, connectDropTarget } = this.props;
return connectDragSource(connectDropTarget(
<section ref={this.node}>
{this.props.name}
</section>
));
}
} const navItemDropSource = {
hover: (props, monitor, component) => {
console.log(component.getDecoratedComponentInstance().node.getBoundingClientRect());
}
} I hope someone will soon be able to update the example. |
I need to add some pins over an image and to get the drop component's viewport positions, I added the ref I saw that I can get the drop: (props, monitor, component) => {
console.log(component.node.current.getBoundingClientRect())
} |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Using
findDOMNode()
is no longer recommended. In fact, if you use it, React will warn you not to. Some of the examples use findDOMNode in places where I don't think you can use refs to replace them. These should be updated to use something else if we shouldn't be using findDOMNode anymore.One specific example of this is here: https://github.com/gaearon/react-dnd/blob/master/examples/04%20Sortable/Simple/Card.js#L34
Other examples: https://github.com/gaearon/react-dnd/search?l=JavaScript&q=finddomnode&utf8=%E2%9C%93
What is the recommended way to resolve this?
The text was updated successfully, but these errors were encountered: