react-dnd: Drag source is not rendering at all
Describe the bug
Component decorated with DragSource is not rendered at all (render function is not called)
To Reproduce Write such component:
import * as React from "react";
import styled from "styled-components";
import {
DragSource,
DragSourceConnector,
DragSourceMonitor,
ConnectDragSource
} from "react-dnd";
const ImageHolder = styled.div`
margin: 10px;
max-width: 200px;
width: 200px;
min-height: 100px;
position: relative;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
`;
const ImageNameHolder = styled.div`
position: absolute;
bottom: 0;
left: 0;
right: 0;
padding: 10px;
background-color: rgba(0, 0, 0, 0.5);
font-size: 13px;
`;
interface Props {
imagePath: string;
connectDragSource?: ConnectDragSource;
isDragging?: boolean;
}
@DragSource<Props>(
"image",
{
beginDrag: (props: Props) => props.imagePath
},
(connect: DragSourceConnector, monitor: DragSourceMonitor) => ({
connectDragSource: connect.dragSource(),
isDragging: monitor.isDragging()
})
)
export class PhotosBrowserPhoto extends React.Component<Props> {
render() {
const { imagePath, connectDragSource } = this.props;
console.log({ connectDragSource });
return connectDragSource(
<div>drag me</div>
);
}
}
Expected behavior
Draggable component with drag me text, connectDragSource printed in the console
Actual behavior Nothing is rendered at all, console is empty
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 5
- Comments: 15 (1 by maintainers)
Commits related to this issue
- fix: throw error when a DragSource or DropTarget is rendered outside DragDropContext (#1132) (#1286) Previously the HOC's would just return null, leading to "missing" components, with nothing pointin... — committed to react-dnd/react-dnd by jeppester 5 years ago
- fix: throw error when a DragSource or DropTarget is rendered outside DragDropContext (#1132) (#1286) Previously the HOC's would just return null, leading to "missing" components, with nothing pointin... — committed to react-dnd/react-dnd by jeppester 5 years ago
I had to add the provider as shown below:
Adding provider component in root of my app solved the issue.
I think however - it should be clearly noted in example code that it’s needed - I thought seeing example is enough to understand how to start using library
This isn’t anywhere in the Quick Start section of the docs.
This is practically the first thing that ought to be in the docs…
I think the problem is this
if-statement: https://github.com/react-dnd/react-dnd/blob/934efc81871f30c6038e2dc52be1504fe38132e7/packages/react-dnd/src/decorateHandler.tsx#L210It looks like it disables a check that would otherwise throw an error if there’s no context.
Same thing is happening to me, though I’m using ES6 syntax:
Using:
Expected: see rendered component and console logs Actual: nothing is rendered; nothing is logged