React Native使用SkeletonPlaceholder组件时报错,求解决方案
React Native SkeletonPlaceholder 骨架屏报错解决方案
缺失依赖或安装不完整
react-native-skeleton-placeholder依赖react-native-linear-gradient,未安装会导致渲染报错。执行以下命令补全依赖:# npm npm install react-native-skeleton-placeholder react-native-linear-gradient # yarn yarn add react-native-skeleton-placeholder react-native-linear-gradient # iOS 需额外执行 cd ios && pod install && cd ..组件使用方式不符合规范
该组件要求子元素需用<SkeletonPlaceholder.Item>包裹,而非直接使用原生<View>。修改SummarySkeleton代码如下:import SkeletonPlaceholder from "react-native-skeleton-placeholder"; import { View } from 'react-native'; const SummarySkeleton = () => { return ( <SkeletonPlaceholder borderRadius={8} highlightColor="#444" backgroundColor="#2b2b2b" > <SkeletonPlaceholder.Item style={{ padding: 16, borderRadius: 12 }}> {[1, 2, 3, 4].map((_, index) => ( <SkeletonPlaceholder.Item key={index} style={{ flexDirection: "row", alignItems: "center", marginBottom: 12, }} > <SkeletonPlaceholder.Item style={{ width: 80, height: 15, marginRight: 10 }} /> <SkeletonPlaceholder.Item style={{ width: 60, height: 15, marginRight: 10 }} /> <SkeletonPlaceholder.Item style={{ width: 100, height: 15 }} /> </SkeletonPlaceholder.Item> ))} </SkeletonPlaceholder.Item> </SkeletonPlaceholder> ); };未导入必要的React Native组件
确保代码顶部导入View组件,否则会报"View is not defined"错误:import { View } from 'react-native';版本兼容属性名问题
若使用新版本组件,部分属性名可能变更,比如旧版的backgroundColor在新版中为baseColor,可尝试调整:<SkeletonPlaceholder borderRadius={8} highlightColor="#444" baseColor="#2b2b2b" > {/* ... */} </SkeletonPlaceholder>
内容的提问来源于stack exchange,提问作者Anjana umendra
相关产品推荐
相关产品推荐

