首先贴出报错信息:
具体报错:
Cannot add a child that doesn't have a YogaNode to a parent without a measure function!(Trying to add a "RCTRawText [text: ]" to a "RCTView")
报错原因:
1. 在组件代码中添加了注释
2. 标签包裹次序出现了问题
3. map函数写法有问题:
在排除了以上几种可能出现的问题后,我将Text标签用View来包裹,仍然无效;发现了map函数中,更改了如下代码,即可解决了问题:
export default class ContentList extends Component {
render() {
const {bannerList} = this.props;
return (
<View style={{height:200}}>
{ bannerList ?
<Swiper
key={bannerList.length}
dot={<View style={styles.dot}/>}
activeDot={<View style={styles.activeDot}/>}
paginationStyle={styles.paginationStyle}
height={200}
autoplay={true}
autoplayTimeout={3}
loop={true}>
{
bannerList && Array.isArray(bannerList) && bannerList.length>0 ? bannerList.map((item,index)=>{
return(
<View key={index} style={styles.slide}>
<ImageBackground
key={index}
source={{uri:item.bannerUrl}}
style={{
width:Dimensions.get('window').width,
height:200,
flex:1,
justifyContent:'flex-end'
}}>
<View>
<Text style={{marginBottom:20, width: Dimensions.get('window').width,color:Color.HSWhiteColor, textAlign:'center'}}>{item.title}</Text>
</View>
</ImageBackground>
</View>
);
}) : ""
}
</Swiper> : null
}
</View>
)
}
}
改成:
export default class ContentList extends Component {
render() {
const {bannerList} = this.props;
return (
<View style={{height:200}}>
{ bannerList ?
<Swiper
key={bannerList.length}
dot={<View style={styles.dot}/>}
activeDot={<View style={styles.activeDot}/>}
paginationStyle={styles.paginationStyle}
height={200}
autoplay={true}
autoplayTimeout={3}
loop={true}>
{
bannerList && Array.isArray(bannerList) && bannerList.length>0 ? bannerList.map((item,index)=>{
return(
<View key={index} style={styles.slide}>
<ImageBackground
key={index}
source={{uri:item.bannerUrl}}
style={{
width:Dimensions.get('window').width,
height:200,
flex:1,
justifyContent:'flex-end'
}}>
<View>
<Text style={{marginBottom:20, width: Dimensions.get('window').width,color:Color.HSWhiteColor, textAlign:'center'}}>{item.title}</Text>
</View>
</ImageBackground>
</View>
);
}) : <View></View>
}
</Swiper> : null
}
</View>
)
}
}
总结一下:就是将render中的三目运算的否定条件下的return“”改成了<View></View>