Skip to content

Commit 6f08204

Browse files
authored
fix: Return proper initial styles in PropsFilter for animated style objects (#7385)
## Summary This PR changes the `PropsFilter` behavior for animated styles. Before these changes, initial styles of all animated style objects were merged into a single initial style object. After the first render, the same initial style object was returned for each animated style, so e.g. when we had multiple animated styles, the same initial style object was added to the style array multiple times. Even though the previous implementation worked on Fabric, I decided to change it for consistency with implementation in reanimated `3.x.x`. The new implementation, that returns a correct initial style corresponding to the animated style object, feels better (in terms of logic) as gives more expected results (each animated style is given its own initial style properties, not combined props from all animated styles). The original PR merged to the `3.17-stable` branch: #7376
1 parent 6383433 commit 6f08204

File tree

1 file changed

+12
-8
lines changed
  • packages/react-native-reanimated/src/createAnimatedComponent

1 file changed

+12
-8
lines changed

packages/react-native-reanimated/src/createAnimatedComponent/PropsFilter.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { initialUpdaterRun } from '../animation';
44
import type { StyleProps } from '../commonTypes';
5+
import type { AnimatedStyleHandle } from '../hook/commonTypes';
56
import { isSharedValue } from '../isSharedValue';
67
import { isChromeDebugger } from '../PlatformChecker';
78
import { WorkletEventHandler } from '../WorkletEventHandler';
@@ -21,30 +22,33 @@ function dummyListener() {
2122
}
2223

2324
export class PropsFilter implements IPropsFilter {
24-
private _initialStyle = {};
25+
private _initialPropsMap = new Map<AnimatedStyleHandle, StyleProps>();
2526

2627
public filterNonAnimatedProps(
2728
component: React.Component<unknown, unknown> & IAnimatedComponentInternal
2829
): Record<string, unknown> {
2930
const inputProps =
3031
component.props as AnimatedComponentProps<InitialComponentProps>;
3132
const props: Record<string, unknown> = {};
33+
3234
for (const key in inputProps) {
3335
const value = inputProps[key];
3436
if (key === 'style') {
3537
const styleProp = inputProps.style;
3638
const styles = flattenArray<StyleProps>(styleProp ?? []);
39+
3740
const processedStyle: StyleProps[] = styles.map((style) => {
3841
if (style && style.viewDescriptors) {
39-
// this is how we recognize styles returned by useAnimatedStyle
42+
const handle = style as AnimatedStyleHandle;
43+
4044
if (component._isFirstRender) {
41-
this._initialStyle = {
42-
...style.initial.value,
43-
...this._initialStyle,
44-
...initialUpdaterRun<StyleProps>(style.initial.updater),
45-
};
45+
this._initialPropsMap.set(handle, {
46+
...handle.initial.value,
47+
...initialUpdaterRun(handle.initial.updater),
48+
} as StyleProps);
4649
}
47-
return this._initialStyle;
50+
51+
return this._initialPropsMap.get(handle) ?? {};
4852
} else if (hasInlineStyles(style)) {
4953
return getInlineStyle(style, component._isFirstRender);
5054
} else {

0 commit comments

Comments
 (0)