I am working on a drag-and-drop implementation (from scratch without using the DND library) and would like to limit the number of unnecessary updates during drag and drop.
Dragging a "clone" (usually a copy of the original element, but can be an arbitrary placeholder) is achieved by updating the state of the container component ("Clonetainer") and using this to apply the transform. However, it makes no sense to update the entire subtree during the move, as the only change is the container cords.
Here is my solution:
const ClonetainerRenderShield = React.createClass({ shouldComponentUpdate: function (newProps) { return newProps.shouldUpdate; }, render: function () { return this.props.children;
(I will not go into the details of the rest of the DND system, except to say that mouse events from the upstream component provide an offset parameter to Clonetainer.)
The solution I came up with to stop the update included determining whether the Clonetainer update was caused due to a move or for some other reason (and setting this.isMoveEvent accordingly), then smoothing the component between the Clonetainer and its children no more than a shouldComponentUpdate based on the missing prop ( shouldUpdate ).
It works. I tested it in such a way as to show that it is updated when it should and does not update, when it should not, but it looks a bit unnecessary to have a separate gasket component to just block updates from flowing down. Is there a way to indicate that the child component should not be updated from the previous state in render without requiring the child component to include its own shouldComponentUpdate logic?
source share