Javascript 找不到状态变量 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=>{albums.title}) } render(){ console.log(this.state); 返回( {this.renderAlbums()} ); } } 导出默认列表;
上面写着“referenceError:找不到变量:albums”。它在工作后突然出现,你知道发生了什么吗?看起来与映射变量不匹配。在:Javascript 找不到状态变量 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=>{albums.title}) } render(){ console.log(this.state); 返回( {this.renderAlbums()} ); } } 导出默认列表;,javascript,react-native,Javascript,React Native,上面写着“referenceError:找不到变量:albums”。它在工作后突然出现,你知道发生了什么吗?看起来与映射变量不匹配。在: import React, { Component } from 'react'; import { View, Text } from 'react-native'; import axios from 'axios'; class AlbumList extends Component { state = { albums: [] } ;
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;
返回此.state.albums.map(album=>{albums.title})
您正在使用
album
作为函数的变量名,但随后引用了albums.title
将renderAlbums(){
更改为renderAlbums=()=>{
谢谢Dan!:)哦,是的,成功了!
return this.state.albums.map(album => <Text>{albums.title}</Text>)