[英]ExceptionsManager.js:84 Unhandled JS Exception: ReferenceError: response is not defined
I have a component that look like this 我有一个看起来像这样的组件
import React, { Component } from 'react';
import { View, Text, StyleSheet } 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>{album.title}</Text>);
}
render() {
console.log(response.data);
return <View>{ this.renderAlbums() }</View>;uy
}
}
export default AlbumList;
This is my entire index.js look like 这是我的整个index.js看起来像
// 1. import library help create a component
import React from 'react';
import { AppRegistry, View } from 'react-native';
import Header from './src/components/Header';
import AlbumList from './src/components/AlbumList';
// 2. create component
const App = () => (
<View>
<Header headerText={'Albums'}/>
<AlbumList />
</View>
)
// 3. render to the device
AppRegistry.registerComponent('albums', () => App )
When I run my app, I kept getting 当我运行我的应用程序时,我一直在努力
ExceptionsManager.js:84 Unhandled JS Exception: ReferenceError:
response is not defined
This error is located at:
in AlbumList (at index.js:11)
in RCTView (at View.js:43)
in App (at renderApplication.js:32)
in RCTView (at View.js:43)
in RCTView (at View.js:43)
in AppContainer (at renderApplication.js:31)
As you can see, I declared it right here 如你所见,我在这里宣布它
.then(response => this.setState({ albums: response.data }));
How would one go about debugging this further? 如何进一步调试?
You are getting the error in the render
method since response
is not defined there. 您在
render
方法中收到错误,因为没有在那里定义response
。
render() {
console.log(response.data);
return <View>{ this.renderAlbums() }</View>;
}
Write this.state.albums
instead. 写
this.state.albums
。
render() {
console.log(this.state.albums);
return <View>{ this.renderAlbums() }</View>;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.