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

Image does not render correctly when included in ternary operator #884

Closed
mrblueblue opened this issue Apr 16, 2015 · 2 comments
Closed
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@mrblueblue
Copy link

tl;dr

A React Image component did not render correctly when included inside a ternary operator. To get around this, I had to separate the Image as a standalone component. After creating this separate component and requiring it in the original component, the Image rendered correctly according to the logic of the ternary.

Is this the correct behavior for the Image component?

Considering that the separate component and the original Image are functionally the same, I don't see how creating the Image as a separate component would cause this change in behavior.

Background

I wanted to create a 'like' button--its default 'unliked' state being a grey heart and its 'liked' state being a color-filled heart.

To implement this I used a ternary operator in render function's return block.

render: function() {
  return(
    <View style={{flex:1}}>
      <TouchableOpacity onPress={this.props.updateHearts}>
        { this.props.hasPressedHeart ?
         <Image
            source={FullHeart}
            style={{width:30, height:30}}
          />
          :
          <Image
            source={EmptyHeart}
            style={{width:30, height:30}}
          />
        }
      </TouchableOpacity>
    </View>
  );
}

In effect, if the user had already pressed the heart, the image would be a "FullHeart," and if not, then the image would be an "EmptyHeart."

The Issue

The correct image would not render after new props were passed to the component.

I verified that the correct props were correct my rendering the logic of the ternary as text, like so:

<Text>
  {this.props.hasPressedHeart ? 1 : 0}
</Text>

Even when the ternary evaluated to false and displayed "0", the corresponding ternary for the image did not evaluate to "FullHeart".

The Workaround

To get around this problem, I created the heart images as separate components and required them like so:

<View style={{flex:1}}>
  <TouchableOpacity onPress={this.props.updateHearts}>
    <View>
      { this.props.hasPressedHeart ?<FullHeart/> : <EmptyHeart/> }
    </View>
  </TouchableOpacity>
</View>
var FullHeart = React.createClass({
  render(){
    return(
      <Image
        source={{uri: 'http://i.imgur.com/SXHb8nG.png?1'}}
        style={{width:30, height:30}}
      />
    );
  }
})

After doing this, the correct image was rendered after receiving new props.

@stubbornleaf
Copy link

It definitely works for me. Not sure what you do in the updateHearts function.

@brentvatne
Copy link
Collaborator

@mrblueblue - if you're still having this issue, ping me and we can look into it further!

@facebook facebook locked as resolved and limited conversation to collaborators May 31, 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

4 participants