Skip to content

Commit

Permalink
RN: Further Optimize createAnimatedComponent
Browse files Browse the repository at this point in the history
Summary:
In addition to memoizing `mergedStyle` in `createAnimatedComponent`, this avoids unnecessary object allocations by:

* Not allocating `passthroughProps`, created via a rest spread operator. It is unnecessary because we always override `style` in the JSX.
* Not allocating a new object if either `style` or `passthroughStyle` are null or undefined. Also, create an array of the two style objects instead of spreading them, which is needless.

Changelog:
[General][Changed] - Improved  performance of `Animated` components

Reviewed By: sammy-SC

Differential Revision: D56621191
  • Loading branch information
yungsters authored and facebook-github-bot committed Apr 26, 2024
1 parent 3fa47d7 commit 93ad288
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import View from '../Components/View/View';
import composeStyles from '../StyleSheet/composeStyles';
import useMergeRefs from '../Utilities/useMergeRefs';
import useAnimatedProps from './useAnimatedProps';
import * as React from 'react';
Expand Down Expand Up @@ -45,17 +46,18 @@ export default function createAnimatedComponent<TProps: {...}, TInstance>(
// without these passthrough values.
// $FlowFixMe[prop-missing]
const {passthroughAnimatedPropExplicitValues, style} = reducedProps;
const {style: passthroughStyle, ...passthroughProps} =
passthroughAnimatedPropExplicitValues ?? {};
const passthroughStyle = passthroughAnimatedPropExplicitValues?.style;
const mergedStyle = useMemo(
() => ({...style, ...passthroughStyle}),
[style, passthroughStyle],
() => composeStyles(style, passthroughStyle),
[passthroughStyle, style],
);

// NOTE: It is important that `passthroughAnimatedPropExplicitValues` is
// spread after `reducedProps` but before `style`.
return (
<Component
{...reducedProps}
{...passthroughProps}
{...passthroughAnimatedPropExplicitValues}
style={mergedStyle}
ref={ref}
/>
Expand Down

0 comments on commit 93ad288

Please sign in to comment.