react-native-worklets-core: In callInContext the function parameter is not a valid worklet and cannot be called between contexts or from/to JS from/to a context.

While using:

function RealApp() {
  const fibonacci = (num: number): number => {
    'worklet';
    if (num <= 1) return 1;
    return fibonacci(num - 1) + fibonacci(num - 2);
  };

  const worklet = Worklets.createRunInContextFn(fibonacci);
  worklet(50).then(result => {
    console.log(`Fibonacci of 50 is ${result}`);
  });
}

I get:

In callInContext the function parameter is not a valid worklet and cannot be called between contexts or from/to JS from/to a context.

Running:

Expo: 49

Babel:
module.exports = function (api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: [
      'nativewind/babel',
      'module:react-native-dotenv',
      'react-native-worklets-core/plugin',
      'react-native-reanimated/plugin',
      ['@babel/plugin-transform-flow-strip-types', { loose: true }],
      ['@babel/plugin-proposal-private-methods', { loose: true }],
    ],
  };
};

I did rebuild, prebuild, clean cache, clean pods, removed node modules reinstalled etc…

About this issue

  • Original URL
  • State: open
  • Created 9 months ago
  • Reactions: 3
  • Comments: 15 (3 by maintainers)

Most upvoted comments

Any update on this? I am experiencing the same error. For example my code is:

const test = (num: number): number => {
    'worklet'
    return num * 2;
};

useEffect(() => {
    const run = async () => {
        try {
            const worklet = Worklets.createRunInContextFn(test);
            const result = await worklet(50);
            console.log(`result is ${result}`);
        } catch (e) {
            console.log(e);
        }
    };

    run();
}, []);

When I look at the logs I see:

Loading react-native-worklets-core… Worklets loaded successfully [Error: In callInContext the function parameter is not a valid worklet and cannot be called between contexts or from/to JS from/to a context.]

My issue above looks to have been caused by the fact that our repo was using an older version of react-native-reanimated (^2.14.1). Upgrading this to the latest version (^3.7.2) has fixed this for me - hope this helps someone else too!

@chrfalch yes I did 🙂 provided you’re talking about this step:

  1. Add the babel plugin to your babel.config.js:
module.exports = {
  plugins: [
    ["react-native-worklets-core/plugin"],
    // ...
  ],
  // ...
};

Same thing happening to me, with the simplest worklet possible - just immediately returning a string