From b3f7d53b87413abdf302c521114e4d77aa92e07f Mon Sep 17 00:00:00 2001 From: michalchudziak Date: Tue, 7 May 2019 02:05:31 -0700 Subject: [PATCH] Fix Animated.Value value after animation if component was re-mounted (#24571) Summary: Fixes https://github.com/facebook/react-native/issues/23712 Currently, It seems like `__nativeAnimatedValueListener` is not listening to the correct `onAnimatedValueUpdate` events if component was re-mounted. In this PR I'm attaching a new listener if the native view tag has changed. [General] [Fixed] - Fixed Animated.Value value after animation if component was re-mounted Pull Request resolved: https://github.com/facebook/react-native/pull/24571 Differential Revision: D15237431 Pulled By: cpojer fbshipit-source-id: 1fe4e290ab45dfe6d1d364d8d7384aabf18d6610 --- Libraries/Animated/src/nodes/AnimatedNode.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Libraries/Animated/src/nodes/AnimatedNode.js b/Libraries/Animated/src/nodes/AnimatedNode.js index f31d5ef354e..b90b4c185b8 100644 --- a/Libraries/Animated/src/nodes/AnimatedNode.js +++ b/Libraries/Animated/src/nodes/AnimatedNode.js @@ -43,6 +43,7 @@ class AnimatedNode { /* Methods and props used by native Animated impl */ __isNative: boolean; __nativeTag: ?number; + __shouldUpdateListenersForNewNativeTag: boolean; constructor() { this._listeners = {}; @@ -104,10 +105,18 @@ class AnimatedNode { } _startListeningToNativeValueUpdates() { - if (this.__nativeAnimatedValueListener) { + if ( + this.__nativeAnimatedValueListener && + !this.__shouldUpdateListenersForNewNativeTag + ) { return; } + if (this.__shouldUpdateListenersForNewNativeTag) { + this.__shouldUpdateListenersForNewNativeTag = false; + this._stopListeningForNativeValueUpdates(); + } + NativeAnimatedAPI.startListeningToAnimatedNodeValue(this.__getNativeTag()); this.__nativeAnimatedValueListener = NativeAnimatedHelper.nativeEventEmitter.addListener( 'onAnimatedValueUpdate', @@ -153,6 +162,7 @@ class AnimatedNode { this.__getNativeConfig(), ); this.__nativeTag = nativeTag; + this.__shouldUpdateListenersForNewNativeTag = true; } return this.__nativeTag; }