首页 > 解决方案 > React Native Flatlist 渲染项

问题描述

我无法显示我的平面列表,也不确定我的编码是否正确。如果我运行它时没有输出。它只会显示一个白屏。我的数据是正确的,即 this.props.section.songs。我想在我的文本中显示标题和艺术家,但我无法做到。

export default class SongList extends Component
  {
     renderSongsList() { 

         return( 
             <View>
                 <FlatList
                     data = {this.props.section.songs}
                     renderItem={(song, sectionId, rowId) => (
                         <TouchableOpacity onPress={ () => Actions.Player({songIndex: parseInt( rowId ),songs: this.props.section.songs, section: this.props.section }) }>
                         <View key={song}  style={ styles.song }>
                             <Text style={styles.itemTitle}>
                               { song.title}
                             </Text >
                             <Text style={styles.itemArtist}>
                               { song.artist }
                             </Text>
                           </View>
                         </TouchableOpacity>
                     )}
                 />
             </View>
         );              
       } 
   render() 
   {
   return (
       <View>     
       { this.renderSongsList() }
       </View>
   );}}

标签: react-nativereact-native-flatlist

解决方案


    export default class SongList extends Component
      {
     renderSongsList() { 

         return( 
             <View>
                 <FlatList
                     data = {this.props.section.songs}

// 您必须交叉检查您在此处收到的值。更好的是接收一个数组项 //here 并传递给渲染道具 // 或者尝试 {song, sectionId, rowId} this 代替 (song, sectionId, rowId) 见下文

                     renderItem={({song, sectionId, rowId}) => (
                         <TouchableOpacity onPress={ () => Actions.Player({songIndex: parseInt( rowId ),songs: this.props.section.songs, section: this.props.section }) }>
                         <View key={song}  style={ styles.song }>
                             <Text style={styles.itemTitle}>
                               { song.title}
                             </Text >
                             <Text style={styles.itemArtist}>
                               { song.artist }
                             </Text>
                           </View>
                         </TouchableOpacity>
                     )}
                 />
             </View>
         );              
       } 
   render() 
   {
   return (
       <View>     
       { this.renderSongsList() }
       </View>
   );}}

推荐阅读