首页 > 解决方案 > react-native-reanimated 2:无法使用“worklet”关键字创建函数(未定义不是函数)

问题描述

我刚刚学习v2,但是在使用“worklet”关键字react-native-reanimated创建函数时遇到了问题。

react-native-reanimated在一个 React Native裸项目上使用npx react-native init myApp.
我已遵循所有react-native-reanimatedv2 安装说明,包括:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    'react-native-reanimated/plugin'
  ]
};

我尝试制作一个简单的“worklet”函数,如下所示:

import React from 'react';
import { View, Button } from 'react-native';

const App = () => {

  const someWorklet = () => {
    'worklet';
    console.log('this run on UI thread');
  };

  return (
    <View >
      <Button title="Press" onPress={() => { }} />
    </View>
  );
};

export default App;

正如您在上面看到的,它只是一个简单的代码App.js,但是如果我输入'worklet'关键字,它总是会undefined is not a function出现这样的错误:


错误信息



如果你明白,请告诉我。谢谢 :)

标签: javascriptreact-nativebabeljsreact-native-reanimatedreact-native-reanimated-v2

解决方案


哦,我觉得自己像个愚蠢的人...
我只需要导入react-native-reanimatedApp.js完成所有操作.. :)

import 'react-native-reanimated'

看起来 react-native-reanimated v2 文档没有提到将react-native-reanimatedon 导入到我们项目的顶部......


推荐阅读