RN报错Cannot add a child that doesn't have a YogaNode to a parent without a measure function!

35 篇文章 1 订阅
2 篇文章 0 订阅

首先贴出报错信息:



具体报错:

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>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值