Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot access 'computeOffsetIfDataChanged' before initialization #729

Open
AshishAcharya083 opened this issue Nov 28, 2024 · 10 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@AshishAcharya083
Copy link

AshishAcharya083 commented Nov 28, 2024

`import { Text, View, StyleSheet, FlatList, Dimensions } from 'react-native';
import { dummyHomePageTopCard } from '../../data/dummyData';
import Carousel from 'react-native-reanimated-carousel';

import ActionButtonCard, {
ActionButtonCardsProps
} from '../cards/ActionButtonCard';

export function ActionButtonCardsList() {
const width = Dimensions.get('window').width;

return (
<View style={{ flex: 1 }}>
<Carousel
loop
width={200}
height={300}
// autoPlay={true}
data={dummyHomePageTopCard}
scrollAnimationDuration={1000}
onSnapToItem={(index) => console.log('current index:', index)}
renderItem={({ item }) => {
return (

);
}}
/>

);
}

export default ActionButtonCardsList;

const styles = StyleSheet.create({
listStyle: {
width: '100%',
marginTop: 30
}
});

`

Describe the bug
A clear and concise description of what the bug is. If you intend to submit a PR for this issue, tell us in the description. Thanks!

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Versions (please complete the following information):

  • react: v0.0.0
  • react-native: v0.0.0
  • react-native-reanimated: v0.0.0
  • react-native-reanimated-carousel: v0.0.0
  • react-native-gesture-handler: v0.0.0

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

@AshishAcharya083 AshishAcharya083 added the bug Something isn't working label Nov 28, 2024
Copy link

dosubot bot commented Nov 28, 2024

Hey @AshishAcharya083! I'm here to help you with any bugs or questions you might have while waiting for a human maintainer. Let me know how I can assist you!

I couldn't find a specific solution to the "Cannot access 'computeOffsetIfDataChanged' before initialization" error in the repository. However, this type of error in JavaScript or React Native typically occurs when you try to use a variable or function before it has been declared or initialized.

Here are some general steps you can take to troubleshoot and potentially resolve this issue:

  1. Check Imports and Declarations: Ensure that computeOffsetIfDataChanged is correctly imported and declared before it is used in your code. If it's a function from the library, make sure the library is properly installed and imported.

  2. Initialization Order: Verify that any variables or functions are initialized before they are used. This might involve checking the order of your code execution.

  3. Library Configuration: Ensure that all necessary configurations for react-native-reanimated-carousel and its dependencies are correctly set up. This includes following the installation instructions for react-native-reanimated and react-native-gesture-handler [1].

  4. Debugging: Use console logs or a debugger to trace where the error occurs and check the state of your variables at that point.

If these steps do not resolve the issue, you might want to look into similar issues that have been reported. For instance, there is an open issue where a similar problem was noted, and it was suggested that the problem might not occur when the debugger is not in use [2]. Additionally, checking the configuration of react-native-reanimated was suggested in another closed issue [3].

To continue talking to Dosu, mention @dosu.

@jaedub8
Copy link

jaedub8 commented Dec 8, 2024

I am using v4.0.0-canary.22 and this is happening to me as well, but only in a web browser. The same code runs fine on a physical iOS device. My code is very simple, using expo navigation template with the carousel example from the usage section.

@CaptainJeff
Copy link

I'm getting ths issue as well on my iPhone

@sunxyw
Copy link

sunxyw commented Dec 15, 2024

any updates?

@ryancircelli
Copy link

Its some kind of build issue where in the compiled files the exports are above the definitions causing errors.

@chmorgan
Copy link

chmorgan commented Jan 5, 2025

Seeing the same here with expo 52, react-native 0.76.5 (basically the latest as of a week ago)

Tested with 4.0.0-canary.22, 21, 20, and alpha.10, the offending function changed at some point in that sequence.

Any thoughts on how to resolve?

@metacop
Copy link

metacop commented Jan 12, 2025

Seeing the same here with expo 52, react-native 0.76.5 (basically the latest as of a week ago)

Tested with 4.0.0-canary.22, 21, 20, and alpha.10, the offending function changed at some point in that sequence.

Any thoughts on how to resolve?

it seems that everything is due to 4.0.0.0-canary.22, we have to wait for them to resolve it to be able to test on the web.

@TheForgetTime
Copy link

There is a way to temporarily fix this bug.

run node scripts/fix-reanimated-carousel.js on you project root

the file "scripts/fix-reanimated-carousel.js" with this code:

const fs = require('fs');
const path = require('path');

const rootPath = path.resolve(__dirname, "..");

const data = [
  {
    filePath: "node_modules/react-native-reanimated-carousel/lib/commonjs/utils/compute-offset-if-data-changed.js",
    targetLines: [
      "exports.computeOffsetIfDataChanged = computeOffsetIfDataChanged;",
      "exports.omitZero = omitZero;"
    ]
  },
  {
    filePath: "node_modules/react-native-reanimated-carousel/lib/commonjs/utils/compute-offset-if-size-changed.js",
    targetLines: [
      "exports.computeOffsetIfSizeChanged = computeOffsetIfSizeChanged;",
    ]
  },
  {
    filePath: "node_modules/react-native-reanimated-carousel/lib/commonjs/utils/handleroffset-direction.js",
    targetLines: [
      "exports.handlerOffsetDirection = handlerOffsetDirection;",
    ]
  },
  {
    filePath: "node_modules/react-native-reanimated-carousel/lib/commonjs/utils/computed-with-auto-fill-data.js",
    targetLines: [
      "exports.computedFillDataWithAutoFillData = computedFillDataWithAutoFillData;",
      "exports.computedOffsetXValueWithAutoFillData = computedOffsetXValueWithAutoFillData;",
      "exports.computedRealIndexWithAutoFillData = computedRealIndexWithAutoFillData;",
      "exports.convertToSharedIndex = convertToSharedIndex;",
    ]
  },
  {
    filePath: "node_modules/react-native-reanimated-carousel/lib/commonjs/utils/deal-with-animation.js",
    targetLines: [
      "exports.dealWithAnimation = dealWithAnimation;",
    ]
  },
  {
    filePath: "node_modules/react-native-reanimated-carousel/lib/commonjs/utils/log.js",
    targetLines: [
      "exports.log = log;",
      "exports.round = round;",
    ]
  }
]

function processFile(filePath, targetLines) {
  fs.readFile(filePath, 'utf8', (err, data) => {
    if (err) {
      console.error('failed to read file:', err);
      return;
    }

    const lines = data.split('\n');
    targetLines.forEach((targetLine) => {
      console.log("processLine", targetLine);
      let targetIndex = -1;
      lines.forEach((line, index) => {
        if (line.trim() === targetLine) {
          targetIndex = index;
        }
      });

      if (targetIndex !== -1) {
        lines.splice(targetIndex, 1);
        lines.push(targetLine);
      } else {
        console.log('not found target line:', targetLine);
      }
    })

    const newData = lines.join('\n');
    fs.writeFile(filePath, newData, 'utf8', err => {
      if (err) {
        console.error('failed to write file:', err);
      } else {
        console.log('file updated!');
      }
    });
  });
}

for (let item of data) {
  const filePath = path.join(rootPath, item.filePath);
  processFile(filePath, item.targetLines);
}

@CaptainJeff
Copy link

@TheForgetTime thanks. I'll give this a try later. I also noticed this fixes the issue as well. Adding this to the metro.config.js

config.resolver.resolveRequest = (context, moduleName, platform) => {
  if (moduleName === "react-native-reanimated-carousel") {
    return {
      filePath: path.resolve(
        __dirname,
        "node_modules/react-native-reanimated-carousel/lib/module/index.js"
      ),
      type: "sourceFile"
    }
  }

@rkahle
Copy link

rkahle commented Jan 21, 2025

Using @TheForgetTime's script from above, I created a patch with patch-package.

"patches/react-native-reanimated-carousel+4.0.0-canary.22.patch":

diff --git a/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/compute-offset-if-data-changed.js b/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/compute-offset-if-data-changed.js
index 6741689..7fb4c04 100644
--- a/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/compute-offset-if-data-changed.js
+++ b/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/compute-offset-if-data-changed.js
@@ -3,8 +3,6 @@
 Object.defineProperty(exports, "__esModule", {
   value: true
 });
-exports.computeOffsetIfDataChanged = computeOffsetIfDataChanged;
-exports.omitZero = omitZero;
 
 function omitZero(a, b) {
   "worklet";
@@ -50,3 +48,5 @@ function computeOffsetIfDataChanged(params) {
   return handlerOffset;
 }
 //# sourceMappingURL=compute-offset-if-data-changed.js.map
+exports.computeOffsetIfDataChanged = computeOffsetIfDataChanged;
+exports.omitZero = omitZero;
\ No newline at end of file
diff --git a/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/compute-offset-if-size-changed.js b/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/compute-offset-if-size-changed.js
index 42a4eca..c589f64 100644
--- a/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/compute-offset-if-size-changed.js
+++ b/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/compute-offset-if-size-changed.js
@@ -3,7 +3,6 @@
 Object.defineProperty(exports, "__esModule", {
   value: true
 });
-exports.computeOffsetIfSizeChanged = computeOffsetIfSizeChanged;
 
 function computeOffsetIfSizeChanged(params) {
   "worklet";
@@ -16,3 +15,4 @@ function computeOffsetIfSizeChanged(params) {
   return handlerOffset / prevSize * size;
 }
 //# sourceMappingURL=compute-offset-if-size-changed.js.map
+exports.computeOffsetIfSizeChanged = computeOffsetIfSizeChanged;
\ No newline at end of file
diff --git a/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/computed-with-auto-fill-data.js b/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/computed-with-auto-fill-data.js
index 0d84fba..174ef4d 100644
--- a/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/computed-with-auto-fill-data.js
+++ b/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/computed-with-auto-fill-data.js
@@ -3,10 +3,6 @@
 Object.defineProperty(exports, "__esModule", {
   value: true
 });
-exports.computedFillDataWithAutoFillData = computedFillDataWithAutoFillData;
-exports.computedOffsetXValueWithAutoFillData = computedOffsetXValueWithAutoFillData;
-exports.computedRealIndexWithAutoFillData = computedRealIndexWithAutoFillData;
-exports.convertToSharedIndex = convertToSharedIndex;
 
 var _constants = require("../constants");
 
@@ -122,3 +118,7 @@ function computedFillDataWithAutoFillData(params) {
   return data;
 }
 //# sourceMappingURL=computed-with-auto-fill-data.js.map
+exports.computedFillDataWithAutoFillData = computedFillDataWithAutoFillData;
+exports.computedOffsetXValueWithAutoFillData = computedOffsetXValueWithAutoFillData;
+exports.computedRealIndexWithAutoFillData = computedRealIndexWithAutoFillData;
+exports.convertToSharedIndex = convertToSharedIndex;
\ No newline at end of file
diff --git a/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/deal-with-animation.js b/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/deal-with-animation.js
index 2851db7..8050ada 100644
--- a/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/deal-with-animation.js
+++ b/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/deal-with-animation.js
@@ -3,7 +3,6 @@
 Object.defineProperty(exports, "__esModule", {
   value: true
 });
-exports.dealWithAnimation = dealWithAnimation;
 
 var _reactNativeReanimated = require("react-native-reanimated");
 
@@ -19,3 +18,4 @@ function dealWithAnimation(withAnimation) {
   }
 }
 //# sourceMappingURL=deal-with-animation.js.map
+exports.dealWithAnimation = dealWithAnimation;
\ No newline at end of file
diff --git a/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/handleroffset-direction.js b/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/handleroffset-direction.js
index e72e598..7d681a6 100644
--- a/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/handleroffset-direction.js
+++ b/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/handleroffset-direction.js
@@ -3,7 +3,6 @@
 Object.defineProperty(exports, "__esModule", {
   value: true
 });
-exports.handlerOffsetDirection = handlerOffsetDirection;
 
 function handlerOffsetDirection(handlerOffset, fixedDirection) {
   "worklet";
@@ -14,3 +13,4 @@ function handlerOffsetDirection(handlerOffset, fixedDirection) {
   return Math.sign(handlerOffset.value);
 }
 //# sourceMappingURL=handleroffset-direction.js.map
+exports.handlerOffsetDirection = handlerOffsetDirection;
\ No newline at end of file
diff --git a/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/log.js b/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/log.js
index dc52a04..f908c38 100644
--- a/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/log.js
+++ b/node_modules/react-native-reanimated-carousel/lib/commonjs/utils/log.js
@@ -3,8 +3,6 @@
 Object.defineProperty(exports, "__esModule", {
   value: true
 });
-exports.log = log;
-exports.round = round;
 
 /**
  * In worklet
@@ -20,3 +18,5 @@ function round(number) {
   return Math.round(number);
 }
 //# sourceMappingURL=log.js.map
+exports.log = log;
+exports.round = round;
\ No newline at end of file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

10 participants