在vue中一般在需要判断时都是通过if语句来实现的,但是在react native中一般则通过三元运算法来实现。

具体代码如下所示。

import React from 'react';
import { View, Image, TextInput, Text } from 'react-native';
class BindCard extends React.Component {
constructor(props) {
super(props);
this.state = {
errorMsg: ""
};
}
render(){
let {errorMsg} = this.state;
return(
<View> //这里要写父标签,要不会报错
{ errorMsg && <Text>{errorMsg}</Text>} //如果有错误信息,就显示,没有就不显示
//三元运算用法
{errorMsg ? <Text>{errorMsg}</Text> : "" }
</View>
)
}
}

也可以这样

{index== ?(
<View style={styles.center}>
<p>index为1时有内容,不为1时为空</p>
</View>
) : (
<Text />
)}

其实两种写法差不多,也都很容易理解,就不多说了。

再说一下在react native中如何进行循环

import React from 'react';
import { View, Image, TextInput, Text } from 'react-native';
class BindCard extends React.Component {
constructor(props) {
super(props);
this.state = {
list: [,,,,],
data:[{
id:,
list:[,,]
},{
id:,
list:[,,]
}]
};
} keyExtractor = item => item.id; renderItem = ({ item, index }) => {
return <Text>{item},{index}</Text>;
}; render(){
let {list} = this.state;
return(
<View> //这里要写父标签,要不会报错
//第一种写法
{ list && list.map(info,index)=>(
<Text>{info},{index}</Text>
)}
//第二种写法
{list.map((info, index) => {
return (
<Text>{info},{index}</Text>
);
})}
//第三种写法
<FlatList
data={list}
keyExtractor={this.keyExtractor}
renderItem={this.renderItem}
style={{ height: ‘500px’}}
/>
//双循环写法
{
data.map(item,index)=>(
<View>
{ item.list.map(info,index)=>{
return(
<Text>{info},index</Text>
)
}}
</View>
)
}
</View>
)
}
}

上面就是关于react native 中的条件判断和循环的写法了,希望对你有帮助。

在React native 如何写if判断和for循环的更多相关文章

  1. 如何在 React Native 中写一个自定义模块

    https://my.oschina.net/jpushtech/blog/983230

  2. 我们一起学React Native(一):环境配置

    最近想在项目中实现跨平台,对比一下主流的实现方式,选用了React Native.参考网上的教程,对于一直都是原生移动端开发,对前端的知识不是很了解的,感觉入门不是特别简单.于是打算把学习React ...

  3. React Native 二维码扫描组件

    学rn得朋友们,你们知道rn开源项目吗?来吧看这里:http://www.marno.cn/(rn开源项目) React Native学习之路(9) - 注册登录验证的实现 + (用Fetch实现po ...

  4. React Native学习(四)—— 写一个公用组件(头部)

    本文基于React Native 0.52 Demo上传到Git了,有需要可以看看,写了新内容会上传的.Git地址 https://github.com/gingerJY/React-Native-D ...

  5. 深入浅出React Native 3: 从零开始写一个Hello World

    这是深入浅出React Native的第三篇文章. 1. 环境配置 2. 我的第一个应用 将index.ios.js中的代码全部删掉,为什么要删掉呢?因为我们准备从零开始写一个应用~学习技术最好的方式 ...

  6. iOS 写给iOS开发者的React Native学习路线(转)

    我是一名iOS开发者,断断续续一年前开始接触React Native,最近由于工作需要,专职学习React Native也有一个多月了.网络上知识资源非常的多,但能让人豁然开朗.迅速学习的还是少数,我 ...

  7. 写给移动开发者的 React Native 指南

    本文原创版权归 简书 wingjay 所有,如有转载,请于文章篇头位置显示标注原创作者及出处,以示尊重! 作者:wingjay 出处:http://www.jianshu.com/p/b8894425 ...

  8. 写给iOS开发者的React Native学习路线(转)

    我是一名iOS开发者,断断续续一年前开始接触React Native,最近由于工作需要,专职学习React Native也有一个多月了.网络上知识资源非常的多,但能让人豁然开朗.迅速学习的还是少数,我 ...

  9. react native 入门实践

    上周末开始接触react native,版本为0.37,边学边看写了个demo,语法使用es6/7和jsx.准备分享一下这个过程.之前没有native开发和react的使用经验,不对之处烦请指出.希望 ...

随机推荐

  1. 二十八. Ceph概述 部署Ceph集群 Ceph块存储

    client   :192.168.4.10 node1 :192.168.4.11 ndoe2 :192.168.4.12 node3 :192.168.4.13   1.实验环境 准备四台KVM虚 ...

  2. Cogs 750. 栅格网络(对偶图)

    栅格网络流 ★★☆ 输入文件:flowa.in 输出文件:flowa.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] Bob 觉得一般图的最大流问题太难了,他不知道如何解决, ...

  3. vue日常学习

    1.$refs可以用来进行父子级间通信.ref被用于作为子组件的索引ID,用以方便的在js中直接访问子组件.用法如下parent.$refs.idname 使用方法: 在父级元素上加上ref属性 &l ...

  4. 上传本地文件到github仓库

    第一步:新建仓库 给仓库一个名字,备注 得到仓库地址: https://github.com/Lucasli2018/java-1-mybatis.git 第二步:进入要上传的文件夹,初始化上传文件夹 ...

  5. 4)抽象方法不能为private,final或者static,为什么?

    抽象方法的最实质的意 义在于被未来的子类覆盖实现掉.它自己是个空方法.private的实质意义在于本类其他方法调用它.你自己是个空方法,别人调用你有什么用?所以 abstract和private在一起 ...

  6. appium 小程序自动化测试

    https://www.cnblogs.com/yoyoketang/p/9144987.html adb shell su ACTIVITY com.tencent.mm/.plugin.appbr ...

  7. Pandas的Categorical Data

    http://liao.cpython.org/pandas15/ Docs » Pandas的Categorical Data类型 15. Pandas的Categorical Data panda ...

  8. redis启动、关闭脚本

    #!/bin/bash PORT= NAME=redis-server ID=`ps -ef | grep "$NAME" | grep -v "grep" | ...

  9. android studio: no idea annotations attached to the jdk 1.8 some issues will not be found

    Android Studio今天早上打开项目提示错误信息: No IDEA annotations attached to the JDK 1.8 (C:\Program Files\Android\ ...

  10. PHP根据传入的经纬度,和距离范围,返回所有在距离范围内的经纬度的取值范围

    /** * 根据传入的经纬度,和距离范围,返回所有在距离范围内的经纬度的取值范围 * @param float $lng 经度 * @param float $lat 纬度 * @param floa ...