-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
PreloadExample.js
96 lines (87 loc) · 2.72 KB
/
PreloadExample.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import React, { Component } from 'react'
import { StyleSheet, View } from 'react-native'
import SectionFlex from './SectionFlex'
import FastImage from 'react-native-fast-image'
import Section from './Section'
import FeatureText from './FeatureText'
import uuid from 'uuid/v4'
import Button from './Button'
import { createImageProgress } from 'react-native-image-progress'
const IMAGE_URL =
'https://cdn-images-1.medium.com/max/1600/1*-CY5bU4OqiJRox7G00sftw.gif'
const Image = createImageProgress(FastImage)
class PreloadExample extends Component {
state = {
show: false,
url: IMAGE_URL,
}
bustCache = () => {
const key = uuid()
const bust = `?bust=${key}`
// Preload images. This can be called anywhere.
const url = IMAGE_URL + bust
this.setState({
url,
show: false,
})
}
preload = () => {
FastImage.preload([{ uri: this.state.url }])
}
showImage = () => {
this.setState({ show: true })
}
render() {
return (
<View>
<Section>
<FeatureText text="• Preloading." />
<FeatureText text="• Progress indication using react-native-image-progress." />
</Section>
<SectionFlex
style={styles.section}
onPress={this.props.onPressReload}
>
{this.state.show ? (
<Image
style={styles.image}
source={{ uri: this.state.url }}
/>
) : (
<View style={styles.image} />
)}
<View style={styles.buttons}>
<View style={{ flex: 1 }}>
<Button text="Bust" onPress={this.bustCache} />
</View>
<View style={{ flex: 1 }}>
<Button text="Preload" onPress={this.preload} />
</View>
<View style={{ flex: 1 }}>
<Button text="Render" onPress={this.showImage} />
</View>
</View>
</SectionFlex>
</View>
)
}
}
const styles = StyleSheet.create({
section: {
flexDirection: 'column',
alignItems: 'center',
},
buttons: {
flexDirection: 'row',
marginHorizontal: 20,
marginBottom: 10,
},
image: {
backgroundColor: '#ddd',
margin: 20,
marginBottom: 10,
height: 100,
width: 100,
},
})
export default PreloadExample