Sortable icon indicating copy to clipboard operation
Sortable copied to clipboard

React + Redux problem

Open rhoman opened this issue 10 years ago • 11 comments

Hello,

I'm trying to use Sortable in Chrome extension based on React + Redux. It all looks well when I'm dragging list items. But when I save list order to redux store, list got rerendered and the elements I've swpaped are swapped again. So the order in store is correct, but

Do you have any idea to fix this?

rhoman avatar Jul 11 '16 06:07 rhoman

No ideas.

RubaXa avatar Jul 11 '16 06:07 RubaXa

I think I've found where is the problem, but still can't solve it.

I'm giving each element of an unique key. When I swap two elements with Sortable, React doesn't know that keys were swapped and after store changes React re-render new elements in old positions.

It all works great when I use random keys, but I'd really like to avoid that as this way each time I'm re-rendering every element of the list.

rhoman avatar Jul 13 '16 05:07 rhoman

I've had to do the exact same thing as you. Please let me know if you come up with a solution and I will do the same.

loucapo avatar Jul 20 '16 14:07 loucapo

We had the same problem before. We ended up with using onMove, dispatch the action and return false, so it doesn't actually change the dom, it just fires the action, change the store and re-render by react.

dkuang1980 avatar Aug 05 '16 20:08 dkuang1980

@dkuang1980 Can you supply for reducer/action and onMove functions?

MrBlenny avatar Sep 02 '16 12:09 MrBlenny

@MrBlenny

    onMove(e, oe){
          const fromIndex = e.dragged.getAttribute('data-index')
          let toIndex = e.related.getAttribute('data-index') || -1
          const hoverClientY = oe.clientY - e.relatedRect.top
          const hoverMiddleY = (e.relatedRect.bottom - e.relatedRect.top) / 2
          if (toIndex === -1)
            toIndex = 0
          else{
            if (fromIndex - 1 === toIndex && hoverClientY > hoverMiddleY)
              return false

            if (fromIndex + 1 === toIndex && hoverClientY < hoverMiddleY)
              return false

            toIndex = toIndex + (hoverClientY > hoverMiddleY ? 1 : 0)
          }
          that.props.onUpdate(
            fromIndex,
            toIndex
          )
          e.dragged.setAttribute('data-index', toIndex)

          return false
        }

For this to actually work, we used a fork https://github.com/joual/Sortable/commits/master, where the original event is added to onMove.

dkuang1980 avatar Sep 02 '16 13:09 dkuang1980

A simple yet dirty hack is to use this.state = newState inside onEnd callback instead of this.setState(newState). Eslint warning will appear and code reviewer is likely to blow its mind, but it works.

viniciusbo avatar Sep 29 '16 18:09 viniciusbo

Sorry, actually it won't work with Redux, BTW.

viniciusbo avatar Sep 29 '16 18:09 viniciusbo

Help wanted ---> https://github.com/SortableJS/react-sortablejs

RubaXa avatar Feb 16 '17 11:02 RubaXa

major kudos @dkuang1980 . we used a similar solution but also forked sortable to allow it to send all move events and not just events that are moves onto a new element--the default behavior allowed a minor case where an item was dragged onto an item and then back (while being held) to move to the first place it was dragged to.

jmagoon avatar Feb 16 '17 22:02 jmagoon

@RubaXa This issue hasn't been resolved. Do you mind reopening?

meandavejustice avatar Mar 09 '17 18:03 meandavejustice