简体   繁体   English

尝试在文本中嵌套视图的 React Native Android 错误

[英]React Native Android error trying to nest a view inside a text

I am working on an iphone/android app with react-native that displays weather in a table.我正在开发一个带有 react-native 的 iphone/android 应用程序,可以在表格中显示天气。 In order to create the table, I found this table-layout library for react-native: https://github.com/Gil2015/react-native-table-component and used it in my code.为了创建表格,我找到了这个用于 react-native 的表格布局库: https : //github.com/Gil2015/react-native-table-component并在我的代码中使用它。 For one of the data rows in my table, I also wanted to display an animated svg file of the weather, so I used this library: https://www.npmjs.com/package/react-native-svg-image .对于表中的数据行之一,我还想显示天气的动画 svg 文件,所以我使用了这个库: https : //www.npmjs.com/package/react-native-svg-image My code is posted below:我的代码贴在下面:

tableData = [["Weather" ,this.getIconForWeather(iconsWeather[0]), this.getIconForWeather(iconsWeather[1]), this.getIconForWeather(iconsWeather[2]), this.getIconForWeather(iconsWeather[3]), this.getIconForWeather(iconsWeather[4]), this.getIconForWeather(iconsWeather[5]), this.getIconForWeather(iconsWeather[6]), this.getIconForWeather(iconsWeather[7]), this.getIconForWeather(iconsWeather[8]), this.getIconForWeather(iconsWeather[9])],
            ["Temp", temp[0] + '°', temp[1]+ '°', temp[2]+ '°', temp[3]+ '°', temp[4]+ '°', temp[5]+ '°', temp[6]+ '°', temp[7]+ '°', temp[8]+ '°', temp[9]+ '°'],
            ["Precip" ,precip[0]+'%', precip[1]+'%', precip[2]+'%', precip[3]+'%', precip[4]+'%', precip[5]+'%', precip[6]+'%', precip[7]+'%', precip[8]+'%', precip[9]+'%'],
            ["Humidity" ,humidity[0]+'%', humidity[1]+'%', humidity[2]+'%', humidity[3]+'%', humidity[4]+'%', humidity[5]+'%', humidity[6]+'%', humidity[7]+'%', humidity[8]+'%', humidity[9]+'%'],
          ];

getIconForWeather(iconName) {
return <View style={{height: 40, width: 40, alignItems:'center', justifyContent:'center'}}>
              <SVGImage
              style={{ width: 40, height: 40, backgroundColor: 'transparent' }}
              scrollEnabled = {false}
              source={{uri: icons[iconName]}}
              />
              </View>
}

//this is the part of the code in my return() in render() that displays the table:
<View style={styles.table}>

           {/* Right scrollview wraper */}
           <ScrollView horizontal={true} showsHorizontalScrollIndicator={false}>
             {/* If parent element is not table element, you should add the type of borderstyle. */}
             <TableWraper borderStyle={{borderWidth: 1,borderColor: 'rgba(255,255,255,0.0)',}}>
               <Row data={tableHead} style={styles.head} textStyle={styles.headText} widthArr={widthArr}/>

               {
                 tableData.map((data, i) => (
                   <Row key={i} data={data} style={[styles.list, i%2 && {backgroundColor: 'rgba(255,255,255,0.0)'}]} widthArr={widthArr} textStyle={styles.listText}/>
                 ))
               }

             </TableWraper>
           </ScrollView>
         </View>

The variable tableData has all the data that needs to be displayed in the table.变量tableData包含需要在表中显示的所有数据。 The part my question focuses on is this.getIconForWeather(iconsWeather[#]) and tableData.map((data, i)) .我的问题关注的部分是this.getIconForWeather(iconsWeather[#])tableData.map((data, i)) The function getIconForWeather(iconName) returns the code to display an SVG image based on the current weather text (ie "rainy", "cloudy", "windy", etc), and the tableData.map is responsible for mapping the data onto the table.函数getIconForWeather(iconName)返回代码以根据当前天气文本(即“下雨”、“多云”、“刮风”等)显示 SVG 图像, tableData.map负责将数据映射到桌子。 This code works perfectly on IOS and the table shows animated SVG files, however when I run it on android I get the error:这段代码在 IOS 上运行良好,表格显示了动画 SVG 文件,但是当我在 android 上运行它时出现错误:

"Invariant violation: Nesting of <View> within <Text> is not supported on android." “不变违规:Android 不支持 <View> 嵌套在 <Text> 中。”

I have done some testing with the table I am using, and my conclusion is that the table is somehow implementing a <Text> </Text> object, and so when I pass <SVGImage> into the table data I am getting a nested view error.我对我正在使用的表进行了一些测试,我的结论是该表以某种方式实现了一个<Text> </Text>对象,因此当我将<SVGImage>传递到表数据中时,我得到了一个嵌套视图错误。 Is there any way I can bypass this or what are other ways I can display my SVG images on the table in Android?有什么方法可以绕过这个,或者还有什么其他方法可以在 Android 的桌子上显示我的 SVG 图像?

for(let i = 0; i < days; i++){

await weatherBuildGUI.push(
<View key={i} style={styles.weatherDays}>

  <View style={{flex:0.25, alignItems:'center', justifyContent:'center'}}>
    <Text style={{fontWeight: 'bold', textAlign:'center'}}>{weatherHi[i]}</Text>
  </View>

  <View style={{flex:0.25, alignItems:'center', justifyContent:'center'}}>
    <Text style={{textAlign:'center'}}>${weatherLo[i]}</Text>
  </View>

  <View style={{flex:0.25, alignItems:'center', justifyContent:'center'}}>
    <Text style={{textAlign:'center', color:clr}}>{weatherDay[i]}</Text>
  </View>

  <View style={{flex:0.25, alignItems:'center', justifyContent:'center'}}>
    <Text style={{textAlign:'center', color:clr}}>{weatherIcon[i]}</Text>
  </View>
</View>
)
}

The react React Native <Text> component only supports nested views on iOS. React Native <Text>组件仅支持 iOS 上的嵌套视图。 The Cell component in your first link uses <Text> to wrap the data you pass into the table, and since you're passing a <View> for your image cell it doesn't work.第一个链接中Cell组件使用<Text>来包装您传递到表格中的数据,并且由于您为图像单元格传递了一个<View> ,因此它不起作用。

I guess you could try to change the <Cell> component to support a custom renderer.我想您可以尝试更改<Cell>组件以支持自定义渲染器。

Or you could modify the Cell component as follows (line 39):或者您可以按如下方式修改 Cell 组件(第 39 行):

{typeof data === 'object'
  ? <View>{data]</View>}
  : <Text style={[textStyle, styles.text]}>{data}</Text>}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM