reactjs React Native -渲染列表

mwkjh3gx  于 2023-05-06  发布在  React
关注(0)|答案(1)|浏览(195)

我有以下代码片段:

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()不工作。有什么想法吗

tkqqtvp1

tkqqtvp11#

你也需要在renderAlbums中执行return

renderAlbums() {
   return this.state.albums.map(album => {
        return (
            <Text key={album.title}>{album.title}</Text>
        );
    });
}

相关问题