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

javascript - React Native display View depending on conditional - Stack Overflow

programmeradmin3浏览0评论

In my render method I'd like to display one of two View ponents depending on a conditional that's in my props. e.g:

render() {
  return(
    <View>
      <View>View A</View>
      <View>View B</View>
    </View>
  );
}

Where the conditional is this.props.myConditional. If false, show A, if true, show B...

In my render method I'd like to display one of two View ponents depending on a conditional that's in my props. e.g:

render() {
  return(
    <View>
      <View>View A</View>
      <View>View B</View>
    </View>
  );
}

Where the conditional is this.props.myConditional. If false, show A, if true, show B...

Share Improve this question asked May 30, 2017 at 8:15 user818700user818700
Add a ment  | 

3 Answers 3

Reset to default 11

It's actually pretty easy

render() {
  return <View>
    {this.props.myConditional ? <View>View B</View> : <View>View A</View>}
  </View>
}

If you are Using Functional Component, The latest Approach then its much more easy as,

return (
<View style={Condition ? Styles.A : Styles.B}>

</View>

)

const Styles = StyleSheet.create({ A: { display:"none" }, B:{ display:"flex" } } )

Rest you have to look into functional ponent

There are multiple ways to display based on condition:

  1. Inline Condition

     { (link) && 
      <Text>Read More</Text> 
     }
    
  2. Inline Condition

    { (link) 
      ? <Text>Read More</Text> 
      : <Text>Thank you</Text> }
    
  3. Using a function

    const Displaylink = (props) => {
    
      const isLink = props.isLink;
      if (isLnk) {
       return <Text>Read More</Text>;
      }
      return <Text>Thank you</Text>;
    } 
    
发布评论

评论列表(0)

  1. 暂无评论