Skip to content
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

Invariant Violation: Expected drag drop context find when deploy to server #1494

Open
TingOOOgniT opened this issue Jul 31, 2019 · 20 comments
Open

Comments

@TingOOOgniT
Copy link

TingOOOgniT commented Jul 31, 2019

My app is working well on my local environment but when I deploy to the server, the console prints error: 'Invariant Violation: Expected drag drop context find' when deploying to the server.
I totally have no idea about what's wrong. Could you help give me some advice on what may be a root cause?
My local node version is 8.11.3. And I use 8.11.3 on the server to build and an express server to serve the static files under default cloud node environment (v6.7.0).
I am using:
"react-dnd": "^9.0.1"
"react-dnd-html5-backend": "^9.0.0"
"react-dnd-touch-backend": "^9.0.2"

@TingOOOgniT
Copy link
Author

I upgraded my packages to the latest and the error occurs in my local environment.

@TingOOOgniT
Copy link
Author

It’s because I have a ^ and I use yarn in local but npm in the cloud to build so I got different package version in local and dev.
And when I use 9.3.3 and 9.3.2, I got the same error: 'Invariant Violation: Expected drag drop context find. I lost the context.
And 9.2.1 is fine.

@coxrichuk
Copy link

+1 I am getting this too

Environment looks like:

"react": "^16.8.6", "react-dnd": "^9.3.4", "react-dnd-html5-backend": "^9.3.4", "react-dom": "^16.8.6",

@sa-scole
Copy link

This probably means you've got DndProvider wrapping a component too low on your component tree.

@jamiewinder
Copy link

Did you solve this? I had the same issue and it turned out I had multiple react-dnd libraries of different versions being bundled in my app. Hopefully this helps someone.

@sauravgarg540
Copy link

I am also facing the same issue. Did someone find the solution, please share.

@binarybaba
Copy link

binarybaba commented Sep 16, 2019

I wasn't exporting the <DndProvider />. Once I did that, everything worked fine.

@roweldev-jukin
Copy link

roweldev-jukin commented Sep 24, 2019

you should wrap your test file component like so:

<DndProvider backend={HTML5Backend}> <Component /> </DndProvider>

@Foxhoundn
Copy link

you should wrap your test file component like so:

<DndProvider backend={HTML5Backend}> <Component /> </DndProvider>

I really wish this info would be available on this page http://react-dnd.github.io/react-dnd/docs/overview.

Otherwise users have to scroll through a whole implementation of the chess board to find this.

@markusmauch
Copy link

markusmauch commented Mar 26, 2020

I'm getting the Expected drag drop context error, too. For me this works only if the DndProvider is the immediate parent node of the component that uses useDrag. It works with an older version though (9.0.0). However, the hint

This should be mounted near the top of our application (meaning the DndProvider)

is definitely wrong.

@jacobdanielferguson
Copy link

@markusmauch

Thanks - this saved me a huge headache. Yeah, the documentation is definitely not accurate here.

@andrewlorenz
Copy link

For me I fixed the issue by having the useDrop and associated div using the ref from that in its own component, as opposed to just inline JSX. I didn't change a single line of code - just generated the droppable div from its own component outside of the component wrapping it with DndProvider ..

@DininduKanchana
Copy link

DininduKanchana commented Apr 24, 2021

Hi. I also faced this issue. The solutions provided here directed me to the right path. I added it to very top of the component tree.
To the index.tsx

ReactDOM.render(
  <React.StrictMode>
    <DndProvider backend={HTML5Backend}>
      <App />
    </DndProvider>
  </React.StrictMode>,
  document.getElementById('root'),
);

The imports --

import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';

@c4lliope
Copy link

c4lliope commented May 1, 2021

Looks like this is the documentation page we're seeking;
can we make this more visible on the docs website?
https://github.com/react-dnd/react-dnd/blob/e8bd6436548d96f6d6594f763752f424c2e0834b/packages/docsite/markdown/docs/03%20Using%20Hooks/DndProvider.md

@brunohlippert
Copy link

brunohlippert commented Apr 12, 2022

Had the same issue here. In my case, I was trying to add the droppable div to a library that was going to be consumed by another application, where the context would be defined. My solution was to move the react-dnd of the library to the devDependencies and as peerDependencie.

@cjmafei
Copy link

cjmafei commented Jul 15, 2022

Hi. I also faced this issue. The solutions provided here directed me to the right path. I added it to very top of the component tree. To the index.tsx

ReactDOM.render(
  <React.StrictMode>
    <DndProvider backend={HTML5Backend}>
      <App />
    </DndProvider>
  </React.StrictMode>,
  document.getElementById('root'),
);

The imports --

import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';

Did you solve this?

@DininduKanchana
Copy link

Hi. I also faced this issue. The solutions provided here directed me to the right path. I added it to very top of the component tree. To the index.tsx

ReactDOM.render(
  <React.StrictMode>
    <DndProvider backend={HTML5Backend}>
      <App />
    </DndProvider>
  </React.StrictMode>,
  document.getElementById('root'),
);

The imports --

import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';

Did you solve this?

yes

@PrestonWinstead
Copy link

I've tried both suggestions in this thread -- wrapping my app at the most core, root level that I can, and wrapping it immediately above the component that's using DND. No matter what, I get this error. Any updates on the correct way to handle this?

@SanjeevThalod
Copy link

Hi. I also faced this issue. The solutions provided here directed me to the right path. I added it to very top of the component tree. To the index.tsx

ReactDOM.render(
  <React.StrictMode>
    <DndProvider backend={HTML5Backend}>
      <App />
    </DndProvider>
  </React.StrictMode>,
  document.getElementById('root'),
);

The imports --

import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';

This solved the error for me

@Dustin-QB
Copy link

Dustin-QB commented Feb 13, 2024

I don't want to add the Dnd Provider any higher in your tree than necessary, since only a small portion of the app is actually using it. I solved it by wrapping sibling components in a fragment.

export default function DndComponentExample() {
  return (
    <div className="max-h-full overflow-auto border-l border-gray-200 py-10">
	<> // added a fragment here
	    <CustomComponent someProps={props} />
	    <CustomComponent someProps={otherProps} />
	</>
	<DndProvider backend={HTML5Backend}>{someDraggableInputs.map(renderDraggableInputs)}</DndProvider>
    </div>
	);
}```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests