How to change views based on condition

rams

I am new to react native development. In my application I need to change the color of the view based on the condition. So my question is can I use if condition between the views in react native. The following is the code

import React, {Component} from 'react';
import {StyleSheet, FlatList, Text, View, Alert,Image,TouchableOpacity} from 'react-native';

export default class Myproject extends Component {

  constructor(props)
  {
    super(props);

    this.state = { FlatListItems: [
      {key: 'One'},
      {key: 'Two'},
      {key: 'Three'},
      {key: 'Four'},
      {key: 'Five'},
      {key: 'Six'},
      {key: 'Seven'},
      {key: 'Eight'},
      {key: 'Nine'},
      {key: 'Ten'},
      {key: 'Eleven'},
      {key: 'Twelve'}
    ]}
  }

  FlatListItemSeparator = () => {
    return (
      <View
        style={{
          height: 1,
          width: "100%",
          marginLeft: 12,
          backgroundColor: "#607D8B",
        }}
      />
    );
  }

  GetItem (item) {

  Alert.alert(item);

  }


  render() {
    return (

 <View style={styles.MainContainer}>

      <FlatList

          data={this.state.FlatListItems}

          ItemSeparatorComponent = {this.FlatListItemSeparator}

          renderItem={
            ({item}) => 
             <View style={styles.mainItem}> 
                <View style={styles.itemContainer}>

                   <View>
                     <Image source={require('./resource/ic_drawer.png')}  />
                     <Text style={styles.item} onPress={this.GetItem.bind(this, item.key)}>{item.key}</Text> 
                   </View>

                   <View style={styles.balanceItem}>
                     <View>
                       <Text >Balance</Text>
                       <Text style={{color: '#000',fontSize: 18}}>$89.04</Text>
                     </View>

                     <View style={styles.subItem}>
                       <View>
                         <Text >Account number</Text>
                         <Text style={{color: '#000',fontSize: 14}}>743509-001</Text>
                       </View>
                       <View style={styles.balanceItem}>
                         <Text >Meter number</Text>
                         <Text style={{color: '#000',fontSize: 14}}>17976849</Text>
                       </View>
                     </View>
                    </View>

                    <View style={styles.balanceItem}>
                      <View style={styles.duenbuttonItem}>
                         <View>
                           <Text >Due Date</Text>
                           <Text style={{color: '#000',fontSize: 14}}>30/09/2016</Text>
                         </View>
                      </View>
                      <TouchableOpacity style={styles.btn} onPress={this.login}><Text style={{color: 'white',fontSize: 14}}>PAY NOW</Text></TouchableOpacity>
                    </View>
                    <Image source={require('./resource/arrow_24.png')} style={styles.arrowImage}/>   

                </View>   
             </View>   
          }
      />


  </View>

    );
  }

  login=()=>{
    alert("testing......");
    // this.props.navigation.navigate('Profile');
}
}

I want the output like the below image

enter image description here

I have designed everything fine. But the problem is green arrow and white arrow. I have designed these arrows too. But these arrows are displaying based on the condition.

I have tried like this but getting the following issue

if(item.key == "One"){
      <View style={{borderColor: 'black',borderWidth: 6,marginBottom: -6}}/>
}else{
      <View style={{borderColor: 'white',borderWidth: 6,marginBottom: -6}}/>
}

enter image description here

So here the problem is string comparison or any other problem.

Daniel Gabor

You can do even better than returning a new view. In your case you only want to change the bg color, so you can have a ternary condition inside your style like this:

<View style={{borderColor: item.key == "One" ? "black" : "white"}}/>

UPDATE for multiple condition :

defineBgColor(key){
  switch(key){
    case "One":
      return "black";
    case "Two":
      return "white";
    case "Three":
      return "orange"; 
  }
}

render(){
  <View style={{borderColor: this.defineBgColor(item.key)}}/>
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to change Background of datagridcell based on multiple condition WPF?

分類Dev

Bind data based on condition in XML views. SAPUI5

分類Dev

Spark DataFrame change datatype based on column condition

分類Dev

How to obtain indices of repetitive values based on condition

分類Dev

How do I merge a dictionary based on a condition?

分類Dev

How to create a new column based on condition?

分類Dev

How to upcast this object without splicing, based on a condition?

分類Dev

How to sum elements of nested list based on condition?

分類Dev

How to choose which decorator to apply based on condition?

分類Dev

How to view dataframe columns based on a condition?

分類Dev

MySQL how to populate query columns based on a condition?

分類Dev

How to shorten an array based on condition in bash

分類Dev

How to sort by the sum of a related field in Django using class based views?

分類Dev

How to get keword argument from url in django class based views?

分類Dev

How do I Change the Alignment of Toolbar Child Views

分類Dev

Change default slim views

分類Dev

How to dynamically change the ControlTemplate based on a property in a ViewModel?

分類Dev

oracle How to group data based on value change

分類Dev

How to change the class of an element based on the class of another

分類Dev

razor MVC EF how to show and hide a div based on model condition

分類Dev

How to filter Records based on the condition from Two Columns that are dependent

分類Dev

How to fetch previous 4 records in MySQL based on where condition

分類Dev

R data.table: How to sum variables by group based on a condition?

分類Dev

How to copy data from a column to another based on a condition in R?

分類Dev

How to add an xml tag based on specific condition using python

分類Dev

How to create condition based multiple filter and return non boolean?

分類Dev

How to apply an operation in a column based on a condition from another column

分類Dev

How to do nested groupby operations in a dataframe based on a condition on a column?

分類Dev

How to filter the first and the last row based on a condition in the last row in R

Related 関連記事

  1. 1

    How to change Background of datagridcell based on multiple condition WPF?

  2. 2

    Bind data based on condition in XML views. SAPUI5

  3. 3

    Spark DataFrame change datatype based on column condition

  4. 4

    How to obtain indices of repetitive values based on condition

  5. 5

    How do I merge a dictionary based on a condition?

  6. 6

    How to create a new column based on condition?

  7. 7

    How to upcast this object without splicing, based on a condition?

  8. 8

    How to sum elements of nested list based on condition?

  9. 9

    How to choose which decorator to apply based on condition?

  10. 10

    How to view dataframe columns based on a condition?

  11. 11

    MySQL how to populate query columns based on a condition?

  12. 12

    How to shorten an array based on condition in bash

  13. 13

    How to sort by the sum of a related field in Django using class based views?

  14. 14

    How to get keword argument from url in django class based views?

  15. 15

    How do I Change the Alignment of Toolbar Child Views

  16. 16

    Change default slim views

  17. 17

    How to dynamically change the ControlTemplate based on a property in a ViewModel?

  18. 18

    oracle How to group data based on value change

  19. 19

    How to change the class of an element based on the class of another

  20. 20

    razor MVC EF how to show and hide a div based on model condition

  21. 21

    How to filter Records based on the condition from Two Columns that are dependent

  22. 22

    How to fetch previous 4 records in MySQL based on where condition

  23. 23

    R data.table: How to sum variables by group based on a condition?

  24. 24

    How to copy data from a column to another based on a condition in R?

  25. 25

    How to add an xml tag based on specific condition using python

  26. 26

    How to create condition based multiple filter and return non boolean?

  27. 27

    How to apply an operation in a column based on a condition from another column

  28. 28

    How to do nested groupby operations in a dataframe based on a condition on a column?

  29. 29

    How to filter the first and the last row based on a condition in the last row in R

ホットタグ

アーカイブ