'state'未定义(no-undef)

时间:2018-04-21 15:34:48

标签: react-native state jsx

我是完整的菜鸟,我只是在udemy上的反应原生教程中学习。但是,我已经到了墙,无法在任何地方找到解决方案?

目前我从ESLint收到错误,显示状态未定义。

enter image description here

以下是完整的代码:

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

class AlbumList extends Component {
 state = { albums: [] };   //state is underlined

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

    renderAlbums() {

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

export default AlbumList;

是否有关于在React-Native中定义'state'的更新?

真诚地感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

试试这个。

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

class AlbumList extends Component {
    constructor(props){
        super(props); 
        this.state = { 
            albums: [] 
        };
        this.renderAlbums = this.renderAlbums.bind(this);
    }

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

    renderAlbums() {
        return (
            <View /> // return your Albums here as you need
        );
    }

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


export default AlbumList;