React-grid-layout: item contents are overwritten (rerendered) if item state changes

Created on 13 Oct 2016  路  3Comments  路  Source: STRML/react-grid-layout

This more like a question than an issue.

I am working on a widget dashboard project, using DC for charts drawn on the widgets.
Similar to the example I have a createElement function bound to this.state.items (or in my case this.props.layout).

The problem is that when createElement is triggered (for example when toggling static or changing the id of items) it rerenders the anchor div of the widget and overwrites the chart which was drawn as child of it. (for example:
<div id={"content_" + this.props.id} className="widgetContent"> /* chart content is later kept here but gets overwritten on rerender */ </div>)
As most projects will have a similar architecture I am sure there is an easy solution for this problem?

Most helpful comment

Yes, for now this is expected behavior as the actual <Resizable> and <Draggable> component wrappers are removed in this case.

I intend to change this in RGLv2. In the meantime, you can mitigate this by setting a class like .static and using that to remove the resizable handle, and also setting draggableHandle=".matches-nothing" (or any other class that doesn't exist in your component). Then the wrappers will still be there and the component won't reset, but they will not be practically resizable or draggable.

Hacky, I know. But it'll get you there until v2.

All 3 comments

Update:

I tried blocking overwrites by putting the chart placeholder div in a separate class (included in the createElement render jsx) and preventing it from ever updating with:

    shouldComponentUpdate: function() {
        // Let's just never update this component again.
        return false;
    },

Unfortunately, it turns out the element is not updated but gets removed and newly created. @STRML is this expected behavior when toggling items to static or modifying the id of the items?

Yes, for now this is expected behavior as the actual <Resizable> and <Draggable> component wrappers are removed in this case.

I intend to change this in RGLv2. In the meantime, you can mitigate this by setting a class like .static and using that to remove the resizable handle, and also setting draggableHandle=".matches-nothing" (or any other class that doesn't exist in your component). Then the wrappers will still be there and the component won't reset, but they will not be practically resizable or draggable.

Hacky, I know. But it'll get you there until v2.

Thanks it worked! Do you have an ETA on v2 yet?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

victor-unsw picture victor-unsw  路  3Comments

ierhyna picture ierhyna  路  3Comments

moritzschaefer picture moritzschaefer  路  3Comments

lantzh picture lantzh  路  3Comments

hepiyellow picture hepiyellow  路  4Comments