react-native-reanimated-carousel: [Android][v4.0.0-canary.17] Carousel crashes the app

Describe the bug On Android when the user tries to scroll the carousel the app crashes and closes itself. Inspecting with Android Studio gives this error (top of the error stack):

java.lang.RuntimeException: Loss of precision during arithmetic conversion: (long) 388.7273063659668
at com.swmansion.reanimated.NativeProxy.performOperations(Native Method)

Maybe is even a bug with the react-native-reanimated, but I tried several versions and I always get the same results.

To Reproduce Steps to reproduce the behavior:

  1. Create a simple Carousel component:
import Carousel from 'react-native-reanimated-carousel';
import {View} from 'react-native';

export function CarouselView() {
  return (
    <View style={{flex: 1}}>
      <Carousel
        ref={ref}
        width={width}
        height={height * 0.8}
        data={[1, 2, 3]}
        mode="parallax"
        modeConfig={{
          parallaxScrollingScale: 0.9,
          parallaxScrollingOffset: 70,
        }}
        pagingEnabled={true}
        snapEnabled={true}
        vertical={false}
        renderItem={({item, index}) => <View index={index}>{item}</View>}
      />
    </View>
  );
};
  1. Render it on an Android device/emulator
  2. Try to scroll left/right
  3. App crashes

Expected behavior The Android app should not crash and the Carousel should scroll.

Versions (please complete the following information):

  • react: v18.3.1
  • react-native: v0.76.1
  • react-native-reanimated: v3.16.1
  • react-native-reanimated-carousel: v4.0.0-canary.17
  • react-native-gesture-handler: v2.20.2

Smartphone (please complete the following information):

  • Device: Pixel 9 [emulator]

Additional context

  • Using the Canary version of react-native-reanimated-carousel

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16

Most upvoted comments

Replace the zIndex variable in node_modules/react-native-reanimated-carousel/src/layouts/parallax.ts

with this:

const zIndex = Math.round(interpolate(value, [-1, 0, 1], [0, size, 0]));

image

Hey @Bekaxp! I’m here to help you with any issues you’re experiencing. I can assist you in solving bugs, answering questions, and becoming a contributor.

A similar issue regarding a “Loss of precision during arithmetic conversion” error has been reported and resolved. The suggested solution involves ensuring that arithmetic operations involving floating-point numbers are correctly handled before casting to a long. You can use Math.round in JavaScript to avoid precision loss. Here’s an example:

const value = 388.7273063659668;
const longValue = Math.round(value); // Use Math.round to avoid precision loss

Additionally, ensure your babel.config.js includes the react-native-reanimated/plugin:

module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: ['react-native-reanimated/plugin'],
  };
};

Make sure your project is using compatible versions of the dependencies:

"dependencies": {
  "react": "18.3.1",
  "react-native": "0.76.1",
  "react-native-reanimated": "3.16.1",
  "react-native-reanimated-carousel": "4.0.0-canary.17",
  "react-native-gesture-handler": "2.20.2"
}

These steps should help resolve the issue and ensure compatibility [1].

To continue talking to Dosu, mention @dosu.

Replace the zIndex variable in node_modules/react-native-reanimated-carousel/src/layouts/parallax.ts

with this:

const zIndex = Math.round(interpolate(value, [-1, 0, 1], [0, size, 0]));

image

This fix works for me. I expect resolved this issue for the future

Faced same issue when using the mode param on IOS mamicrose’s suggestion fixed it for me

Replace the zIndex variable in node_modules/react-native-reanimated-carousel/src/layouts/parallax.ts

with this:

const zIndex = Math.round(interpolate(value, [-1, 0, 1], [0, size, 0]));

image

Replace the zIndex variable in node_modules/react-native-reanimated-carousel/src/layouts/parallax.ts

with this:

const zIndex = Math.round(interpolate(value, [-1, 0, 1], [0, size, 0]));

image

Thank you, this worked!

Hi @SunartoSun, I think the simplest way now would be to use “react-native-reanimated-carousel”: “4.0.0-canary.22”. I think it already includes the fix… try it out. 😃

Otherwise… this was the PR with the fix => https://github.com/dohooo/react-native-reanimated-carousel/pull/716

And you can apply patches easily with yarn => https://yarnpkg.com/features/patching (if you use yarn…) 😃

I had the same issue on android and this solved it. Thank you

Faced same issue when using the mode param on IOS mamicrose’s suggestion fixed it for me

Replace the zIndex variable in node_modules/react-native-reanimated-carousel/src/layouts/parallax.ts with this: const zIndex = Math.round(interpolate(value, [-1, 0, 1], [0, size, 0])); image

const zIndex = Math.round(interpolate(value, [-1, 0, 1], [0, size, 0]));

Thank you it worked for me🙂

Hi everyone, I have encountered with the same problem for “stack” animation. Does anyone have a solution? Thanks.

Version:

  • react: v18.3.1
  • react-native: v0.76.2
  • react-native-reanimated: v3.16.1
  • react-native-reanimated-carousel: v3.5.1
  • react-native-gesture-handler: v2.20.2

Have a look at the discussion in #715 . There is also a PR open to fix this. I tried with the patch from @thibaultcapelli and it worked. So I will happily wait for the PR to be merged and a new canary version to be released 👍 . Thanks.

Hi there! "react-native-reanimated-carousel": "^3.5.1" also has this issue