Skip to content

moveOnStartChange causes unnecessary state updates #72

@jmuerle

Description

@jmuerle

I'm using react-draggable in my application to scroll an item in a viewport (i.e. dragging the background scrolls the view), and also for custom scrollbars around the viewport. Needless to say, I needed to use the moveOnStartChange attribute to keep these two manners of scrolling the same viewport consistent. I found, however, that there was some really buggy behavior in react-draggable, and it stemmed from the fact that componentWillReceiveProps triggers too often.

If you declare your component like this:

<Draggable start={{x: 0, y: 0}}>...</Draggable>

then every time the render() method is called, the componentWillReceiveProps method is called because, technically, a new object is being passed as the 'start' property.

The solution I have come up with is to change lines 471-473 in the componentWillReceiveProps method to:

if (newProps.moveOnStartChange && newProps.start &&
    (newProps.start.x !== this.props.start.x || newProps.start.y !== this.props.start.y))
{
    this.setState(this.getInitialState(newProps));
}

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions