Skip to content

Commit

Permalink
Merge pull request DylanVann#4 from SocialMediaEmotions/john/issue/118
Browse files Browse the repository at this point in the history
show placeholder when timer is expired
  • Loading branch information
kukiron authored Mar 19, 2019
2 parents b286d2f + de6ece4 commit eda9027
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module.exports = {
parser: 'babel-eslint',
env: {
es6: true,
browser: true,
node: true,
},
plugins: ['jest'],
overrides: {
Expand Down
34 changes: 25 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,36 @@ import {
const FastImageViewNativeModule = NativeModules.FastImageView

class FastImage extends Component {
componentDidMount() {
this.runTimer()
}

componentDidUpdate(prevProps) {
if (this.props.source.uri === prevProps.source.uri) {
return
if (this.props.source.uri !== prevProps.source.uri) {
this.setState({
loaded: false,
error: null,
expired: false,
})

this.runTimer()
}
}

componentWillUnmount() {
clearTimeout(this.timeout)
}

this.setState({
loaded: false,
error: null,
})
runTimer() {
this.timeout = setTimeout(() => {
this.setState({ expired: true })
}, 1000)
}

state = {
loaded: false,
error: null,
expired: false,
}

setNativeProps(nativeProps) {
Expand All @@ -49,7 +65,7 @@ class FastImage extends Component {
...props
} = this.props

const { loaded, error } = this.state
const { expired, loaded, error } = this.state
const resolvedSource = Image.resolveAssetSource(source)

if (fallback) {
Expand All @@ -58,7 +74,7 @@ class FastImage extends Component {
style={[styles.imageContainer, style]}
ref={this.captureRef}
>
{(!loaded || error) && placeholder}
{expired && (!loaded || error) && placeholder}
<FastImageView
{...props}
style={StyleSheet.absoluteFill}
Expand All @@ -76,7 +92,7 @@ class FastImage extends Component {

return (
<View style={[styles.imageContainer, style]} ref={this.captureRef}>
{(!loaded || error) && placeholder}
{expired && (!loaded || error) && placeholder}
<FastImageView
{...props}
style={StyleSheet.absoluteFill}
Expand Down

0 comments on commit eda9027

Please sign in to comment.