ReactNative中如何实现条件判断显示不同视图 已解决
2017-03-19 22:10
741 查看
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>
);
}
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>
);
}
相关文章推荐
- C++--如何实现SDI程序使用CSplitterWnd创建的多个视图的动态地显示和关闭视图
- Datagrid数据绑定后,根据不同条件判断显示问题
- 如何解决css样式表在不同浏览器中显示效果不同的问题
- css判断不同分辨率显示不同宽度布局实现自适应宽度
- JSF怎样根据判断条件显示不同文本
- css判断不同分辨率显示不同宽度布局实现自适应宽度
- C++--如何实现SDI程序使用CSplitterWnd创建的多个视图的动态地显示和关闭视图
- android中如何实现button 不同状态显示切换背景图
- 关于在GridView模版里添加DropDownList实现根据下拉列表的不同选择gridview根据条件筛选显示。
- css判断不同分辨率显示不同宽度布局实现自适应宽度
- C++--如何实现SDI程序使用CSplitterWnd创建的多个视图的动态地显示和关闭视图
- Delphi 如何实现在DbgridEh中不同的行显示不同的颜色
- 如何解决“未能在设计视图中打开,在中以不同方式将值括起来
- 如何实现textarea里的不同文本显示不同颜色
- 如何用DataGrid实现根据日期判断是否显示New标志
- C++--如何实现SDI程序使用CSplitterWnd创建的多个视图的动态地显示和关闭视图
- source_insight3.5字体全部变成了黑色,不能对不同类型的字段显示不同的颜色,如何解决?
- 如何在ecshop中实现后台商品列表显示会员等级价格的解决办法
- 如何实现SDI程序使用CSplitterWnd创建的多个视图的动态地显示和关闭视图
- 基于DevExpress开发的GridView如何实现一列显示不同的控件类型