Javascript 在React Native中显示基于HTTP GET响应的列表

Javascript 在React Native中显示基于HTTP GET响应的列表,javascript,reactjs,react-native,jsx,react-native-ios,Javascript,Reactjs,React Native,Jsx,React Native Ios,我正在尝试根据HTTP GET响应将列表显示到我的视图库中 我有 import React, { Component } from 'react'; import { View, Text } from 'react-native'; import axios from 'axios'; class AlbumList extends Component { state = { albums: [] }; componentWillMount() { ax

我正在尝试根据HTTP GET响应将列表显示到我的视图库中

我有

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

class AlbumList extends Component {

    state = { albums: [] }; 

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

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

    render() { 
        // console.log(this.state);
        console.log(this.state.albums);

        return (
            <View>
                { this.renderAlbums() }
            </View>
        );
    }
}

export default AlbumList; 
import React,{Component}来自'React';
从“react native”导入{View,Text};
从“axios”导入axios;
类列表扩展组件{
状态={相册:[]};
组件willmount(){
axios.get()https://rallycoding.herokuapp.com/api/music_albums')
.then(response=>this.setState({albums:response.data}));
}
renderAlbums(){
this.state.albums.map(album=>{album.title});
}
render(){
//console.log(this.state);
console.log(this.state.albums);
返回(
{this.renderAlbums()}
);
}
}
导出默认列表;
我一直空着

但我似乎无法在控制台中看到我的反应

更新 修好这条线之后

返回这个.state.albums.map(album=>{album.title})

我仍然明白这一点

TypeError:this.state.albums.map不是函数


我希望看到这样的事情


renderAlbums
中,您缺少一个
返回值

renderAlbums () {
  return this.state...
}

值得一提的是,
componentWillMount
不应该用于获取数据(并调用
this.setState
)-您应该将它放在
componentDidMount

中,我现在只是在学习教程,但是的,我会考虑您的建议。:)你还发现了什么吗?事实上,我有一个动作。加载屏幕,当请求发出时会被推,当请求成功时会弹出,因此动画在弹出时不会工作,因为动画在屏幕创建过程中只工作一次。我似乎尝试了你的固定代码,它正在工作。你们觉得你们可能遗漏了什么吗?我在世博会网站和应用程序中也看到了。我不知道为什么它对我的本地。。。我应该重新启动吗?如果是
react native init
项目,您可以尝试重新启动捆绑服务器。尝试删除缓存也可能有所帮助。我不知道你为什么也会出错。我想是因为我没有你那种风格???
map
函数仅适用于数组。确保您得到一个数组作为响应,并且在状态中设置了相同的数组。