2017-10-08 6 views
0

をレンダリングI次のコードスニペットがありますはネイティブリアクト - リスト

import React, { Component } from 'react'; 
import { View, Text } from 'react-native'; 
import axios from 'axios'; 
import AlbumDetail from './AlbumDetail'; 

class AlbumList extends Component { 
    state = { albums: [{}, {}, {}] }; 

    componentWillMount() { 
     //console.log('componentWillMount in AlbumList'); 
     axios.get('https://rallycoding.herokuapp.com/api/music_albums') 
      .then(response => this.setState({ albums: response.data })); 
    } 

    renderAlbums() { 


     this.state.albums.map(album => { 


      return (


       <Text key={album.title}>{album.title}</Text> 
      ); 
     }); 
    } 

    render() { 
     const movies = this.state.albums.map(album => { 


      return (

       <Text key={album.title}>{album.title}</Text> 

      ); 
     }); 

     return (
      <View> 
       {movies} 
       {this.renderAlbums()} 

      </View> 
     ); 
    } 
} 

export default AlbumList; 

映画のリストは、重複して印刷する必要があるのためmovies変数やその他の理由のrenderAlbums()コールの一つ。しかし、それはmovies変数のために1回だけ印刷されます。 renderAlbums()は機能しません。何か案は ?

答えて

3

returnrenderAlbumsにする必要があります。

renderAlbums() { 
    return this.state.albums.map(album => { 
     return (
      <Text key={album.title}>{album.title}</Text> 
     ); 
    }); 
} 
関連する問題