Skip to content

Commit b186d8f

Browse files
yungstersfacebook-github-bot
authored andcommitted
Animated: Hoist Non-Lifecycle Logic from useAnimatedPropsLifecycle (facebook#48877)
Summary: Pull Request resolved: facebook#48877 This is a minor refactor to hoist logic that is not actually specific to the `AnimatedProps` lifecycle out of the hook named `useAnimatedPropsLifecycle`. This will make it easier to iterate on the implementation of `useAnimatedPropsLifecycle` using a feature flag in a subsequent diff. This has no behavior change. Changelog: [Internal] Reviewed By: javache Differential Revision: D68516034 fbshipit-source-id: 2cd6d9b0f2a5c0ada10cf01c8c14ed5510fbf25a
1 parent 54e0b69 commit b186d8f

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

packages/react-native/Libraries/Animated/useAnimatedProps.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ export default function useAnimatedProps<TProps: {...}, TInstance>(
5858
const useNativePropsInFabric =
5959
ReactNativeFeatureFlags.shouldUseSetNativePropsInFabric();
6060

61+
useEffect(() => {
62+
// If multiple components call `flushQueue`, the first one will flush the
63+
// queue and subsequent ones will do nothing.
64+
NativeAnimatedHelper.API.flushQueue();
65+
let drivenAnimationEndedListener: ?EventSubscription = null;
66+
if (node.__isNative) {
67+
drivenAnimationEndedListener =
68+
NativeAnimatedHelper.nativeEventEmitter.addListener(
69+
'onUserDrivenAnimationEnded',
70+
data => {
71+
node.update();
72+
},
73+
);
74+
}
75+
76+
return () => {
77+
drivenAnimationEndedListener?.remove();
78+
};
79+
});
80+
6181
useAnimatedPropsLifecycle(node);
6282

6383
// TODO: This "effect" does three things:
@@ -232,27 +252,6 @@ function useAnimatedPropsLifecycle(node: AnimatedProps): void {
232252
const prevNodeRef = useRef<?AnimatedProps>(null);
233253
const isUnmountingRef = useRef<boolean>(false);
234254

235-
useEffect(() => {
236-
// It is ok for multiple components to call `flushQueue` because it noops
237-
// if the queue is empty. When multiple animated components are mounted at
238-
// the same time. Only first component flushes the queue and the others will noop.
239-
NativeAnimatedHelper.API.flushQueue();
240-
let drivenAnimationEndedListener: ?EventSubscription = null;
241-
if (node.__isNative) {
242-
drivenAnimationEndedListener =
243-
NativeAnimatedHelper.nativeEventEmitter.addListener(
244-
'onUserDrivenAnimationEnded',
245-
data => {
246-
node.update();
247-
},
248-
);
249-
}
250-
251-
return () => {
252-
drivenAnimationEndedListener?.remove();
253-
};
254-
});
255-
256255
useInsertionEffect(() => {
257256
isUnmountingRef.current = false;
258257
return () => {

0 commit comments

Comments
 (0)