react-native-reanimated icon indicating copy to clipboard operation
react-native-reanimated copied to clipboard

Performance issue / Infinite rendering loop

Open Titozzz opened this issue 1 year ago • 5 comments

Description

When rendering that simple svg with an animated line, CPU/Memory allocations go crazy. Unsure what's happening

import {
  useEffect,
  useLayoutEffect,
  useRef,
  useState,
  type FunctionComponent,
} from 'react';
import { StyleSheet, View } from 'react-native';
import Animated, {
  useAnimatedProps,
  useSharedValue,
  withTiming,
} from 'react-native-reanimated';
import Svg, { Line } from 'react-native-svg';

const GAP = 8;

const animationConfigThinSlider = {
  duration: 1000,
};

const AnimatedLine = Animated.createAnimatedComponent(Line);

const ProgressBarWrapper = ({
  containerWidth,
  initialStep,
  currentStep,
  steps,
}: {
  containerWidth: number;
  initialStep?: number;
  currentStep: number;
  steps: number;
}) => {
  const initialWidth = containerWidth * ((initialStep || 0) / steps);
  const animatedWidth = useSharedValue<number>(initialWidth);

  const barWidth = containerWidth * (currentStep / steps);

  useEffect(() => {
    animatedWidth.set(withTiming(barWidth, animationConfigThinSlider));
  }, [animatedWidth, barWidth]);

  const animatedProps = useAnimatedProps(() => {
    return {
      x2: animatedWidth.get(),
    };
  });

  return (
    <Svg width={containerWidth} height={8}>
      <Line
        x1={2}
        y1={4}
        x2={containerWidth}
        y2={4}
        stroke="blue"
        strokeDasharray={`${(containerWidth - GAP * steps) / steps} ${GAP}`}
        strokeLinecap="round"
        strokeWidth={4}
      />

      <AnimatedLine
        animatedProps={animatedProps}
        x1={2}
        y1={4}
        x2={initialWidth}
        y2={4}
        stroke="red"
        strokeDasharray={`${(containerWidth - GAP * steps) / steps} ${GAP}`}
        strokeLinecap="round"
        strokeWidth={4}
      />
    </Svg>
  );
};

type ProgressBarProps = {
  steps: number;
  currentStep: number;
  color?: string;
  initialStep?: number;
};

const ProgressBar: FunctionComponent<ProgressBarProps> = ({
  steps,
  currentStep,
  color,
  initialStep,
}) => {
  const [containerWidth, setContainerWidth] = useState(0);
  const containerRef = useRef<View | null>(null);

  useLayoutEffect(() => {
    if (containerRef.current) {
      containerRef.current.measure((_, __, width) => {
        setContainerWidth(width);
      });
    }
  }, []);

  return (
    <View ref={containerRef} style={styles.container}>
      {containerRef.current && (
        <ProgressBarWrapper
          containerWidth={containerWidth}
          initialStep={initialStep}
          currentStep={currentStep}
          steps={steps}
          color={color}
        />
      )}
    </View>
  );
};

const App = () => {
  return (
    <View style={styles.container}>
      <ProgressBar steps={4} currentStep={1} />
    </View>
  );
};

export default App;

const styles = StyleSheet.create({
  container: {
    flexGrow: 1,
    width: '100%',
    justifyContent: 'center',
    alignItems: 'center',
  },
  text: {
    color: '#fff',
  },
});
  "dependencies": {
    "@react-native/new-app-screen": "0.82.1",
    "react": "19.1.1",
    "react-native": "0.82.1",
    "react-native-reanimated": "^4.1.5",
    "react-native-safe-area-context": "^5.5.2",
    "react-native-svg": "^15.15.0",
    "react-native-worklets": "^0.6.1"
  },

https://github.com/user-attachments/assets/67dc2cd8-d3c6-4703-bc7c-9f8474469351

Steps to reproduce

Use the App.tsx and package.json from above

Feel free to comment the animatedProps line so on/off the issue

Snack or a link to a repository

Not relevant // Provided App.tsx

Reanimated version

4.1.5

Worklets version

0.6.1

React Native version

0.82.1

Platforms

iOS

JavaScript runtime

None

Workflow

None

Architecture

New Architecture (Fabric renderer)

Reanimated feature flags

No

React Native release level

Stable

Build type

Debug app & dev bundle

Device

iOS simulator

Host machine

macOS

Device model

No response

Acknowledgements

No

Titozzz avatar Dec 01 '25 15:12 Titozzz

Hey! 👋

The issue doesn't seem to contain a minimal reproduction.

Could you provide a snack or a link to a GitHub repository under your username that reproduces the problem?

github-actions[bot] avatar Dec 01 '25 15:12 github-actions[bot]

cc @tomekzaw, (& @davzhardy for internal follow up)

Titozzz avatar Dec 01 '25 15:12 Titozzz

Yes, same issue. Try to useAnimatedProps on SVG ( circular progress ) - more then one sample on screen - freezes app

in4core avatar Dec 01 '25 16:12 in4core

Can you reproduce it on Android ?

in4core avatar Dec 01 '25 16:12 in4core

Fixed by react-native-svg 15.15.1. The previous version introduced some huge regression. See https://github.com/software-mansion/react-native-svg/issues/2831.

Also, this is fixed by the new feature flag of reanimated https://github.com/software-mansion/react-native-reanimated/blob/9895af6df6722fbfc8dfd426149cac0adc85d2d5/packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.cpp#L73-L82 that would escape the issue.

Titozzz avatar Dec 01 '25 16:12 Titozzz