From 64bc7cbc288b6190474e96d80a1beade66c7f6da Mon Sep 17 00:00:00 2001 From: Kareem Janoudi Date: Sat, 12 Mar 2016 20:17:11 -0500 Subject: [PATCH 1/3] Reinitialize sortable on componentDidUpdate --- src/index.jsx | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/index.jsx b/src/index.jsx index cd3b5a4..85b86a9 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -105,9 +105,8 @@ const SortableMixin = (options = defaultOptions) => (Component) => class extends }, 0); }; }); - - const domNode = ReactDOM.findDOMNode(sortableComponent.refs[this.sortableOptions.ref] || sortableComponent); - this.sortableInstance = Sortable.create(domNode, copyOptions); + this.populatedOptions = copyOptions + this.initSortable(sortableComponent); } componentWillReceiveProps(nextProps) { const sortableComponent = this.refs[refName]; @@ -120,12 +119,23 @@ const SortableMixin = (options = defaultOptions) => (Component) => class extends sortableComponent.setState(newState); } } + componentDidUpdate() { + this.initSortable(this.refs[refName]); + } componentWillUnmount() { if (this.sortableInstance) { this.sortableInstance.destroy(); this.sortableInstance = null; } } + initSortable(sortableComponent) { + if (this.sortableInstance) { + this.sortableInstance.destroy(); + this.sortableInstance = null; + } + const domNode = ReactDOM.findDOMNode(sortableComponent.refs[this.sortableOptions.ref] || sortableComponent); + this.sortableInstance = Sortable.create(domNode, this.populatedOptions); + } render() { return ( From c334eed4a0a3e5dd33de135ca702b23580c9be4c Mon Sep 17 00:00:00 2001 From: Kareem Janoudi Date: Sat, 12 Mar 2016 20:46:51 -0500 Subject: [PATCH 2/3] redundant code --- src/index.jsx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/index.jsx b/src/index.jsx index 85b86a9..8a27f61 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -123,18 +123,18 @@ const SortableMixin = (options = defaultOptions) => (Component) => class extends this.initSortable(this.refs[refName]); } componentWillUnmount() { - if (this.sortableInstance) { - this.sortableInstance.destroy(); - this.sortableInstance = null; - } + this.destroySortable(); } initSortable(sortableComponent) { + this.destroySortable(); + const domNode = ReactDOM.findDOMNode(sortableComponent.refs[this.sortableOptions.ref] || sortableComponent); + this.sortableInstance = Sortable.create(domNode, this.populatedOptions); + } + destroySortable() { if (this.sortableInstance) { this.sortableInstance.destroy(); this.sortableInstance = null; } - const domNode = ReactDOM.findDOMNode(sortableComponent.refs[this.sortableOptions.ref] || sortableComponent); - this.sortableInstance = Sortable.create(domNode, this.populatedOptions); } render() { From e45d6e578b49b3ec2decfe566ffc1a26d457becc Mon Sep 17 00:00:00 2001 From: Kareem Janoudi Date: Sat, 12 Mar 2016 20:58:24 -0500 Subject: [PATCH 3/3] reduce calls of componentdidUpdate --- src/index.jsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/index.jsx b/src/index.jsx index 8a27f61..20690f9 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -119,8 +119,13 @@ const SortableMixin = (options = defaultOptions) => (Component) => class extends sortableComponent.setState(newState); } } - componentDidUpdate() { - this.initSortable(this.refs[refName]); + componentDidUpdate(prevProps) { + const model = this.sortableOptions.model; + const prevItems = prevProps[model]; + const currItems = this.props[model]; + if(prevItems !== currItems) { + this.initSortable(this.refs[refName]); + } } componentWillUnmount() { this.destroySortable();