react-native Image 实现placeholder占位图

react-native Image没有placeholder这样的props,但是业务有需要这种场景,网上找了几种方法:

  • 包裹Image,但是该方法在新版本已无法使用
  • 使用ImageBackground包裹Image,如果图片有透明度,背景图和网络加载的图片就叠加在一起了

所以可以自定义一个组件实现网络加载图片失败后的占位图:

import React from 'react';
import {Image} from 'react-native';
import PropTypes from 'prop-types';class ImageView extends React.PureComponent {static propTypes = {source: PropTypes.object,style: Image.propTypes.style,placeholderSource: PropTypes.number.isRequired,};state: {source: {},loading: boolean};constructor(props) {super(props);this.state = {source: this.props.source,loading: true,};}render() {return (<Image source={this.state.source} style={this.props.style} onLoad={() => {this.setState({loading: false})}} onLoadEnd={() => {if (this.state.loading === true) {this.setState({source: this.props.placeholderSource})}}}/>);}}export default ImageView;

如果希望Image在未加载网络图片时就有占位图可以用两个Image实现:

import React from 'react';
import {Image, StyleSheet, View, ViewPropTypes} from 'react-native';
import PropTypes from 'prop-types';class ImageView extends React.PureComponent {static propTypes = {source: PropTypes.object,style: ViewPropTypes.style,placeholderSource: PropTypes.number.isRequired,};state: {loading: boolean;};constructor(props) {super(props);this.state = {loading: true,};}render() {return (<View style={this.props.style}><Image style={[this.props.style, styles.imageStyle]} source={this.props.source} onLoad={() => this.setState({loading: false})}/>{this.state.loading ? <Image style={[this.props.style, styles.imageStyle]} source={this.props.placeholderSource}/> : null}</View>);}}const styles = StyleSheet.create({imageStyle: {position: 'absolute', top: 0, right: 0, left: 0, bottom: 0}
});export default ImageView;

使用时设置placeHolderSource为占位图资源即可。
<ImageView style={styles.icon} source={{uri: imageUrl}} placeholderSource={require("./img/icon.png")}/>

END
有更好的想法欢迎留言交流~~

react-native Image 实现placeholder占位图相关推荐

  1. android显示多个网络图片,Android加载网络图片并显示占位图

    我们使用glide来实现ImageView加载网络图片的功能,首先引入依赖包 implementation "com.github.bumptech.glide:glide:3.7.0&qu ...

  2. html怎么帮图片占位,html占位图img placeholder

    html image placeholder while loading update 2013 11.21 还有两个问题,解决的不是很好. 1.加载的速度太慢,有时候,没看到它加载,通过网络获取的图 ...

  3. React Native开发总结(一)

    一.RN优劣势 React Native的设计理念: 既拥有Native的用户体验.又保留React的开发效率 优势: 它对比原生开发更为灵活,对比H5体验更为高效. 替代传统的WebView,打开效 ...

  4. React Native 二 常用组件与开源组件

    2019独角兽企业重金招聘Python工程师标准>>> #0.手把手教React Native实战之开山篇##作者简介东方耀 Android开发RN技术 facebookgithub ...

  5. H5、React Native、Native应用对比分析

    @王利华,vczero "存在即合理".凡是存在的,都是合乎规律的.任何新事物的产生总要的它的道理:任何新事物的发展总是有着取代旧事物的能力.React Native来的正是时候, ...

  6. 初步了解React Native的新组件库firstBorn

    first-born is a React Native UI Component Framework, which follows the design methodology Atomic Des ...

  7. React Native基础入门教程:初步使用Flexbox布局

    一.长度的单位 在开始任何布局之前,让我们来首先需要知道,在写React Native组件样式时,长度的不带单位的,它表示"与设备像素密度无关的逻辑像素点". 这个怎么理解呢? 我 ...

  8. 【REACT NATIVE 系列教程之十三】利用LISTVIEW与TEXTINPUT制作聊天/对话框获取组件实例常用的两种方式...

    本站文章均为 李华明Himi 原创,转载务必在明显处注明:  转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/react-native/2346.html ...

  9. React Native - FlexBox弹性盒模型

    FlexBox布局 1. 什么是FlexBox布局?   弹性盒模型(The Flexible Box Module),又叫FlexBox,意为"弹性布局",旨在通过弹性的方式来对 ...

最新文章

  1. 简单粗暴,5行代码,快速实现图像分割
  2. 机器学习笔记 时间序列预测(基本数据处理,Box-Cox)
  3. 转:MAC 下安装PHONEGAP开发环境
  4. 【面试总结】java测试工程师培训
  5. 【HDU - 5900】QSC and Master(区间dp)
  6. java 监听本地端口_Java-在本地端口上侦听RTP数据包
  7. Python新手学习基础之数据结构-序列2
  8. Java操作Mongodb 保存/读取java对象到/从mongodb
  9. dcs 管理计算机的功能,计算机控制系统 第三章 集散控制系统(DCS).doc
  10. Android 自定义组件学习 3
  11. 反编译那些事儿(四)—序列化问题
  12. Tp 引入 simple_html_dom.php
  13. ie浏览器当前的安全设置使计算机面临风险,IE安全设置
  14. 介绍几个比较出名的编程acm题库
  15. 基本农田卫星地图查询_别被迷惑了,你手机上的北斗导航App和北斗卫星导航没关系...
  16. 左手指令_屈老师大班科学教案:猜左手猜右手
  17. 教你如何利用灰度图透明效果制作隐藏图片.Part2
  18. Intriguing properties of neural networks——L-BFGS attack
  19. 基于变色龙认证树的云边端协同流式数据完整性验证模型
  20. 编译出现错误,想知道为什么错误

热门文章

  1. 117页产业互联网发展趋势及机会分析
  2. 安卓手机MTK手机3d模型查看器源码APP下载
  3. html5订货系统,order-admin
  4. 梅斯医学药物警戒系统(PV)通过E2B R3测试,释放PV价值
  5. [统计学笔记](五)统计量及其抽样分布
  6. js 操作字符串的API
  7. TL6678-EasyEVM高端多核DSP评估板
  8. 2022 最新版前端工程师面试题手册
  9. 互联网一二线大厂名单_为什么字节跳动、阿里巴巴、腾讯等互联网大厂都喜欢拿金融牌照...
  10. springboot配置sql日志打印