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

Cannot have two HTML5 backends at the same time #3178

Open
ThiBsc opened this issue Apr 16, 2021 · 15 comments
Open

Cannot have two HTML5 backends at the same time #3178

ThiBsc opened this issue Apr 16, 2021 · 15 comments

Comments

@ThiBsc
Copy link

ThiBsc commented Apr 16, 2021

Describe the bug
I use different react-table draggable in different react-tabs
My Index.js contains:

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

ReactDOM.render(
   <DndProvider backend={HTML5Backend}>
      <BrowserRouter basename={baseUrl}>
         <App />
      </BrowserRouter>,
   </DndProvider>,
  rootElement);

each time I navigate into my components insinde and come back to the component with the react-table inside react-tabs, this following error is triggered:

HTML5BackendImpl.js:423 Uncaught Error: Cannot have two HTML5 backends at the same time.
    at HTML5BackendImpl.setup (HTML5BackendImpl.js:423)
    at DragDropManagerImpl.handleRefCountChange (DragDropManagerImpl.js:21)
    at Object.dispatch (redux.js:222)
    at HandlerRegistryImpl.addTarget (HandlerRegistryImpl.js:101)
    at registerTarget (registration.js:3)
    at registerDropTarget (useRegisteredDropTarget.js:23)
    at commitHookEffectListMount (react-dom.development.js:19731)
    at commitLifeCycles (react-dom.development.js:19787)
    at commitLayoutEffects (react-dom.development.js:22803)
    at HTMLUnknownElement.callCallback (react-dom.development.js:188)

I also try those workaround:

But both doesn't solve my problem

Expected behavior
I would like to change tab without trigger this error, I am unable to use it without having this error.

Desktop (please complete the following information):

  • OS: Win10
  • Browser: Chrome Version 89.0.4389.114 (Build officiel) (64 bits)
  • Version: ["react-dnd": "^14.0.2", "react-dnd-html5-backend": "^14.0.0"]
@ThiBsc
Copy link
Author

ThiBsc commented Apr 16, 2021

I found a way that fix the problem but it's not satisfying:

  • Downgrade react-dnd and react-dnd-html5-backend to the same version used by our version of react-sortable-tree (11.1.3)
  • create a file like dnd-context.js:
    import React, { useRef } from 'react';
    import PropTypes from 'prop-types';
    //import { createDragDropManager } from 'dnd-core';
    import { DndProvider } from 'react-dnd';
    import { HTML5Backend } from 'react-dnd-html5-backend';
    
    //const DNDManager = createDragDropManager(HTML5Backend);
    export const GlobalDndContext = (props) =>
    {
       //const manager = useRef(DNDManager);
       // the following line solve the problem only with key property
       return <DndProvider backend={HTML5Backend} key={1}>{props.children}</DndProvider>
    }
    
    GlobalDndContext.PropTypes = { children: PropTypes.node };
    
    export default GlobalDndContext;
  • In index.js that render <App/>:
    ReactDOM.render(
       <GlobalDndContext>
         <BrowserRouter basename={baseUrl}>
           <App />
         </BrowserRouter>
      </GlobalDndContext>,
    rootElement);

Help source: MJGang answer, milban answer

@ffan0811
Copy link

I found a way that fix the problem but it's not satisfying:

  • Downgrade react-dnd and react-dnd-html5-backend to the same version used by our version of react-sortable-tree (11.1.3)
  • create a file like dnd-context.js:
    import React, { useRef } from 'react';
    import PropTypes from 'prop-types';
    //import { createDragDropManager } from 'dnd-core';
    import { DndProvider } from 'react-dnd';
    import { HTML5Backend } from 'react-dnd-html5-backend';
    
    //const DNDManager = createDragDropManager(HTML5Backend);
    export const GlobalDndContext = (props) =>
    {
       //const manager = useRef(DNDManager);
       // the following line solve the problem only with key property
       return <DndProvider backend={HTML5Backend} key={1}>{props.children}</DndProvider>
    }
    
    GlobalDndContext.PropTypes = { children: PropTypes.node };
    
    export default GlobalDndContext;
  • In index.js that render <App/>:
    ReactDOM.render(
       <GlobalDndContext>
         <BrowserRouter basename={baseUrl}>
           <App />
         </BrowserRouter>
      </GlobalDndContext>,
    rootElement);

Help source: MJGang answer, milban answer

Thanks for the suggestion! I use ^10.0.2 for react-dnd and react-dnd-html5-backend and it works fine.
but why key={1}? how does it solve the issue? also, why not key={Math. random()}?

@ThiBsc
Copy link
Author

ThiBsc commented Apr 26, 2021

@ffan0811 I have absolutely no idea why key={1} solve the problem, and it works too with Math.random() but as I try first with 1 and it were working, I let it.

@vitrinh282
Copy link

i tried like above, but still same error, any way to fix it, it take me along time

@ThiBsc
Copy link
Author

ThiBsc commented May 31, 2021

@vitrinh282 Have you other component that use react-dnd ? And what version do you use, and in the case of you have component using it... what version too ?

@webiamcome
Copy link

if (window.__isReactDndBackendSetUp) {
window.__isReactDndBackendSetUp = false
}

@wohaiwo
Copy link

wohaiwo commented Nov 11, 2022

关联 #3257
一般都是嵌套了两个 试试 context={window}

@rumunanfeng
Copy link

rumunanfeng commented Oct 19, 2023

const myFirstId = 'first-DnD-Containier';
const mySecondId = 'second-DnD-Containier';

export const DndWrapper = React.memo((props) => {
  const [context, setContext] = useState(null);

  useEffect(() => {
    setContext(document.getElementById(props.id))
  },[props.id])

  return context ? (
      <DndProvider backend={HTML5Backend} options={{ rootElement: context}}>
          {props.children}
      </DndProvider>
  ) : null;
});

export default function App() {
  return (
    <div>
      <div id={myFirstId}>
        <DndWrapper id={myFirstId}>
               <MyOtherComponents /> 
        </DndWrapper>
      </div>
      <div id={mySecondId}>
          <DndWrapper id={mySecondId}>
            <MyOtherComponents />
          </DndWrapper>
      </div>
    </div>
  );
}

@barbalex
Copy link

barbalex commented Oct 26, 2023

Happens in my project too, when react-dnd is used in multiple forms and more than one of them is rendered at the same time.
This footgun makes react-dnd basically unusable 😢

Edit: seems to only happen sometimes. I have no idea what is going on...

@GabrielModog
Copy link

I'm having this issue too. I tried every solution that could find, but nothing works.

This footgun makes react-dnd basically unusable².

@mcturner1995
Copy link

mcturner1995 commented Feb 7, 2024

I'm also having this issue in our project. Any other long term solutions?

@barbalex
Copy link

This is extremely frustrating. Just had users report this error in production. I now have to remove ract-dnd 😞

@barbalex
Copy link

I am now trying to circumvent this problem. The basic idea is:

  • create a HTML5Backend only once
  • save it in an appropriate place (in my case: as volatile state in the mobx store)
  • when a component needs a HTML5Backend:
    • check if there is one in the store. If true: use it
    • if false: create one and save it in the store

These are the changes I made in our project: barbalex/apf2@4011e3d

@maskott
Copy link

maskott commented Mar 6, 2024

I think this may be manifesting in https://github.com/sanity-io/hierarchical-document-list as a totally broken experience when trying to drag and drop a hierarchical document.

@dannobytes
Copy link

The answer is here!
#3257 (comment)

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