Open GoDotDotDot opened 7 years ago
Try wrapping <Panel />
in a <div />
inside of <Draggable />
.
Ran into something similar and wrapping my custom component in a standard one did the trick!
What @joshtronic suggested indeed worked for me. I had to provide a wrapping <div>
you see here:
{React.Children.map(this.props.children, child => {
return (
<Draggable>
<div>{React.cloneElement(child)}</div>
</Draggable>
);
})}
The children I'm mapping over here is a custom component: <Card />
It's unclear to me why this works, and I suppose it is worth documenting if it's a thing, heh
Oh, huh, I landed on this thread from a search but should note that I didn't get the error the OP mentioned. I had no error at all, and instead just wasn't able to drag the <Card />
around.
I hit this problem as well. In fact I already had a React component that was already a div (a styled component div), and I had to wrap it in another just <div></div>
before it started dragging!
I also encounter this problem.
Wrap the children with a div
element works in most case, but finally I choose to use React Context api to solve this problem, it should be more flexible, for you can consume the injected draggable props anywhere you want in the hierarchy children components and not need to add extra div
element.
Here are the sample typescript codes:
type DraggableChildrenProps = {
style?: React.CSSProperties
// other mouse or touch event from DraggableCore
}
const DraggableProxyContext = React.createContext<DraggableChildrenProps>(null)
const DraggableProxy: React.FC<DraggableChildrenProps> = (
props
) => {
const { children, ...rest } = props
return (
<DraggableProxyContext.Provider value={rest}>
{children}
</DraggableProxyContext.Provider>
)
}
const MyDraggableComponent: React.FC = (props) => {
return (
<DraggableCore>
<DraggableProxy>{props.children}</DraggableProxy>
</DraggableCore>
)
}
const Box: React.FC = (props) => {
const { style: draggableStyle, ...rest } = useContext(DraggableProxyContext)
return <div style={draggableStyle} {...rest}>{props.children}</div>
}
When I put my React component in Draggable,the terminal give me some error info:
code
app.js
panel.js
code pen
Could you help me resolve this?thanks.