无法找到状态变量

时间:2018-08-09 15:51:54

标签: javascript react-native

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(){
        return this.state.albums.map(album => <Text>{albums.title}</Text>)
    }

    render(){
        console.log(this.state);
        return (
            <View>
               {this.renderAlbums()}
            </View>
    );
    }
}

export default AlbumList;

上面写着“ referenceError:找不到变量:专辑”,它在工作后突然冒出来,你知道发生了什么吗?

1 个答案:

答案 0 :(得分:1)

看起来与您的映射变量不匹配。在:

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

您使用album作为该函数的变量名,但随后引用了albums.title