gpt4 book ai didi

javascript - 找不到状态变量

转载 作者:行者123 更新时间:2023-11-29 15:13:39 25 4
gpt4 key购买 nike

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: Can't find variable: albums ”它在工作后突然冒出来,你知道发生了什么吗?

最佳答案

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

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

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

关于javascript - 找不到状态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51771341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com