Skip to content

Commit

Permalink
Use shallowEqualScalar by default
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Apr 2, 2015
1 parent fd700f6 commit 28242c4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
2 changes: 0 additions & 2 deletions examples/_sortable-simple/Card.js
Expand Up @@ -58,8 +58,6 @@ const cardTarget = {
};

export default configureDragDrop(Card, {
arePropsEqual: shallowEqual,

configure: (register) => ({
cardSourceId: register.dragSource(ItemTypes.CARD, cardSource),
cardTargetId: register.dropTarget(ItemTypes.CARD, cardTarget)
Expand Down
6 changes: 0 additions & 6 deletions examples/_sortable-stress/Card.js
Expand Up @@ -23,10 +23,6 @@ const propTypes = {
};

class Card {
shouldComponentUpdate(nextProps) {
return !shallowEqual(nextProps, this.props);
}

render() {
const { text, isDragging, dragSourceRef, dropTargetRef } = this.props;
const opacity = isDragging ? 0 : 1;
Expand Down Expand Up @@ -58,8 +54,6 @@ const cardTarget = {
};

export default configureDragDrop(Card, {
arePropsEqual: shallowEqual,

configure: (register) => ({
cardSourceId: register.dragSource(ItemTypes.CARD, cardSource),
cardTargetId: register.dropTarget(ItemTypes.CARD, cardTarget)
Expand Down
8 changes: 4 additions & 4 deletions modules/configureDragDrop.js
@@ -1,23 +1,23 @@
import React, { Component, PropTypes, findDOMNode } from 'react';
import ComponentDragSource from './ComponentDragSource';
import ComponentDropTarget from './ComponentDropTarget';
import shallowEqualScalar from './utils/shallowEqualScalar';
import assign from 'lodash/object/assign';
import memoize from 'lodash/function/memoize';
import invariant from 'react/lib/invariant';
import shallowEqual from 'react/lib/shallowEqual';

const DEFAULT_KEY = '__default__';

export default function configureDragDrop(InnerComponent, {
configure,
inject,
arePropsEqual = () => false,
arePropsEqual = shallowEqualScalar,
managerName = 'dragDropManager'
}) {
class DragDropContainer extends Component {
shouldComponentUpdate(nextProps, nextState) {
return !arePropsEqual(nextProps, this.props) ||
!shallowEqual(nextState, this.state);
!shallowEqualScalar(nextState, this.state);
}

constructor(props, context) {
Expand Down Expand Up @@ -63,7 +63,7 @@ export default function configureDragDrop(InnerComponent, {

handleChange() {
const nextState = this.getCurrentState();
if (!shallowEqual(nextState, this.state)) {
if (!shallowEqualScalar(nextState, this.state)) {
this.setState(nextState);
}
}
Expand Down
29 changes: 29 additions & 0 deletions modules/utils/shallowEqualScalar.js
@@ -0,0 +1,29 @@
import isBoolean from 'lodash/lang/isBoolean';
import isString from 'lodash/lang/isString';
import isNumber from 'lodash/lang/isNumber';
import isFunction from 'lodash/lang/isFunction';

export default function shallowEqualScalar(objA, objB) {
if (objA === objB) {
return true;
}

var key;
for (key in objA) {
if (objA.hasOwnProperty(key) &&
(!objB.hasOwnProperty(key) ||
objA[key] !== objB[key] ||
typeof objA[key] === 'object' ||
typeof objB[key] === 'object')) {
return false;
}
}

for (key in objB) {
if (objB.hasOwnProperty(key) && !objA.hasOwnProperty(key)) {
return false;
}
}

return true;
}

0 comments on commit 28242c4

Please sign in to comment.