在React native 如何写if判断和for循环
在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循环的更多相关文章
- 如何在 React Native 中写一个自定义模块
https://my.oschina.net/jpushtech/blog/983230
- 我们一起学React Native(一):环境配置
最近想在项目中实现跨平台,对比一下主流的实现方式,选用了React Native.参考网上的教程,对于一直都是原生移动端开发,对前端的知识不是很了解的,感觉入门不是特别简单.于是打算把学习React ...
- React Native 二维码扫描组件
学rn得朋友们,你们知道rn开源项目吗?来吧看这里:http://www.marno.cn/(rn开源项目) React Native学习之路(9) - 注册登录验证的实现 + (用Fetch实现po ...
- React Native学习(四)—— 写一个公用组件(头部)
本文基于React Native 0.52 Demo上传到Git了,有需要可以看看,写了新内容会上传的.Git地址 https://github.com/gingerJY/React-Native-D ...
- 深入浅出React Native 3: 从零开始写一个Hello World
这是深入浅出React Native的第三篇文章. 1. 环境配置 2. 我的第一个应用 将index.ios.js中的代码全部删掉,为什么要删掉呢?因为我们准备从零开始写一个应用~学习技术最好的方式 ...
- iOS 写给iOS开发者的React Native学习路线(转)
我是一名iOS开发者,断断续续一年前开始接触React Native,最近由于工作需要,专职学习React Native也有一个多月了.网络上知识资源非常的多,但能让人豁然开朗.迅速学习的还是少数,我 ...
- 写给移动开发者的 React Native 指南
本文原创版权归 简书 wingjay 所有,如有转载,请于文章篇头位置显示标注原创作者及出处,以示尊重! 作者:wingjay 出处:http://www.jianshu.com/p/b8894425 ...
- 写给iOS开发者的React Native学习路线(转)
我是一名iOS开发者,断断续续一年前开始接触React Native,最近由于工作需要,专职学习React Native也有一个多月了.网络上知识资源非常的多,但能让人豁然开朗.迅速学习的还是少数,我 ...
- react native 入门实践
上周末开始接触react native,版本为0.37,边学边看写了个demo,语法使用es6/7和jsx.准备分享一下这个过程.之前没有native开发和react的使用经验,不对之处烦请指出.希望 ...
随机推荐
- WiredTiger 4.1.0 发布,MongoDB 存储引擎
WiredTiger 4.1.0 发布了.WiredTiger 是一个开源的.高性能.可伸缩的 MongoDB 数据存储引擎. 下面是一些性能比较图表: 该版本暂未提供更新内容. 下载地址: Sour ...
- P2543 [AHOI2004]奇怪的字符串
题目描述 输入输出格式 输入格式: 输入文件中包含两个字符串X和Y.当中两字符串非0即1.序列长度均小于9999. 输出格式: X和Y的最长公共子序列长度. 输入输出样例 输入样例#1: 复制 010 ...
- 用nginx解决前端跨域问题
假如前端你项目部署在nginx的根目录下,然后项目需要请求后台小伙伴写的接口 nginx配置: #user nobody; worker_processes 1; #error_log logs/er ...
- 在docker容器中编译hadoop 3.1.0
在docker容器中编译hadoop 3.1.0 优点:docker安装好之后可以一键部署编译环境,不用担心各种库不兼容等问题,编译失败率低. Hadoop 3.1.0 的源代码目录下有一个 `sta ...
- elasticsearch bootstrap.memory_lock
检查bootstrap.memory_lock设置是否生效 get http://10.127.0.1:9200/_nodes?filter_path=**.mlockall 响应: { " ...
- springMVC_配置文件搭建基础环境
SpringMVC与Struts的区别. 一.基础jar包 二.①DispatcherServlet,handelMapping,webAction(colltroller),ModelAndView ...
- IIS URL Rewriting and ASP.NET Routing
IIS URL Rewriting and ASP.NET Routing With the release of the URL Rewrite Module for IIS and the inc ...
- Code First 迁移----官方 应用程序启动时自动升级(MigrateDatabaseToLatestVersion 初始值设定项)
Code First 迁移 如果使用的是 Code First 工作流,推荐使用 Code First 迁移改进应用程序的数据库架构. 迁移提供一组允许以下操作的工具: 创建可用于 EF 模型的初始数 ...
- OpenSL ES: OpenSL ES 简介
1. OpenSL ES 是什么 OpenSL ES (Open Sound Library for Embedded Systems)是无授权费.跨平台.针对嵌入式系统精心优化的硬件音频加速API. ...
- 浅析angular,react,vue.js jQuery-1
作者:尚春链接:https://www.zhihu.com/question/38989845/answer/79201080来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...