-
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
React-dnd hijacks out of context "draggable" elements #3304
Comments
Also seeing this. |
+1 |
Reading the documentation I found one way to approach this use case. // ...
const DnDRef = React.useRef(null);
// ...
<div
key={`${headerGroup.id}-${headerGroupIndex}`}
className={`${c("tr header-group")}`}
ref={DnDRef} <-- Create your context
>
<DndProvider
key={`${headerGroup.id}-${headerGroupIndex}-dnd-provider`}
backend={HTML5Backend}
context={DnDRef} <-- Use the context you create
>
{// ...}
</DndProvider>
</div> |
It seems that there is something wrong with RafaelGB's solution. DnD don't work correctly. |
My use case was to fix an incompatibility with an architecture element used by my component (a plugin). I was able to fix it but finally dispensing with react-dnd. The problem came in the usedrop declaration since it affects all those components with the "onDrop" event loaded disabling them. Depends of your case just use native dnd |
Thank you for your reply which solved my doubt! Some of my imported components use native dnd. These components and react-dnd are not compatible at the same time.😭 |
I found the reason why dnd don't work.(referring to #3344) function DndProviderWrapper({ className, children }) {
const context = useRef(null);
const dndArea = context.current;
const html5Options = useMemo(() => ({ rootElement: dndArea }), [dndArea]);
return (
<div className={className} ref={context}>
{// it is important that DndProvider don't render if dndArea is null }
{dndArea && (
<DndProvider backend={HTML5Backend} options={html5Options}>
{children}
</DndProvider>
)}
</div>
);
}
// ...
<div>
<DndProviderWrapper>
{// ... your component used react-dnd}
</DndProviderWrapper>
{// ... your component used native dnd}
</div> |
Thank you all for addressing this ! const DndProviderWrapper: React.FC<any> = ({ className, children }) => {
const context = useRef(null);
//Make the dndAre a part of the component state otherwise, some times, the content doesn't renders
const [dndArea, setDnDArea] = useState(context.current);
//additionally, add a useEffect to track the context reference => might be overkill performance wise but works for my use case
useEffect(() => {
setDnDArea(context?.current);
}, [context])
const html5Options = useMemo(() => ({ rootElement: dndArea }), [dndArea]);
return (
<div className={className} ref={context}>
{/* it is important that DndProvider don't render if dndArea is null */}
{dndArea && (
<DndProvider backend={HTML5Backend} options={html5Options}>
{children}
</DndProvider>
)}
</div>
);
} |
I had the exact same problem and the above fixed my issue. Should consider merging this into the next release. |
When calling
useDrop
it will effectively remove all dragging capabilities from your app, even if they are outside theDndProvider
container.See:
https://codesandbox.io/s/festive-sky-muxxt?file=/src/index.js
Try dragging the red square, it's marked as
draggable
which means it should show the HTML5 dragging overlay.The issue is that I'm allowing components to be dragged in from outside the
DndProvider
because it's using react-grid-layout, but react-dnd breaks it.The text was updated successfully, but these errors were encountered: