@@ -1510,9 +1510,9 @@ class AnimatedStyle extends AnimatedWithChildren {
1510
1510
1511
1511
// Recursively get values for nested styles (like iOS's shadowOffset)
1512
1512
__walkStyleAndGetValues(style) {
1513
- let updatedStyle = {};
1514
- for (let key in style) {
1515
- let value = style[key];
1513
+ const updatedStyle = {};
1514
+ for (const key in style) {
1515
+ const value = style[key];
1516
1516
if (value instanceof Animated) {
1517
1517
if (!value.__isNative) {
1518
1518
// We cannot use value of natively driven nodes this way as the value we have access from
@@ -1535,9 +1535,9 @@ class AnimatedStyle extends AnimatedWithChildren {
1535
1535
1536
1536
// Recursively get animated values for nested styles (like iOS's shadowOffset)
1537
1537
__walkStyleAndGetAnimatedValues(style) {
1538
- let updatedStyle = {};
1539
- for (let key in style) {
1540
- let value = style[key];
1538
+ const updatedStyle = {};
1539
+ for (const key in style) {
1540
+ const value = style[key];
1541
1541
if (value instanceof Animated) {
1542
1542
updatedStyle[key] = value.__getAnimatedValue();
1543
1543
} else if (value && !Array.isArray(value) && typeof value === 'object') {
@@ -1690,7 +1690,9 @@ class AnimatedProps extends Animated {
1690
1690
}
1691
1691
1692
1692
setNativeView(animatedView: any): void {
1693
- invariant(this._animatedView === undefined, 'Animated view already set.');
1693
+ if (this._animatedView === animatedView) {
1694
+ return;
1695
+ }
1694
1696
this._animatedView = animatedView;
1695
1697
if (this.__isNative) {
1696
1698
this.__connectAnimatedView();
@@ -1729,7 +1731,9 @@ class AnimatedProps extends Animated {
1729
1731
function createAnimatedComponent(Component: any): any {
1730
1732
class AnimatedComponent extends React.Component {
1731
1733
_component: any;
1734
+ _prevComponent: any;
1732
1735
_propsAnimated: AnimatedProps;
1736
+ _eventDetachers: Array<Function> = [];
1733
1737
_setComponentRef: Function;
1734
1738
1735
1739
constructor(props: Object) {
@@ -1739,7 +1743,7 @@ function createAnimatedComponent(Component: any): any {
1739
1743
1740
1744
componentWillUnmount() {
1741
1745
this._propsAnimated && this._propsAnimated.__detach();
1742
- this._detachNativeEvents(this.props );
1746
+ this._detachNativeEvents();
1743
1747
}
1744
1748
1745
1749
setNativeProps(props) {
@@ -1752,42 +1756,28 @@ function createAnimatedComponent(Component: any): any {
1752
1756
1753
1757
componentDidMount() {
1754
1758
this._propsAnimated.setNativeView(this._component);
1755
-
1756
- this._attachNativeEvents(this.props);
1759
+ this._attachNativeEvents();
1757
1760
}
1758
1761
1759
- _attachNativeEvents(newProps) {
1760
- if (newProps !== this.props) {
1761
- this._detachNativeEvents(this.props);
1762
- }
1763
-
1762
+ _attachNativeEvents() {
1764
1763
// Make sure to get the scrollable node for components that implement
1765
1764
// ` ScrollResponder . Mixin `.
1766
- const ref = this._component.getScrollableNode ?
1765
+ const scrollableNode = this._component.getScrollableNode ?
1767
1766
this._component.getScrollableNode() :
1768
1767
this._component;
1769
1768
1770
- for (const key in newProps ) {
1771
- const prop = newProps [key];
1769
+ for (const key in this.props ) {
1770
+ const prop = this.props [key];
1772
1771
if (prop instanceof AnimatedEvent && prop.__isNative) {
1773
- prop.__attach(ref, key);
1772
+ prop.__attach(scrollableNode, key);
1773
+ this._eventDetachers.push(() => prop.__detach(scrollableNode, key));
1774
1774
}
1775
1775
}
1776
1776
}
1777
1777
1778
- _detachNativeEvents(props) {
1779
- // Make sure to get the scrollable node for components that implement
1780
- // ` ScrollResponder . Mixin `.
1781
- const ref = this._component.getScrollableNode ?
1782
- this._component.getScrollableNode() :
1783
- this._component;
1784
-
1785
- for (const key in props) {
1786
- const prop = props[key];
1787
- if (prop instanceof AnimatedEvent && prop.__isNative) {
1788
- prop.__detach(ref, key);
1789
- }
1790
- }
1778
+ _detachNativeEvents() {
1779
+ this._eventDetachers.forEach(remove => remove());
1780
+ this._eventDetachers = [];
1791
1781
}
1792
1782
1793
1783
_attachProps(nextProps) {
@@ -1820,10 +1810,6 @@ function createAnimatedComponent(Component: any): any {
1820
1810
callback,
1821
1811
);
1822
1812
1823
- if (this._component) {
1824
- this._propsAnimated.setNativeView(this._component);
1825
- }
1826
-
1827
1813
// When you call detach, it removes the element from the parent list
1828
1814
// of children. If it goes to 0, then the parent also detaches itself
1829
1815
// and so on.
@@ -1835,9 +1821,18 @@ function createAnimatedComponent(Component: any): any {
1835
1821
oldPropsAnimated && oldPropsAnimated.__detach();
1836
1822
}
1837
1823
1838
- componentWillReceiveProps(nextProps) {
1839
- this._attachProps(nextProps);
1840
- this._attachNativeEvents(nextProps);
1824
+ componentWillReceiveProps(newProps) {
1825
+ this._attachProps(newProps);
1826
+ }
1827
+
1828
+ componentDidUpdate(prevProps) {
1829
+ if (this._component !== this._prevComponent) {
1830
+ this._propsAnimated.setNativeView(this._component);
1831
+ }
1832
+ if (this._component !== this._prevComponent || prevProps !== this.props) {
1833
+ this._detachNativeEvents();
1834
+ this._attachNativeEvents();
1835
+ }
1841
1836
}
1842
1837
1843
1838
render() {
@@ -1850,6 +1845,7 @@ function createAnimatedComponent(Component: any): any {
1850
1845
}
1851
1846
1852
1847
_setComponentRef(c) {
1848
+ this._prevComponent = this._component;
1853
1849
this._component = c;
1854
1850
}
1855
1851
0 commit comments