react-beautiful-dnd icon indicating copy to clipboard operation
react-beautiful-dnd copied to clipboard

React 18 (RC 0) StrictMode rendering breaks ability to find node by it's draggable id

Open Malien opened this issue 2 years ago • 37 comments

Expected behavior

React Beautiful DnD should work as advertised on the latest React version with StrictMode enabled (which is a common default nowadays)

Actual behavior

Library fails to find draggable nodes with messaged logged (in development mode): Unable to find draggable with id: card-2

When using the, now deprecated, ReactDOM.render API (instead of new ReactDOM.createRoot), React is forced to work in React 17 compatible manner, which eliminates the problem.

As I've found out, removing <React.StrictMode /> wrapper resolved the issue, which means React Beautiful DnD is not compatible with Strict Effects

With the release of React 18, StrictMode gets an additional behavior that we call “strict effects” mode. When strict effects are enabled, React intentionally double-invokes effects (mount -> unmount -> mount) for newly mounted components. Like other strict mode behaviors, React will only do this for development builds.

Steps to reproduce

  1. yarn add react@rc react-dom@rc react-beautiful-dnd,
ReactDOM.createRoot(document.getElementById("root")).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
)
  1. Follow the guide precisely
  2. *profit*

Suggested solution?

I am not familiar with innerworkings of the library, but I suspect the way registrations registry.draggable are handled makes StrictMode incompatible (I've only looked into it in debugger for like 10 seconds). I suspect other registrations also fail

What version of React are you using?

18.0.0-rc0

What version of react-beautiful-dnd are you running?

13.1.2

What browser are you using?

Safari 15.2 beta, Chrome 96

Demo

https://codesandbox.io/s/vertical-list-forked-hd8ve?file=/index.js

Malien avatar Dec 22 '21 10:12 Malien

Same here, any plans to resolve this?

faradaytrs avatar Dec 28 '21 01:12 faradaytrs

You can follow the progress of this issue here: https://github.com/react-forked/dnd/issues/317

100terres avatar Apr 10 '22 16:04 100terres

any updates on this issue?

Shahzad6077 avatar May 08 '22 02:05 Shahzad6077

Hit this issue today too. 'Fixed' by removing the <React.StrictMode> components round the rendering of<App>in my index.js.

mjoycemilburn avatar May 10 '22 09:05 mjoycemilburn

Beware guys that StrictMode also helps you with preparing your development builds for production. Make sure to check whether the issue is present in production too.

enisdenjo avatar May 10 '22 09:05 enisdenjo

Will it be fixed ? I'm not a fan of removing StrictMode... it's a dirty fix and React 18 is not in rc anymore.

jbournonville avatar Jun 16 '22 09:06 jbournonville

any fix yet?

Danzo7 avatar Jun 20 '22 22:06 Danzo7

same problem here :/

Malin88 avatar Jun 29 '22 13:06 Malin88

same issue. Is there any fix for avoiding a way around like removing strict mode?

aujisti-ador avatar Jul 01 '22 15:07 aujisti-ador

same problem

ShekibAziz avatar Jul 12 '22 03:07 ShekibAziz

yup, same thing

peterb0yd avatar Jul 16 '22 05:07 peterb0yd

going through same issue.

bhagi108 avatar Jul 18 '22 11:07 bhagi108

The workaround here works OK for me: https://github.com/atlassian/react-beautiful-dnd/issues/2399#issuecomment-1167427762

Meligy avatar Jul 19 '22 13:07 Meligy

If anyone is looking for a less hacky solution, I have a fork of this library @hello-pangea/dnd which natively supports react 18 and react strict mode.

Just replace react-beautiful-dnd with @hello-pangea/dnd and you should be good to go 👍

Xhale1 avatar Aug 13 '22 08:08 Xhale1

If anyone is looking for a less hacky solution, I have a fork of this library @hello-pangea/dnd which natively supports react 18 and react strict mode.

Just replace react-beautiful-dnd with @hello-pangea/dnd and you should be good to go 👍

Is this going to be merged into react-beautiful-dnd eventually?

Kahitar avatar Sep 11 '22 08:09 Kahitar

same problem

RGBWang avatar Sep 24 '22 03:09 RGBWang

@Xhale1 I seems not to get it, replacing it means installing @hello-pangea/dnd](https://github.com/hello-pangea/dnd). won't it affect the functions?

Hollumidhe avatar Sep 28 '22 16:09 Hollumidhe

@Kahitar

Is this going to be merged into react-beautiful-dnd eventually?

I do not think it will. Atlassian do not plan to put more effort on this library. At least in the foreseeable future.

100terres avatar Oct 13 '22 02:10 100terres

@olumide12-cell

@Xhale1 I seems not to get it, replacing it means installing @hello-pangea/dnd](https://github.com/hello-pangea/dnd). won't it affect the functions?

I'm not certain to understand the question, but yes you should try to install @hello-pangea/dnd instead of react-beautiful-dnd and replace all the imports in your code.

@hello-pangea/dnd is a fork of react-beautiful-dnd and it supports react 18 with strict mode enabled.

100terres avatar Oct 13 '22 02:10 100terres

focus on this issue

linxiaowu66 avatar Oct 16 '22 15:10 linxiaowu66

Ran into same problem

arfanliaqat avatar Oct 18 '22 03:10 arfanliaqat

It's been nearly a year, please fix this.

WGTW avatar Nov 16 '22 17:11 WGTW

This article is helpful to me. https://medium.com/@wbern/getting-react-18s-strict-mode-to-work-with-react-beautiful-dnd-47bc909348e4

shunk76 avatar Nov 17 '22 07:11 shunk76

I can confirm that solution from @shunk76 comment is working

Grawl avatar Dec 07 '22 03:12 Grawl

I can confirm that the solution https://github.com/atlassian/react-beautiful-dnd/issues/2350#issuecomment-1276934082 by 100terres is working.

This library continues to be relied upon heavily by Atlassian products, but we are focused on other priorities right now and have no current plans for further feature development or improvements.

It will continue to be here on GitHub and we will still make critical updates (e.g. security fixes, if any) as required, but will not be actively monitoring or replying to issues and pull requests.

This opening statement from the readme does not sound true as strict mode seems pretty critical to me for any serious production environment.

ThaJay avatar Feb 27 '23 14:02 ThaJay

This article is helpful to me. https://medium.com/@wbern/getting-react-18s-strict-mode-to-work-with-react-beautiful-dnd-47bc909348e4

Yes, this works.

immortalt avatar Mar 16 '23 19:03 immortalt

This article is helpful to me. https://medium.com/@wbern/getting-react-18s-strict-mode-to-work-with-react-beautiful-dnd-47bc909348e4

Awesome! It works!

ranlix avatar Mar 28 '23 07:03 ranlix

After some searching, I came across a very helpful snippet by GiovanniACamacho, which was converted to TypeScript by Meligy in that same issue, which resolved my problems and made me able to drag and drop again, with Strict Mode enabled.

import { useEffect, useState } from "react"; import { Droppable, DroppableProps } from "react-beautiful-dnd"; export const StrictModeDroppable = ({ children, ...props }: DroppableProps) => { const [enabled, setEnabled] = useState(false); useEffect(() => { const animation = requestAnimationFrame(() => setEnabled(true)); return () => { cancelAnimationFrame(animation); setEnabled(false); }; }, []); if (!enabled) { return null; } return <Droppable {...props}>{children}</Droppable>; };

minhtrung0110 avatar Jul 10 '23 03:07 minhtrung0110

I switched to https://github.com/hello-pangea/dnd and all issues were solved.

checklist avatar Jul 29 '23 10:07 checklist

If anyone is looking for a less hacky solution, I have a fork of this library @hello-pangea/dnd which natively supports react 18 and react strict mode.

Just replace react-beautiful-dnd with @hello-pangea/dnd and you should be good to go 👍

Tks man you helped me

Chaobanh27 avatar Sep 07 '23 14:09 Chaobanh27