SortableJS / react-sortablejs

React bindings for SortableJS
http://sortablejs.github.io/react-sortablejs/
MIT License
1.98k stars 206 forks source link

Is React-SortableJS 6.1.4 compatible with React 18? #263

Open SVenkat99 opened 1 year ago

SVenkat99 commented 1 year ago

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?

PrabhatO commented 1 year ago

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

Priyanka1794 commented 1 year ago

Facing the same issue..

Heilemann commented 1 year ago

Same.

BenGladman commented 1 year ago

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))} />
artsiadi commented 1 year ago

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!

inkiltie commented 1 year ago

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

IAmVisco commented 1 year ago

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.

etiennelacoursiere commented 1 year ago

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.
BenGladman commented 1 year ago

@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);
        }
      }}
etiennelacoursiere commented 1 year ago

@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?

mikebm commented 1 year ago

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 commented 1 year ago

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...

stefba commented 1 year ago

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

RemLawrence commented 11 months ago

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