Reactnativeif判断和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(
<View> //这⾥要写⽗标签,要不会报错
{ errorMsg && <Text>{errorMsg}</Text>} //如果有错误信息,就显⽰,没有就不显⽰
//三元运算⽤法
{errorMsg ? <Text>{errorMsg}</Text> : "" }
</View>
)
}
}
2. 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 <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}
reactnative开发
renderItem={derItem}
style={{ height: ‘500px’}}
/>
//双循环写法
{
data.map(item,index)=>(
<View>
{ item.list.map(info,index)=>{
return(
<Text>{info},index</Text>
)
}}
</View>
)
}
</View>
)
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。