react-sortablejs icon indicating copy to clipboard operation
react-sortablejs copied to clipboard

Is React-SortableJS 6.1.4 compatible with React 18?

Open SVenkat99 opened this issue 2 years ago • 14 comments

I have tried implementing this package (6.1.4) with React 18, and it looks like the state is not updated property after drop, if the approach is nested. Can someone please have a look on this?

SVenkat99 avatar Dec 08 '22 16:12 SVenkat99

Facing the same issue with package(6.1.4) with react 18, state not updating in case of nested structure

PrabhatO avatar Dec 09 '22 11:12 PrabhatO

Facing the same issue..

Priyanka1794 avatar Dec 15 '22 11:12 Priyanka1794

Same.

Heilemann avatar Dec 30 '22 02:12 Heilemann

I've found that this problem is due to automatic batching. You can work around it by wrapping the setList function in flushSync:

import { flushSync } from 'react-dom';

...then...

<ReactSortable setList={(items) => flushSync(() => onChange(items))} />

BenGladman avatar Jan 10 '23 13:01 BenGladman

I've found that this problem is due to automatic batching. You can work around it by wrapping the setList function in flushSync:

import { flushSync } from 'react-dom';

...then...

<ReactSortable setList={(items) => flushSync(() => onChange(items))} />

Hi, this fix did not work for us (and we get warnings in the console for using flushSync inside a lifecycle method). Seems like the component state is not updated.

Did someone find a workaround? Thanks!

artsiadi avatar Jan 31 '23 09:01 artsiadi

Same issue. Unusable on React 18 and flushSync do not solve anything for my project. Any updates on React 18 compatibility?

inkiltie avatar Feb 13 '23 01:02 inkiltie

Dropping this on top, in React 18 StrictMode the animations are really bugged for me during the development but work fine in the production build. I didn't not suspect this at first and spent quite some time trying to fix the animations only to turn off them in the end and accidentally find out that they work fine on the production build.

IAmVisco avatar Mar 12 '23 19:03 IAmVisco

I've found that this problem is due to automatic batching. You can work around it by wrapping the setList function in flushSync:

import { flushSync } from 'react-dom';

...then...

<ReactSortable setList={(items) => flushSync(() => onChange(items))} />

This worked for us but seems precarious.. we get some errors in the console.

flushSync was called from inside a lifecycle method. React cannot flush when React is already rendering. Consider moving this call to a scheduler task or micro task.

etiennelacoursiere avatar Apr 14 '23 15:04 etiennelacoursiere

@etiennelacoursiere I changed it to this later on, which I think fixes that problem:

      setList={(newSteps, sortable) => {
        if (sortable) {
          // flushSync required for this to work in React 18
          flushSync(() => onChange(newSteps));
        } else {
          // called from component constructor, where flushSync throws error
          onChange(newSteps);
        }
      }}

BenGladman avatar Apr 14 '23 15:04 BenGladman

@etiennelacoursiere I changed it to this later on, which I think fixes that problem:

      setList={(newSteps, sortable) => {
        if (sortable) {
          // flushSync required for this to work in React 18
          flushSync(() => onChange(newSteps));
        } else {
          // called from component constructor, where flushSync throws error
          onChange(newSteps);
        }
      }}

hmm, yeah It got rid of the errors I had in the console, but can't say I understand why. Care to explain?

etiennelacoursiere avatar Apr 14 '23 15:04 etiennelacoursiere

Same issue here, busted in react 18. The drop never works in development builds, but production builds it works fine. flushSync did not fix it for me either.

mikebm avatar Apr 26 '23 23:04 mikebm

I did get it to somewhat work - Adding the prop group="grouped" seemed to make things behave. Sometimes it feels like I have to drag upwards to get it to start working. But it made it somewhat usable...

mikebm avatar Apr 27 '23 23:04 mikebm

I am also having issues. Does someone know of a different package with the same functionality?

s-barsch avatar May 31 '23 15:05 s-barsch

Same issue. Passing 'dropBubble' to ReactSortable resolved it for me.

RemLawrence avatar Jul 12 '23 17:07 RemLawrence