Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to avoid flickering when Image re-render ? #981

Closed
leecade opened this issue Apr 23, 2015 · 10 comments
Closed

How to avoid flickering when Image re-render ? #981

leecade opened this issue Apr 23, 2015 · 10 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@leecade
Copy link

leecade commented Apr 23, 2015

Is there something wrong?

Maybe I should do some cache first like image render with canvas?

Any help, thanks

@sahrens
Copy link
Contributor

sahrens commented Apr 24, 2015

Are you changing the image URL? If so, then you can layer multiple images on top of each other with transparent backgrounds (and position: 'absolute') so the old image will remain visible until the new image loads on top of it. If you're not changing the URL, then I'm not sure why this would happen - what does your code look like?

@leecade
Copy link
Author

leecade commented Apr 29, 2015

hi, @sahrens

There is a example, maybe re-render by key cause flickering, not sure.

'use strict'

var React = require('react-native')
var {
  AppRegistry,
  ScrollView,
  View,
  Text,
  StyleSheet,
  Image,
} = React

var test = React.createClass({

  getInitialState() {
    return {
      i: 1
    }
  },

  componentDidMount() {
    var that = this
    setInterval(function () {
      that.setState({
        i: that.state.i + 1
      })
    }, 500)
  },

  render() {
    var content = <Image style={{width: 300, height: 300,}} source={{uri: 'https://tse1-mm.cn.bing.net/th?id=RlT8VmiyzVdVy5tpPgcU1bAlnhtSrwZb3vxI1r2lWH9Yg&w=272&h=272&c=7&rs=1&qlt=90&o=4&pid=1.9'}} />
    return (
      <ScrollView style={{marginTop: 100}} key={this.state.i}>
        <View>{content}</View>
      </ScrollView>
    )
  }
})

AppRegistry.registerComponent('swiper', () => test)

@leecade leecade changed the title How to avoid flickering when Image re-render? How to avoid flickering when Image re-render ? Apr 29, 2015
@sahrens
Copy link
Contributor

sahrens commented Apr 29, 2015

Why are you changing the key? That could definitely cause flickering because it will bypass the react diffing algorithm and force the view to be destroyed and a fresh one replace it. Usually that will happen seamlessly within one frame, but it might not in all cases. If you're doing it to refresh scroll position, I'd recommend using something like this.refs.scrollView.scrollTo(0, 0)

On Apr 28, 2015, at 7:27 PM, 斯人 notifications@github.com wrote:

hi, @sahrens

There is a example, maybe re-render by key cause flickering, not sure.

'use strict'

var React = require('react-native')
var {
AppRegistry,
ScrollView,
View,
Text,
StyleSheet,
Image,
} = React

var test = React.createClass({

getInitialState() {
return {
i: 1
}
},

componentDidMount() {
var that = this
setInterval(function () {
that.setState({
i: that.state.i + 1
})
}, 500)
},

render() {
var content = <Image style={{width: 300, height: 300,}} source={{uri: 'https://tse1-mm.cn.bing.net/th?id=RlT8VmiyzVdVy5tpPgcU1bAlnhtSrwZb3vxI1r2lWH9Yg&w=272&h=272&c=7&rs=1&qlt=90&o=4&pid=1.9'}} />
return (
<ScrollView style={{marginTop: 100}} key={this.state.i}>
{content}

)
}
})

AppRegistry.registerComponent('swiper', () => test)

Reply to this email directly or view it on GitHub.

@leecade
Copy link
Author

leecade commented May 1, 2015

thanks @sahrens I see, key cause a overall refresh

@leecade leecade closed this as completed May 1, 2015
@nikhilgoswami
Copy link

I am using ImageBackground component on a login form and changing any state of form field or when the from receives props the image flickers, at first I thought it might be due to me using high resolution image but changing resolution did not solve the issue.

@MacKentoch
Copy link
Contributor

MacKentoch commented Jan 2, 2018

I had flickering issues with Image.

I don't know why, but replacing source prop with defaultSource prop fixed it for me in iOS:

<Image
  style={styles.logoImage}
  resizeMethod="scale"
  resizeMode="contain"
  // source={Logo}
  defaultSource={Logo}
 />

But in Android there is no defaultSource, so I had to

  • get back source prop
  • changed resizeModeprop from contain to cover:
<Image
  style={styles.logoImage}
  resizeMethod="scale"
  resizeMode="cover"
  source={Logo}
  defaultSource={Logo} 
 />

@VolkRiot
Copy link

@MacKentoch Your solution worked perfectly for me developing an iOS Build.

@quantumproducer
Copy link

@MacKentoch what did you do for Android regarding "get back source prop"?

@GreenRidingHood
Copy link

@MacKentoch thank you so much, works like charm
(npm run ios > react ios app)

@GreenRidingHood
Copy link

@MacKentoch is that only solution for android, I mean defaultSource not supported at android, can I use "... platform" thing if android source, if ios defaultSource or something like that :D

@facebook facebook locked as resolved and limited conversation to collaborators May 29, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

8 participants