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
Using React DnD with styled components. #1021
Comments
Edit: thanks to @zainfathoni for the suggestion to remove findDOMNode I recently ran into this issue myself and found that Dan answered this question a while ago. I adapted his solution to Semantic UI using its Here's an example with Semantic UI import React from 'react'
import { Item, Ref } from 'semantic-ui-react'
import { DragSource } from 'react-dnd'
const MyItem = props => (
<Item>
<Item.Content>
<Item.Header>{props.header}<Item.Header>
<Item.Content>{props.content}<Item.Content>
</Item.Content>
</Item>
)
const DraggableItem = props => {
const { connectDragSource } = props
return (
<Ref innerRef={instance => connectDragSource(instance)}>
<MyItem {...props} />
</Ref>
)
}
export default DragSource(...)(DraggableItem) |
You can just use Styled Component's innerRef to get the underlying DOM node, then you can call your connectDragSource to it. Apparently you don't need ReactDOM's In your case, it should be like this: class MyComponent extends Component {
...
render() {
const { connectDragSource } = this.props;
return (
<StyledComponent
innerRef={instance => connectDragSource(instance)}
/>
)
}
} You can also look at my implementation of Knight component for the official chess tutorial as a reference. |
I have a similar use case, only inside of a
Using the techique suggested by @zainfathoni above works well on the
Any ideas how I can 'dig down' to the native |
@artooras you probably have solved your problem until now but here is the solution - #347 (comment)
|
@darkowic |
@rzkhosroshahi what do you exactly mean? |
@darkowic |
@rzkhosroshahi I think we can't use that API for this. Just as they noted in the
|
I would like to add that styled components in version 4 passes original ref
via React.forwardRef to original component. So, probably styled components
may be used the same way like normal DOM elements with react dnd. I didn't
check it yet
pt., 26 paź 2018, 22:52 użytkownik Zain Fathoni <notifications@github.com>
napisał:
… @rzkhosroshahi <https://github.com/rzkhosroshahi>
Do you mean this createRef API
<https://reactjs.org/blog/2018/03/29/react-v-16-3.html#createref-api>?
I think we can't use that API for this.
Anyway, there's no plan to remove the callback API, so you can still use
the callback API for this.
Just as they noted in the createRef API docs:
Callback refs will continue to be supported in addition to the new
createRef API.
You don’t need to replace callback refs in your components. They are
slightly more flexible, so they will remain as an advanced feature.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1021 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AK-jNfM0ShyfqQdjt7nGS4uw0Zd_5kAqks5uo3X2gaJpZM4T1fRp>
.
|
<StyledComp ref={instance => connectDragSource(instance)} /> works flawlessly for me, EXCEPT it seems to completely break PropTypes as described in my SO question here... It think it must be something with the refs, since I have several other components which uses one or the other as well as a few which combines the two using native DOM element wrappers.. |
Is that work in production? I used this approach but when I building project my component is not dragged anymore. |
@rzkhosroshahi Minification seems to break it. It's not in production yet, so I haven't found a solution, but as long as i set |
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. |
I have the same problem. My question is, does it work when we're using react-dnd with their hooks ( useDrag and useDrop) and not decorators? Thanks |
I need to use React DnD with styled components.
I raised this question on stackoverflow, but have also posted this issue here in the hope of obtaining an answer:
https://stackoverflow.com/questions/50220622/how-to-use-react-dnd-with-styled-component
The text was updated successfully, but these errors were encountered: