Expo Linear Gradient在TV项目中无法识别start和end属性问题
基于Expo TV创建的新项目中集成了expo-linear-gradient组件,未指定start和end属性时渐变效果正常;但添加这两个属性修改渐变方向后,模块出现报错:
ERROR Cannot set prop 'startPoint' on view 'class expo.modules.lineargradient.LinearGradientView'
→ Caused by: java.lang.IllegalArgumentException: Class declares 0 type parameters, but 2 were provided.
项目的package.json内容如下:
{ "name": "myapp", "version": "1.0.0", "main": "index.js", "dependencies": { "@gorhom/portal": "^1.0.14", "@react-native-async-storage/async-storage": "^1.21.0", "@react-native-tvos/config-tv": "~0.0.4", "@react-navigation/native": "^6.1.9", "@react-navigation/stack": "^6.3.20", "@shopify/flash-list": "^1.6.3", "axios": "^1.6.2", "bluebird": "^3.7.2", "expo": "^50.0.0-preview.4", "expo-linear-gradient": "12.5.0", "expo-splash-screen": "~0.26.0", "expo-status-bar": "~1.11.0", "lottie-react-native": "^6.4.1", "moti": "^0.27.2", "react": "18.2.0", "react-dom": "18.2.0", "react-native": "npm:react-native-tvos@~0.73.1-0", "react-native-gesture-handler": "~2.14.0", "react-native-linear-gradient": "^2.8.3", "react-native-qrcode-svg": "^6.2.0", "react-native-reanimated": "~3.6.0", "react-native-safe-area-context": "4.7.4", "react-native-svg": "^14.1.0", "react-native-vector-icons": "^10.0.3" }, "devDependencies": { "@babel/core": "~7.20.0", "@types/react": "~18.2.14", "typescript": "^5.3.0" }, "expo": { "install": { "exclude": [ "react-native" ] } }, "private": true, "scripts": { "start": "expo start", "android": "expo run:android", "ios": "expo run:ios", "web": "expo start --web" } }
移除冲突依赖:你的项目同时安装了
expo-linear-gradient和react-native-linear-gradient,两个包功能重叠会导致原生层冲突,先卸载后者:npm uninstall react-native-linear-gradient # 或使用yarn yarn remove react-native-linear-gradient替换渐变方向写法:Expo TV的Android端对
start/end属性可能存在适配问题,可改用angle属性设置渐变角度(版本支持的话),示例:import { LinearGradient } from 'expo-linear-gradient'; <LinearGradient colors={['#4c669f', '#3b5998', '#192f6a']} angle={45} // 取值范围0-360,对应不同渐变方向 style={{ flex: 1 }} > {/* 子组件内容 */} </LinearGradient>降级渐变包版本:当前使用的
expo-linear-gradient@12.5.0是Expo 50预览版配套包,可能存在TV端bug,可降级到Expo 49兼容的稳定版本:npm install expo-linear-gradient@12.3.0 # 或使用yarn yarn add expo-linear-gradient@12.3.0清理缓存重建项目:修改依赖后清理Expo缓存并重新构建:
expo r -c expo run:android
报错核心是原生层类型参数不匹配,大概率是两个渐变包导致的原生模块冲突,或是Expo 50预览版的expo-linear-gradient在Android TV上对start/end属性的处理存在bug。
内容的提问来源于stack exchange,提问作者Vald

