React native if判断和for循环的写法

  1. if判断的写法
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(
			 //这里要写父标签,要不会报错
				{ errorMsg && {errorMsg}} //如果有错误信息,就显示,没有就不显示
				//三元运算用法
				{errorMsg ? {errorMsg} : "" }
			
		)
	}
}
  1. for循环写法
import React from 'react';
import { View, Image, TextInput, Text } from 'react-native';
class BindCard extends React.Component {
	constructor(props) {
	    super(props);
	    this.state = {
	      list: [1,2,3,4,5],
	      data:[{
		 id:1,
		 list:[1,2,3]
		},{
		 id:2,
		 list:[4,5,6]
		}]
	    };
	  }
	  
	keyExtractor = item => item.id;
	
	 renderItem = ({ item, index }) => {
	     return {item},{index};
	 };

	  render(){
	        let {list} = this.state;
		return(
			 //这里要写父标签,要不会报错
				//第一种写法
				{  list && list.map(info,index)=>(
					{info},{index}
				)}
				//第二种写法
				{list.map((info, index) => {
			            return (
				         {info},{index}
			            );
			      })}
			      //第三种写法
			      
		              //双循环写法
		              {
					data.map(item,index)=>(
						
							{ item.list.map(info,index)=>{
								return(
									{info},index
								)
							}}
						
					)
				}
			
		)
	}
}

你可能感兴趣的:(移动端)