3

I want to write if-else inside render return. I want to write if ImageSource === null then I want to get the code below.

    ImagePicker.showImagePicker(options, (response) => {
  console.log('Response = ', response);

  if (response.didCancel) {
    console.log('User cancelled photo picker');
  }
  else if (response.error) {
    console.log('ImagePicker Error: ', response.error);
  }
  else if (response.customButton) {
    console.log('User tapped custom button: ', response.customButton);
  }
  else {
    let source = { uri: response.uri };
    this.setState({
      ImageSource: source,
      data: response.data

    });
  }
});

here is inside render return

<TouchableOpacity onPress={this.selectPhotoTapped.bind(this)}>
      <View style={styles.ImageContainer}>
        {this.state.ImageSource === null ? <Text>Select a Photo</Text> :
          <Image style={styles.ImageContainer} source={this.state.ImageSource} />
        }
      </View>
    </TouchableOpacity>

else I want to get the uploaded image.

7
  • 2
    What is the problem in this code ? Commented Mar 7, 2019 at 2:17
  • @Prasheel This code no problem, I want to implement if else inside the code. ImageSource === null the the code above else else I want to get the uploaded image Commented Mar 7, 2019 at 4:16
  • I haven't see any issue in code. Commented Mar 7, 2019 at 4:19
  • @Nirmalsinh, thanks for your reply. the code can work but I want to implement if-else inside the code. Commented Mar 7, 2019 at 4:23
  • What is the default value for ImageSource? Commented Mar 7, 2019 at 4:23

5 Answers 5

3

Update your condition ImageSource condition with undefined.

<TouchableOpacity onPress={this.selectPhotoTapped.bind(this)}>
      <View style={styles.ImageContainer}>
        {this.state.ImageSource === undefined || this.state.ImageSouce === null ? <Text>Select a Photo</Text> :
          <Image style={styles.ImageContainer} source={this.state.ImageSource} />
        }
      </View>
    </TouchableOpacity>
Sign up to request clarification or add additional context in comments.

4 Comments

still have the bug on image missing
Can we talk vai skype or other middle? So I can help you to resolved the issue
That will help for both
Sent you a request. Kindly reply on it.
0

Try adding parenthesis and let me know if it did work.

<TouchableOpacity onPress={this.selectPhotoTapped.bind(this)}>
    <View style={styles.ImageContainer}>
        {this.state.ImageSource === null ? (
            <Text>Select a Photo</Text> 
        ):(
          <Image style={styles.ImageContainer} source={this.state.ImageSource} />
        )}
     </View>
</TouchableOpacity>

3 Comments

Thanks for your reply. This code can work. I have the bug on image missing when backward still there. I try to put if else inside the code but cannot work.
@qing Do you think I should leave this answer or should I remove it?
this code no error can work but cannot solve my bug. No problem if you want to remove it. If you want to leave this answer no problem also. Anyway thanks for your reply and help me. Thank you very much :)
0

This usually happens when you do inline conditional rendering like

{var && <MyComponent />}

which will render MyComponent if var is true. But it tried to render value of var. The fix is change conditions to make sure it's Boolean expression like

{var !== 0 && <MyComponent/ >}.

so you can change your code like this

<TouchableOpacity onPress={this.selectPhotoTapped.bind(this)}>
  <View style={styles.ImageContainer}>
    {this.state.ImageSource === null && <Text>Select a Photo</Text> }
    { this.state.ImageSource &&  <Image style={styles.ImageContainer} source={this.state.ImageSource} />
    }
  </View>
</TouchableOpacity>

this should work! for more details refer to this thread

Comments

0

I suggest that you should split your render functions into several small function like:

renderTitle = () => <Text>Select a Photo</Text> 

renderImage = () => <Image style={styles.ImageContainer} source={this.state.ImageSource} />

 render(){
  return <TouchableOpacity onPress={this.selectPhotoTapped.bind(this)}>
      <View style={styles.ImageContainer}>
        {this.state.ImageSource === null ? this.renderTitle() : this.renderImage()
        }
      </View>
    </TouchableOpacity>
}

Comments

0

I can't find anything wrong in your code?

or you can try this way.

<TouchableOpacity onPress={this.selectPhotoTapped.bind(this)}>
  <View style={styles.ImageContainer}>

    {!this.state.ImageSource && <Text>Select a Photo</Text> }
    {this.state.ImageSource && <Image style={styles.ImageContainer} source={this.state.ImageSource} />
    }
  </View>
</TouchableOpacity>

1 Comment

thanks for your reply. I have the bug on image missing when backward still there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.