如果写下if else返回反应原生

时间:2017-11-13 09:00:54

标签: react-native

希望你做得很好。

我遇到的问题是我只想在条件成真的情况下传递标签。

代表:

render(
   return(
     <View>
         {
          if(data==='test')
            {
              <Text>Hello this is Test Execution </Text>
            }
         }
     </View>
   )
)

我想完全像上面那样做。

我遇到一些错误,我无法理解该怎么做,请帮助我。

谢谢

5 个答案:

答案 0 :(得分:2)

一种简短而安全的方法是使用三元运算符,如果为false则渲染null

{ data === 'test' ? <Text>Hello this is Test Execution </Text> : null }

答案 1 :(得分:1)

你可以这样做

public abstract class Expression {
    private double expression;

    public Expression(double expression) {
        this.expression = expression;
    }
    public abstract double calculate();

    public boolean equals(Object o) {
        Expression e = (Expression) o;
        return this.expression == e.expression;
    }
}

public class AtomicExpression extends Expression {
    private double operand;
    private static final String BLANK = "";

    public AtomicExpression(double operand) {
        super(operand);
        this.operand = operand;
    }

    public double calculate() {
        return this.operand;
    }

    public String toString() {
        String s;

        if(this.operand == (int) this.operand) { //the operand is an integer
            //s = String.format("%d", this.operand);
            System.out.println("this.operand == (int) this.operand");
            s = BLANK + this.operand;
            return s;
        } else { //the num is a double
            s = BLANK + this.operand;
            return s;
        }

    }
}


public class Main {
    public static void main(String[] args) {
        AtomicExpression a = new AtomicExpression(5);
        //System.out.println(a);
    }
}

答案 2 :(得分:1)

您可以尝试

return(){
    <View>
    {data === 'test' && <Text>Hello this is Test Execution </Text>}
    </View>
}

希望这会有所帮助。

答案 3 :(得分:0)

您需要编写一个可选择渲染文本的函数。

    - (void)viewDidLoad
 {
 [self AdRequest1];
 [self AdRequest2];
 }


 -(void)AdRequest1
 {

 CGRect frame = baseView1.frame;

 int yPos = frame.origin.y +frame.size.height;

 bannerView1 =[[GADBannerView alloc]initWithFrame:CGRectMake(0, yPos , scrollViewMain.frame.size.width, 50)];
 bannerView1.adUnitID = @"/1028307/Calendar-Banner-320x50"; // 320x50 size bannerAd
 bannerView1.rootViewController = self;
 bannerView1.delegate = self;
 [bannerView1 loadRequest:[GADRequest request]];
 bannerView1.tag = 111;
 [viewDetailAll addSubview:bannerView1];

 }

 -(void)AdRequest2
 {

 CGRect frame = baseView2.frame;

 int yPos = frame.origin.y +frame.size.height;

 bannerView2 =[[GADBannerView alloc]initWithFrame:CGRectMake(0, yPos , scrollViewMain.frame.size.width, 250)];
 bannerView2.adUnitID = @"1028307/Calendar-300x250"; // 320x250 size bannerAd
 bannerView2.rootViewController = self;
 bannerView2.delegate = self;
 [bannerView2 loadRequest:[GADRequest request]];
 bannerView2.tag = 222;
 [viewDetailAll addSubview:bannerView2];

 }

 - (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error;
 {
 if (bannerView1.tag == 111) {
 bannerView1.hidden =YES;
 NSLog(@"Failed to receive ad with error: %@", [error localizedFailureReason]);
 [self AdRequest1];
 }


 if (bannerView2.tag == 222) {
 bannerView2.hidden =YES;
 NSLog(@"Failed to receive ad with error: %@", [error localizedFailureReason]);
 [self AdRequest2];
 }
 }

 - (void)adViewDidReceiveAd:(GADBannerView *)view;
 {
 dispatch_async(dispatch_get_main_queue(), ^(void)
 {
 if (bannerView1.tag == 111) {

 bannerView1.hidden =NO;
 bannerView1.backgroundColor = [UIColor whiteColor];
 NSLog(@"Ad Received bannerView1");

 //Above baseview1
 }

 if (bannerView2.tag == 222) {
 bannerView2.hidden =NO;
 bannerView2.backgroundColor = [UIColor whiteColor];
 NSLog(@"Ad Received bannerView2");
 //Above baseview2

 }
 });
 }

答案 4 :(得分:0)

你可以试试这个:

render(
   return(
     <View>
         { (data==='test') && <Text>Hello this is Test Execution </Text>}
     </View>
   )
)

但我建议使用以下语法:

render(
  if (data === 'test') {
     return (
        <View>
          <Text>Hello this is Test Execution </Text>
        <View>
     )
  }

  return null;
)