简体   繁体   English

.map()renderRow()中的JSX数组

[英].map() an array into JSX inside renderRow()

I am having trouble using taking an array of strings and 'converting' that into JSX using the map() function so that the JSX can make one line or row in a ListView. 我在使用字符串数组并使用map()函数将其“转换”为JSX时遇到麻烦,以便JSX可以在ListView中制作一行或一行。

  renderRow: function(rowData) { //rowData is an Array
    return rowData.map((data) => {
      // now data is a single element of my array
      return <View><Text> <Emoji size={40} name={data} /></Text></View>
      // also tried ( ) around the above for the return
    });
  },
  render: function() {
    return (
        <ListView contentContainerStyle={styles.list}
          enableEmptySections={true}
          initialListSize={500}
          pageSize={20}
          dataSource={this.state.dataSource}
          renderRow={(rowData) => this.renderRow(rowData)}
        />
    )
  }

The error I get with this is as follows: 我得到的错误如下:

StaticRenderer.render(): A valid React element (or null) must be returned. StaticRenderer.render():必须返回有效的React元素(或null)。 You may have returned undefined, an array or some other invalid object. 您可能返回了undefined,数组或其他无效对象。

Therefore I know it's something to do with what's going on in the map, and for some reason the JSX isn't being constructed properly inside the renderRow() function. 因此,我知道这与地图中发生的事情有关,并且由于某种原因,JSX在renderRow()函数内部未正确构建。 Returning some basic JSX without the map works ok.. but I need to be able to do it for all elements of my array. 返回一些没有地图的基本JSX可以正常工作..但是我需要能够对数组的所有元素执行此操作。

Is map() even the best way to do this? map()甚至是执行此操作的最佳方法吗? Any help would be greatly appreciated. 任何帮助将不胜感激。

React doesn't know how to deal with lists of elements. React不知道如何处理元素列表。 Every component render method, as well as callbacks like renderRow , typically need to return a single element. 每个组件的render方法以及诸如renderRow回调通常都需要返回一个元素。

You need to wrap the items in a View , or other container element: 您需要将项目包装在View或其他容器元素中:

renderRow: function(rowData) { //rowData is an Array
  return (
    <View>
      {rowData.map((data) => {
        return <View><Text> <Emoji size={40} name={data} /></Text></View>
      })}
    </View>
  )
}

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

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