文档开发者中心
我的
|

HarmonyOS 6

HarmonyOS 5

0.72RN按照@gorhom/bottom-sheet指导文档运行demo报错

已解决

【问题描述】:0.72RN按照@gorhom/bottom-sheet指导文档运行上面的demo报错
1. 希望能优化一下指导文档上面的demo
2. 我看了下这个库本身是不含原生侧的代码的,指导文档上那个codegen应该不需要配置吧?是否能优化这点

【问题现象】:看报错信息是手势库的问题,但是我直接运行手势库的demo是正常的。

​

【版本信息】:

harmony:

收起
自动换行
深色代码主题
复制
"targetSdkVersion": "6.0.0(20)",
"compatibleSdkVersion": "5.0.0(12)",

package.json:

收起
自动换行
深色代码主题
复制
{
  "name": "AwesomeProject",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "lint": "eslint .",
    "start": "hdc rport tcp:8081 tcp:8081 && react-native start --config metro.config.harmony.js",
    "test": "jest",
    "codegen": "react-native codegen-harmony --cpp-output-path ./harmony/entry/src/main/cpp/generated --rnoh-module-path ./harmony/entry/oh_modules/@rnoh/react-native-openharmony",
    "dev": "react-native bundle-harmony --dev"
  },
  "dependencies": {
    "@react-native-oh-tpl/bottom-sheet": "^4.6.4-0.0.2",
    "@react-native-oh/react-native-harmony": "^0.72.96",
    "@react-native-ohos/react-native-gesture-handler": "^2.14.18-rc.3",
    "@react-native-ohos/react-native-reanimated": "^3.6.5-rc.2",
    "@react-native-ohos/react-native-webview": "^13.10.5-rc.5",
    "react": "18.2.0",
    "react-native": "0.72.5",
    "react-native-echarts-pro": "^1.9.1",
    "react-native-reanimated": "^3.6.0"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@babel/preset-env": "^7.20.0",
    "@babel/runtime": "^7.20.0",
    "@react-native/eslint-config": "^0.72.2",
    "@react-native/metro-config": "^0.72.11",
    "@tsconfig/react-native": "^3.0.0",
    "@types/react": "^18.0.24",
    "@types/react-test-renderer": "^18.0.0",
    "babel-jest": "^29.2.1",
    "eslint": "^8.19.0",
    "jest": "^29.2.1",
    "metro-react-native-babel-preset": "0.76.8",
    "prettier": "^2.4.1",
    "react-test-renderer": "18.2.0",
    "typescript": "4.8.4"
  },
  "engines": {
    "node": ">=16"
  }
}

【复现代码】:

收起
自动换行
深色代码主题
复制
// bottom-sheet组件
import React, { useCallback, useRef, useMemo } from "react";
import { StyleSheet, View, Text, Button } from "react-native";
import BottomSheet, { BottomSheetView } from "@gorhom/bottom-sheet";

const App = () => {
  // hooks
  const sheetRef = useRef<BottomSheet>(null);

  // variables
  const snapPoints = useMemo(() => ["25%", "50%", "90%"], []);

  // callbacks
  const handleSheetChange = useCallback((index) => {
    console.log("handleSheetChange", index);
  }, []);
  const handleSnapPress = useCallback((index) => {
    sheetRef.current?.snapToIndex(index);
  }, []);
  const handleClosePress = useCallback(() => {
    sheetRef.current?.close();
  }, []);

  // render
  return (
    <View style={styles.container}>
      <Button title="Snap To 90%" onPress={() => handleSnapPress(2)} />
      <Button title="Snap To 50%" onPress={() => handleSnapPress(1)} />
      <Button title="Snap To 25%" onPress={() => handleSnapPress(0)} />
      <Button title="Close" onPress={() => handleClosePress()} />
      <BottomSheet
        ref={sheetRef}
        snapPoints={snapPoints}
        onChange={handleSheetChange}
      >
        <BottomSheetView>
          <Text>Awesome 🔥</Text>
        </BottomSheetView>
      </BottomSheet>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: 200,
  },
});

export default App;

【尝试解决方案】:

点赞
收藏
回复
7
分享
举报
浏览184 编辑于2026-01-05 01:37湖北
全部评论
最多点赞
最新发布
最早发布
|
采纳答复

View不支持手势操作,需要使用GestureHandlerRootView,可以使用GestureHandlerRootView代替View

收起
自动换行
深色代码主题
复制
<GestureHandlerRootView style={styles.container}>
    
</GestureHandlerRootView>

【背景知识】
‌react-native-gesture-handler‌是一个用于React Native的库,它提供了丰富的手势识别功能,包括拖拽、捏合等,使得开发者可以更容易地实现复杂的交互效果。

5
1楼回复于2026-01-04 06:39 来自广东
  • 杨志鹏
    我试了下,改为 GestureHandlerRootView 标签就没报错且功能正常,那这样的话鸿蒙化的 @gorhom/bottom-sheet 库的指导文档上面的 demo 是否需要更改一下,因为他上面用的就是 View 标签写的,并且指导文档上面还有配置codegen的信息,这个库应该不需要配置codegen吧?希望能修改一下,谢谢
    2026-01-05 01:34 来自湖北
  • 云帆扬回复杨志鹏
  • 杨志鹏回复云帆扬
    配置codegen的段落没有删除吗?这个库不涉及codegen吧
    2026-01-20 07:03 来自湖北
  • 展开剩余2条回复

这个报错(Cannot read property 'getViewByTag' of undefined)是鸿蒙适配版 RN 环境下的依赖冲突 + 配置缺失 + 导入错误导致的,结合你的环境和代码,按以下步骤解决:

一、先修复「依赖冲突 + 导入错误」(核心原因)

你的package.json存在重复依赖 + 导入路径错误,这是底层报错的关键:

1. 卸载重复 / 不兼容的依赖

你同时装了普通 RN 的react-native-reanimated和鸿蒙适配版的@react-native-ohos/react-native-reanimated,两者冲突;且@gorhom/bottom-sheet实际装的是鸿蒙适配包,不能直接导入原包:

bash

运行

收起
自动换行
深色代码主题
复制
# 卸载普通RN的reanimated(鸿蒙环境用适配版)
npm uninstall react-native-reanimated

# 卸载原包(实际用鸿蒙适配版)
npm uninstall @gorhom/bottom-sheet

2. 修正导入路径

你装的是鸿蒙适配版的@react-native-oh-tpl/bottom-sheet,代码中却导入了原包@gorhom/bottom-sheet,导致组件未正确初始化,需修改导入:

tsx

收起
自动换行
深色代码主题
复制
// 原错误导入
// import BottomSheet, { BottomSheetView } from "@gorhom/bottom-sheet";

// 修正为鸿蒙适配包的导入
import BottomSheet, { BottomSheetView } from "@react-native-oh-tpl/bottom-sheet";

二、补充「手势库根容器」(必须配置)

react-native-gesture-handler(鸿蒙适配版)要求所有手势组件必须包裹在GestureHandlerRootView中,你的代码缺失这个根容器,导致手势系统初始化失败:

tsx

收起
自动换行
深色代码主题
复制
// 从鸿蒙适配的手势库中导入根容器
import { GestureHandlerRootView } from "@react-native-ohos/react-native-gesture-handler";

const App = () => {
  // ...你的hooks和回调逻辑保持不变

  // 外层包裹GestureHandlerRootView
  return (
    <GestureHandlerRootView style={styles.containerRoot}>
      <View style={styles.container}>
        {/* 你的Button和BottomSheet代码保持不变 */}
        <Button title="Snap To 90%" onPress={() => handleSnapPress(2)} />
        {/* ...其他Button */}
        <BottomSheet
          ref={sheetRef}
          snapPoints={snapPoints}
          onChange={handleSheetChange}
        >
          <BottomSheetView>
            <Text>Awesome 🔥</Text>
          </BottomSheetView>
        </BottomSheet>
      </View>
    </GestureHandlerRootView>
  );
};

const styles = StyleSheet.create({
  containerRoot: { // 根容器占满全屏
    flex: 1,
  },
  container: {
    flex: 1,
    paddingTop: 200,
  },
});

三、配置 Reanimated 的 Babel 插件(确保动画生效)

@react-native-ohos/react-native-reanimated需要 Babel 插件转译,修改babel.config.js:

javascript

运行

收起
自动换行
深色代码主题
复制
module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    // 添加Reanimated的Babel插件
    '@react-native-ohos/react-native-reanimated/plugin'
  ]
};

四、清理缓存并重新构建

依赖和配置修改后,必须清理缓存避免残留冲突:

bash

运行

收起
自动换行
深色代码主题
复制
# 清理RN缓存
npm cache clean --force

# 删除node_modules和锁文件
rm -rf node_modules package-lock.json

# 重新安装依赖
npm install

# 重新编译鸿蒙项目
npm run codegen
npm run dev

补充说明(避坑点)

  1. 鸿蒙适配包的版本匹配:@react-native-oh-tpl/bottom-sheet依赖@react-native-ohos/react-native-gesture-handler和@react-native-ohos/react-native-reanimated,确保这三个包的版本是适配 RN 0.72 的(你的版本2.14.18-rc.3和3.6.5-rc.2是兼容的);
  2. 不要混用普通 RN 包和鸿蒙适配包:所有依赖都要选@react-native-ohos/xxx或@react-native-oh-tpl/xxx前缀的,避免普通 RN 包在鸿蒙环境下无法运行;
  3. BottomSheet 的必填属性:若后续仍报错,可给BottomSheet添加enablePanDownToClose={true}等基础属性,确保组件初始化完整。

按以上步骤操作后,BottomSheet 的手势系统会正常初始化,getViewByTag的未定义错误会被解决。

展开全部
2楼回复于2026-01-05 00:46 来自广东
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。