Skip to content

Commit

Permalink
Remove all listeners of AnimatedNode when detached
Browse files Browse the repository at this point in the history
Summary:
changelog: [internal]

Removing listener on detached node leads to a red box, if the said node is `DiffClampAnimatedNode`. This is because calling `AnimatedNode.__getNativeTag()` makes native module call and creates node in native. This node is not completely initialised and red boxes because `[RCTAnimatedNode parentNodes]` is nil when it shouldn't be.

The fix is make sure all listeners are removed before node is destroyed. It is logically correct thing to do. The fix is global, for all components using `DiffClampAnimatedNode`.

Reviewed By: yungsters

Differential Revision: D40381895

fbshipit-source-id: 4f558faf8101b70552f30e6360998e902aacbc83
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Oct 17, 2022
1 parent b0da349 commit cd83194
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Libraries/Animated/nodes/AnimatedNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import type {PlatformConfig} from '../AnimatedPlatformConfig';

import ReactNativeFeatureFlags from '../../ReactNative/ReactNativeFeatureFlags';
import NativeAnimatedHelper from '../NativeAnimatedHelper';
import invariant from 'invariant';

Expand All @@ -29,6 +30,9 @@ export default class AnimatedNode {
__nativeAnimatedValueListener: ?any;
__attach(): void {}
__detach(): void {
if (ReactNativeFeatureFlags.removeListenersOnDetach()) {
this.removeAllListeners();
}
if (this.__isNative && this.__nativeTag != null) {
NativeAnimatedHelper.API.dropAnimatedNode(this.__nativeTag);
this.__nativeTag = undefined;
Expand Down
3 changes: 3 additions & 0 deletions Libraries/ReactNative/ReactNativeFeatureFlags.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export type FeatureFlags = {|
* to render react components driven by classes written in C++.
*/
enableCppRenderSystem: () => boolean,

removeListenersOnDetach: () => boolean,
|};

const ReactNativeFeatureFlags: FeatureFlags = {
Expand All @@ -52,6 +54,7 @@ const ReactNativeFeatureFlags: FeatureFlags = {
animatedShouldDebounceQueueFlush: () => false,
animatedShouldUseSingleOp: () => false,
enableCppRenderSystem: () => false,
removeListenersOnDetach: () => false,
};

module.exports = ReactNativeFeatureFlags;

0 comments on commit cd83194

Please sign in to comment.