1. 程式人生 > >Progressive Image Loading in React Native

Progressive Image Loading in React Native

Progressive Image Loading in React Native

Internet connection speeds are variable, especially when you’re working with mobile devices. As developers we often forget that many of our users are running our apps on less powerful devices and with slower internet connections than we are. Go out into the mountains and try to access your app and see how it performs.

One of the more expensive things in an app is the remote images you’re loading. They’re going to take time to load, especially if they’re large images.

Today we’ll build a component that allows us to:

  1. Pass a full size image to display (just like a normal Image component)
  2. Pass a thumbnail image to display while we’re loading the full size image
  3. Automatically display a placeholder in the soon-to-be-downloaded image’s place to indicate that something will be there
  4. Animate between each state as we go.

Getting Started

To start create a new React Native app (via react-native init, create-react-native-app, or the expo cli) and add the following to App.js

.

import React from 'react';
import { StyleSheet, View, Dimensions, Image } from 'react-native';

const w = Dimensions.get('window');

const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});

export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<Image
source={{ uri: `https://images.pexels.com/photos/671557/pexels-photo-671557.jpeg?w=${w.width * 2}&buster=${Math.random()}` }}
style={{ width: w.width, height: w.width }}
resizeMode="cover"
/>
</View>
);
}
}

That block of code simply displays an image. It's requesting an image from Pexels at 2x the size of the screen (we want big images so they load slowly) and the buster query param will help us not cache the image so we can fully see what's going on. You wouldn't want to do this in your actual app.

Feel the Pain

Like I said before — as a developer you’ve probably got a pretty decent internet connection.

Let’s change that.

If you’re on a Mac you can install a tool called Network Link Conditioner (here’s how to install it). I’m sure there is similar for my Windows and Linux fans (chime in below if you have tool suggestions).

It will allow you to simulate just about any network condition you need. Just remember to turn it off before you start trying to stream video.

Enable it and set it to the “3G” profile.

Network Link Conditioner

Here’s a comparison of the starter with Network Link Conditioner off vs. on.

相關推薦

Progressive Image Loading in React Native

Progressive Image Loading in React NativeInternet connection speeds are variable, especially when you’re working with mobile devices. As developers we ofte

[React Native] Reduce Long Import Statements in React Native with Absolute Imports

other absolute fig Go react project only port gen In large React Native projects, it’s common to have long relative import paths li

How to structure your project and manage static resources in React Native

How to structure your project and manage static resources in React NativeReact and React Native are just frameworks, and they do not dictate how we should

Routing in React Native apps and how to configure your project with React

Tab Navigator implements a type of navigation that exists in native iOS for many years already. Recently, Android added it to its Material design patterns

location and App Permissions in React Native and Expo

In web, Geolocation is provided as an API that has different methods to use in a web application. Similarly, React Native takes advantage of this API and i

Setting up ESLint and EditorConfig in React Native projects

Setting up ESLint and EditorConfig in React Native projectsI had a hard time going from 4 spaces to 2 spaces indentation, now I’m quite comfortable with it

Position element at the bottom of the screen using Flexbox in React Native

Position element at the bottom of the screen using Flexbox in React NativeSource: British AirwaysReact Native uses Yoga to achieve Flexbox style layout, wh

基於React Native官方元件Image封裝的具備載入生命週期視覺反饋的元件react-native-loading-image

react-native-loading-image 基於React Native官方元件Image封裝的具備載入生命週期視覺反饋的元件,具體實現功能如下: 網路圖片Pending狀態渲染,提供閃爍動畫、loading.gif兩種方式 網路圖片Error狀態渲染

編譯依賴於React Native0.46.4的工程,報錯“react-native-xcode.sh now lives in a different location”

webstorm alt where git span command com pen app 我的環境: WebStorm 2017.2Build #WS-172.3317.70, built on July 14, 2017 JRE: 1.8.0_152-release

React Native Image多種加載圖片方式

ner styles ole 刷新 理論 width reg apple content p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } p.p2 { margin: 0.0px 0.0px

react-native Text和Image混合嵌套布局

ati turn ext style view ima 保險 ext1 text1 <View style={[styles.p4_text1,]}> {"以下各項稅費及商業保險僅供客戶參考不作為最終購車價格".split("").map((str)=&g

RN 使用第三方組件之react-native-image-picker

app ons sep too code project width odata ext 首先給個github地址:https://github.com/react-community/react-native-image-picker 該插件可以同時給iOS和Andro

windows上react-native run-android時Exception in thread "main" java.lang.IllegalArgumentException: MALFORMED報錯

andro 重新 ive div alt 刪除 ima 用戶 文件夾 報錯如圖 解決 在C:\Users\{用戶名}\.gradle\wrapper\dists路徑下,刪除所有文件夾,重新run-android ps:網上搜了說是說是java解壓縮編碼格式問題什麽的,感覺

React-Native開發五 React NativeImage元件

1 Image元件介紹 RN中Image元件主要用於載入圖片,可載入靜態圖片,網路圖片,以及原生圖片,本地檔案系統中圖片資源 官方參考https://facebook.github.io/react-native/docs/image#resizemode 2 Image元件功

[vscode react-native] xcrun: error: unable to find utility "instruments", not a developer tool or in

執行環境:Mac + vscode + xcode IOS ReactNative執行的時候出現這個錯誤 使用vscode執行reactnative 專案時報錯:xcrun: error: unable to find utility "instruments", not a develop

react-native Could not find "store" in either the context or props

1.參考 https://stackoverflow.com/questions/35420989/could-not-find-store-in-either-the-context-or-props 2.在所有需要繫結Redux的檔案中使用connect 容器元件使用 connect() 方法連線 R

React Native ListView警告:in next release empty section headers ...

升級React Native到最新版本出現了一個Warning: Warning:In next release empty section headers will be rendered. In this release you can use 'enableEmptySections

react native 自定義Image實現預載入圖片及錯誤之後圖片顯示

使用自定義Image直接移步到文章結尾檢視使用 需要新增的第三方庫,prop-types,新增如下 npm install --save prop-types 我們知道react native 裡面的Image元件,預載入圖片只實現了ios,android沒有。載入錯誤

12月11日,發生大面積的包含 react-native-image-picker 的安卓專案啟動崩潰現象解決方案。(我的已經解決)

12月11日,發生大面積的包含 react-native-image-picker 的安卓專案啟動崩潰現象。疑似原因為maven源丟失。解決方案見github:https://github.com/react-native-community/react-native-image-picke

react-native多圖上傳 react-native-image-picker圖片上傳之多個上傳圖片

話不多說 直接貼程式碼, 如有疑問 下方留言或者發郵箱 引入需要的元件 import ImagePicker2 from 'react-native-image-picker'; import RNHeicConverter from 'react-native