Skip to content

Commit f52acd2

Browse files
committed
Revert shared Animated.Value amongst transitions
This reverts commit 82a2ba2. Found to cause cascading re-renders which can delay app startup.
1 parent 2cd7c79 commit f52acd2

File tree

1 file changed

+29
-82
lines changed

1 file changed

+29
-82
lines changed

packages/react-strict-dom/src/native/modules/useStyleTransition.js

Lines changed: 29 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ type TransitionMetadata = $ReadOnly<{
3030
shouldUseNativeDriver: boolean
3131
}>;
3232

33-
type AnimatedConfig = {
34-
start: () => void,
35-
dispose: () => void,
36-
value: ReactNative.Animated.Value,
37-
referenceCount: number
38-
};
39-
4033
const INPUT_RANGE: $ReadOnlyArray<number> = [0, 1];
4134

4235
function isNumber(num: mixed): num is number {
@@ -268,53 +261,6 @@ function getAnimation(
268261
});
269262
}
270263

271-
const animatedConfigs = new Map<string, AnimatedConfig>();
272-
273-
function getOrCreateAnimatedConfig(transitionMetadata: TransitionMetadata) {
274-
const key = JSON.stringify(transitionMetadata);
275-
276-
const animatedConfig = animatedConfigs.get(key);
277-
if (animatedConfig != null) {
278-
animatedConfig.referenceCount++;
279-
280-
return animatedConfig;
281-
}
282-
283-
const animatedValue = new ReactNative.Animated.Value(0);
284-
let hasStarted = false;
285-
let animation;
286-
const newAnimatedConfig = {
287-
referenceCount: 1,
288-
value: animatedValue,
289-
start: () => {
290-
if (hasStarted) {
291-
return;
292-
}
293-
hasStarted = true;
294-
const { delay, duration, timingFunction, shouldUseNativeDriver } =
295-
transitionMetadata;
296-
animation = ReactNative.Animated.sequence([
297-
ReactNative.Animated.delay(delay),
298-
getAnimation(
299-
animatedValue,
300-
duration,
301-
timingFunction,
302-
shouldUseNativeDriver
303-
)
304-
]);
305-
animation.start();
306-
},
307-
dispose: () => {
308-
if (--newAnimatedConfig.referenceCount === 0) {
309-
animation?.stop();
310-
}
311-
}
312-
};
313-
animatedConfigs.set(key, newAnimatedConfig);
314-
315-
return newAnimatedConfig;
316-
}
317-
318264
export function useStyleTransition(style: ReactNativeStyle): ReactNativeStyle {
319265
const {
320266
transitionDelay: _delay,
@@ -344,9 +290,8 @@ export function useStyleTransition(style: ReactNativeStyle): ReactNativeStyle {
344290
React.useState<ReactNativeStyle | void>(style);
345291
const [previousStyle, setPreviousStyle] =
346292
React.useState<ReactNativeStyle | void>(undefined);
347-
348-
const [animatedConfig, setAnimatedConfig] =
349-
React.useState<AnimatedConfig | void>(undefined);
293+
const [animatedValue, setAnimatedValue] =
294+
React.useState<ReactNative.Animated.Value | void>(undefined);
350295

351296
// This ref is utilized as a performance optimization so that the effect that contains the
352297
// animation trigger only is called when the animated value's identity changes. As far as the effect
@@ -373,32 +318,28 @@ export function useStyleTransition(style: ReactNativeStyle): ReactNativeStyle {
373318
]);
374319

375320
// effect to trigger a transition
376-
// REMEMBER: it is super important that this effect's dependency array **only** contains the animated config
321+
// REMEMBER: it is super important that this effect's dependency array **only** contains the animated value
377322
React.useEffect(() => {
378-
if (animatedConfig == null) {
379-
return;
380-
}
381-
animatedConfig.start();
382-
animatedConfigs.clear();
383-
return () => {
384-
animatedConfig.dispose();
385-
};
386-
}, [animatedConfig]);
323+
if (animatedValue !== undefined) {
324+
const { delay, duration, timingFunction, shouldUseNativeDriver } =
325+
transitionMetadataRef.current;
387326

388-
const transitionStyleHasChangedResult = transitionStyleHasChanged(
389-
transitionStyle,
390-
currentStyle
391-
);
392-
393-
React.useLayoutEffect(() => {
394-
if (transitionStyleHasChangedResult) {
395-
setCurrentStyle(style);
396-
setPreviousStyle(currentStyle);
397-
setAnimatedConfig(
398-
getOrCreateAnimatedConfig(transitionMetadataRef.current)
399-
);
327+
const animation = ReactNative.Animated.sequence([
328+
ReactNative.Animated.delay(delay),
329+
getAnimation(
330+
animatedValue,
331+
duration,
332+
timingFunction,
333+
shouldUseNativeDriver
334+
)
335+
]);
336+
animation.start();
337+
338+
return () => {
339+
animation.stop();
340+
};
400341
}
401-
}, [currentStyle, style, transitionStyleHasChangedResult]);
342+
}, [animatedValue]);
402343

403344
if (
404345
_delay == null &&
@@ -410,11 +351,17 @@ export function useStyleTransition(style: ReactNativeStyle): ReactNativeStyle {
410351
return style;
411352
}
412353

413-
if (transitionStyle === undefined) {
354+
if (transitionStyleHasChanged(transitionStyle, currentStyle)) {
355+
setCurrentStyle(style);
356+
setPreviousStyle(currentStyle);
357+
setAnimatedValue(new ReactNative.Animated.Value(0));
358+
// This commit will be thrown away due to the above state setters so we can bail out early
414359
return style;
415360
}
416361

417-
const animatedValue = animatedConfig?.value;
362+
if (transitionStyle === undefined) {
363+
return style;
364+
}
418365

419366
const outputAnimatedStyle: AnimatedStyle = Object.entries(
420367
transitionStyle

0 commit comments

Comments
 (0)