最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Conditional Styling in React Native - Stack Overflow

programmeradmin0浏览0评论

I have this ImageBackground tag in my React-Native App.

const{height,width} = Dimensions.get('window);
const navHeight = ExtraDimensions.get('SOFT_MENU_BAR_HEIGHT');

render(){
 return(
  <ImageBackground source={Images.bg} style={{width=width+48,height=height}}>
     //content
  </ImageBackground>
 );
}

The number 48 is the height of the default Android navigation bar (the one contains BACK button). The navHeight is to detect the height of navigation bar on the device (refer here:).

Since there are now devices with no navigation bar, i want to make a conditional styling in the ImageBackground style to take style={styles.bg1} when there is a value of navHeight and take style={styles.bg2} when there is no navHeight value. May i know where and how should i implement the styling? thanks

My current wrong way of doing it is

<ImageBackground source={Images.bg} style={navHeight=0 ? styles.bg1 : styles.bg2}>

I have this ImageBackground tag in my React-Native App.

const{height,width} = Dimensions.get('window);
const navHeight = ExtraDimensions.get('SOFT_MENU_BAR_HEIGHT');

render(){
 return(
  <ImageBackground source={Images.bg} style={{width=width+48,height=height}}>
     //content
  </ImageBackground>
 );
}

The number 48 is the height of the default Android navigation bar (the one contains BACK button). The navHeight is to detect the height of navigation bar on the device (refer here:https://github.com/Sunhat/react-native-extra-dimensions-android).

Since there are now devices with no navigation bar, i want to make a conditional styling in the ImageBackground style to take style={styles.bg1} when there is a value of navHeight and take style={styles.bg2} when there is no navHeight value. May i know where and how should i implement the styling? thanks

My current wrong way of doing it is

<ImageBackground source={Images.bg} style={navHeight=0 ? styles.bg1 : styles.bg2}>
Share Improve this question asked Oct 19, 2018 at 5:12 nobody99nobody99 1451 gold badge2 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 16

There is a syntatical error, for comparison you have to use ==.

Try this,

<ImageBackground source={Images.bg} style={ (navHeight==0) ? styles.bg1 : styles.bg2}>

Moreover, i would recommend you using Image tag and append a child component to it by using position="absolute". Because some styling props like borderRadius don't work in the case of ImageBackground tag.

I hope this helps you !

发布评论

评论列表(0)

  1. 暂无评论