1

我使用'worklet';然后runOnUI()得到下面的错误。

因为我用'worklet';我加了import 'react-native-reanimated'

因为我也用runOnUI我加了import {runOnUI} from 'react-native-animated'

错误:

ERROR  TypeError: global.__reanimatedWorkletInit is not a function. (In 'global.__reanimatedWorkletInit(_f)', 'global.__reanimatedWorkletInit' is undefined)
 ERROR  Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication). A frequent cause of the error is that the application entry file path is incorrect.
      This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.
 ERROR  Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication). A frequent cause of the error is that the application entry file path is incorrect.
      This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.
4

1 回答 1

3

简短的回答

除了runOnUI从 导入和使用其他东西react-native-reanimated,例如还添加useSharedValue

import { useSharedValue, runOnUI } from 'react-native-reanimated';

并使用共享值:

const sharedVal = useSharedValue(0);

现在错误将消失。


长答案(我如何得到解决方案):

起初我global.__reanimatedWorkletInit is not a function.在尝试创建小部件时收到错误():

import React from 'react';
import { Text } from 'react-native';

const Example = () => {
  const someWorklet = () => {
    'worklet';
    console.log("Hey I'm running on the UI thread");
  };

  return (<Text>Example</Text>);
};

export default Example;

添加import 'react-native-reanimated';帮助我摆脱了错误。

然后我想跑someWorklet。所以我添加了一个runOnJSrunOnUI让我得到了相同的结果)电话。我的组件现在返回:

<View>
  <Text>Worklet: {runOnUI(someWorklet)()}</Text>
</View>

我通过import { runOnUI } from 'react-native-reanimated';.

现在同样的错误又回来了。

只有在我也导入并调用useSharedValue一切都按预期工作之后。这是我不再抛出错误的组件代码:

import React from 'react';
import { View, Text } from 'react-native';
import { useSharedValue, runOnJS } from 'react-native-reanimated';

const Example = () => {
  const someWorklet = () => {
    'worklet';
    console.log("Hey I'm running on the UI thread");
    return 'World';
  };

  const sharedVal = useSharedValue(0);

  return (
    <View>
      <Text>Hello, {runOnJS(someWorklet)()}</Text>
    </View>
  );
};

export default Example;
于 2021-12-12T01:24:12.710 回答