我有以下代码片段:
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
变量,它只打印一次。renderAlbums()
不工作。有什么想法吗
1条答案
按热度按时间tkqqtvp11#
你也需要在
renderAlbums
中执行return
。