Skip to content

React integration: multiple controlled grids issues #2094

@dprokop

Description

@dprokop

Hi, I've been struggling a lot trying to get multiple controlled grids working with React. At Grafana we are exploring alternatives to react-grid-layour and gridstack seems interesting.

I've created this demo that sort of solves the issue dprokop#1, but it feels like it's hacky.

A couple of questions from my side:

  1. Is there a way to tell gridstack not to create "phantom" element, when grid item is dropped from one grid to another? I've solved it by identifying the grid items by <gridId>:<itemId> and removing the phantom one manually by using the removeWidget method.
  2. The removeAll in the batch update in react demo that you provide triggers the removed event on the grid. It makes it impossible to change the state of the grid from which the item is removed. Basically what I want to do is to remove item from the state when it's moved from one grid to another, and then re-initialize gridstack. To achieve that in my demo I'm relying on a silent removeWidget call again: https://github.com/dprokop/gridstack.js/pull/1/files#diff-857613860ab6ab3d2b25db0b15493cce1fb0fcf3a188fdf199ed129b1d6e6965R86 . The demo when performed on code sandbox throws a React error related to the underlying dom being modified by gridstack (basically widget dom element is removed and React goes funky with it). Interestingly enough the demo does not suffer from this when run locally (try https://github.com/dprokop/gridstack.js/tree/dprokop/multi-grid-react)
  3. When moving items from one grid to another at some point the grid container starts growing in a weird way - can you provide some insights on why this may be happening? I went through the code but couldn't figure this out yet. The phenomenon is visible on the attached video of my demo. Looking into the generated html it seems like gs-current-row is being increased with every interaction.
Screen.Recording.2022-11-03.at.14.37.38.mov
  1. Do you have any suggestions on how to reliably destroy gridstack instances when react component is unmounted? I've tried to do it the legitimate react way (via useEffect return callback) but unfortunately, this is basically breaking gridstack all together with the follwing error:
gridstack.ts:1240 Uncaught TypeError: Cannot set properties of undefined (setting 'removedNodes')
    at a._triggerRemoveEvent (gridstack.ts:1240:19)
    at dd-gridstack.ts:339:15
    at Object.drop (dd-gridstack.ts:134:9)
    at l.triggerEvent (dd-base-impl.ts:40:34)
    at l.drop (dd-droppable.ts:146:10)
    at l._mouseUp (dd-draggable.ts:245:31)
_triggerRemoveEvent @ gridstack.ts:1240
(anonymous) @ dd-gridstack.ts:339
(anonymous) @ dd-gridstack.ts:134
triggerEvent @ dd-base-impl.ts:40
drop @ dd-droppable.ts:146
_mouseUp @ dd-draggable.ts:245
dd-gridstack.ts:598 Uncaught TypeError: Cannot read properties of undefined (reading 'removeNode')
    at o.GridStack._leave (dd-gridstack.ts:598:15)
    at dd-gridstack.ts:257:19
    at Object.dropover (dd-gridstack.ts:134:9)
    at l.triggerEvent (dd-base-impl.ts:40:34)
    at l._mouseEnter (dd-droppable.ts:104:10)

Steps to reproduce

Partially working demo: https://codesandbox.io/s/blazing-browser-6dqptj?file=/src/App.js
You can also run https://github.com/dprokop/gridstack.js/tree/dprokop/multi-grid-react which even though is the same as above, it works better ;)

I would appreciate any help from your side!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions