React Native Flatlist渲染项目

时间:2018-11-02 06:23:06

标签: react-native 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>
   );}}

2 个答案:

答案 0 :(得分:1)

    export default class SongList extends Component
      {
     renderSongsList() { 

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

//您必须交叉检查您在此处收到的值。更好的是接收数组项 //这里并传递给渲染道具 //或尝试使用{song,sectionId,rowId}代替下面的(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>
   );}}

答案 1 :(得分:0)

export default class SongList extends Component
  {
     renderItems(item){
    // Here you can access all items of your json by item.property  
    // Like your JSON is { id: '1', title: 'Better Now', artist: 'Post Malone', }
    // You can access these values as item.id, item.title etc       
    }

     renderSongsList() { 

         return( 
             <View>
                 <FlatList
                     data = {this.props.section.songs}
                     renderItem={(item) => this.renderItems(item)}
                 />
             </View>
         );              
       } 
   render() 
   {
   return (
       <View>     
       { this.renderSongsList() }
       </View>
   );}}