将console.log添加到类主体会导致错误

时间:2017-07-13 01:58:03

标签: reactjs react-native

当我将console.log(3);添加到下面的类时,它会抛出错误

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

    console.log(3);

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

      renderAlbums() {
        console.log(5);
        return this.state.albums.map(
          album => <AlbumDetail key={album.title} myAlbum={album} />
        );
      }

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

    }

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要将console.log(3)放在任何方法中,例如您可以将其添加到componentDidMount()

componentDidMount() {
  console.log(3)
}