ReactNative中如何实现条件判断显示不同视图 已解决

http://www.tuicool.com/articles/qEBJ3aq

React Native中,现在代码是:

var noticeType = ”;

switch (rowData.noticeType) {

case "expired":

noticeType = require(‘./img/clock_red.png’);

break;

case "normal":

noticeType = require(‘./img/clock_green.png’);

break;

case "notEmergent":

noticeType = require(‘./img/clock_gray.png’);

break;

default:

noticeType = ”;

}

console.log("noticeType=" + noticeType);

return (

<View style={styles.customerRow}>

<Image style={styles.intentionLevel} source={levelIcon} />

<Text style={styles.customerName}>{rowData.customerName}</Text>

<Text style={styles.intentionCar}>{rowData.intentionCar}</Text>

<Image style={styles.noticeType} source={noticeType} />

<Text style={styles.updateTime}>{rowData.updateTime}</Text>

</View>

);

导致结果是:

当noticeType为空时,Image的source值就是空,是无效的,非法的,所以会报错:

现在希望实现:

根据条件判断,显示不同的视图,比如

当noticeType为空时,只是加载一个空的view,透明色,占位,即可;

当noticeType不为空时,就加载一个图片。

react native conditional render

Conditional Rendering – React

然后去试试:

function NoticeTypeImage(props){

const noticeType = props.noticeType;

switch (noticeType) {

case "expired":

return <Image style={styles.noticeType} source={require(‘./img/clock_red.png’)} />;

case "normal":

return <Image style={styles.noticeType} source={require(‘./img/clock_green.png’)} />;

case "notEmergent":

return <Image style={styles.noticeType} source={require(‘./img/clock_gray.png’)} />;

case "":

return <View style={{paddingLeft: 10, width:20, height:20, backgroundColor:’red’}}></View>;

//return <Image style={{paddingLeft: 10, width:20, height:20, backgroundColor:’transparent’}}></Image>;

}

}

return (

<View style={styles.customerRow}>

<Image style={styles.intentionLevel} source={levelIcon} />

<Text style={styles.customerName}>{rowData.customerName}</Text>

<Text style={styles.intentionCar}>{rowData.intentionCar}</Text>

<NoticeTypeImage noticeType={rowData.noticeType}/>

<Text style={styles.updateTime}>{rowData.updateTime}</Text>

</View>

);

}

真的可以了:

【总结】

此处,想要实现,根据输入的值不同,条件判断后,再决定显示什么

思路是:把要显示返回的内容,封装成一个函数,然后传入参数,函数中,根据参数,用if else或switch case就可以返回所需要显示的内容了。

比如:

function NoticeTypeImage(props){

const noticeType = props.noticeType;

switch (noticeType) {

case "expired":

return <Image style={styles.noticeType} source={require(‘./img/clock_red.png’)} />;

case "normal":

return <Image style={styles.noticeType} source={require(‘./img/clock_green.png’)} />;

case "notEmergent":

return <Image style={styles.noticeType} source={require(‘./img/clock_gray.png’)} />;

case "":

return <View style={{paddingLeft: 10, width:20, height:20, backgroundColor:’transparent’}}></View>;

}

}

调用的地方:

return (

<View style={styles.customerRow}>

<Image style={styles.intentionLevel} source={levelIcon} />

<Text style={styles.customerName}>{rowData.customerName}</Text>

<Text style={styles.intentionCar}>{rowData.intentionCar}</Text>

<NoticeTypeImage noticeType={rowData.noticeType}/>

<Text style={styles.updateTime}>{rowData.updateTime}</Text>

</View>

);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值