Skip to content

Instantly share code, notes, and snippets.

@ldco2016
Created February 28, 2018 05:01
Show Gist options
  • Save ldco2016/e9898ffebf2172af9f4761897b552722 to your computer and use it in GitHub Desktop.
Save ldco2016/e9898ffebf2172af9f4761897b552722 to your computer and use it in GitHub Desktop.
// Import a library to help create a component
import React, { Component } from 'react';
import { Text, View } from 'react-native';
import axios from 'axios';
// ES6 Class Component
class AlbumList extends Component {
// initializing component level state
// to get access to data and rerender
// this.state.albums equal to empty array
state = { albums: [] };
// lifecycle method to
// fetch data as soon as app boots up
componentWillMount() {
axios
.get('https://rallycoding.herokuapp.com/api/music_albums')
// called once http request is complete with response and data
// inside object in chrome console dictated use of response.data
// passing albums object to setState because updating state
// this.state.albums no longer equal to empty array
// setState need not be defined, comes with Component class
.then(response => this.setState({ albums: response.data }));
}
render() {
// result of adding state code
console.log(this.state);
return (
<View>
<Text>Album List!!</Text>
</View>
);
}
}
// Make the component available to other parts of the app
export default AlbumList;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment