Skip to content

Instantly share code, notes, and snippets.

@barraponto
Created October 11, 2018 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barraponto/3979834db4d82a1f866c80517fc8ec08 to your computer and use it in GitHub Desktop.
Save barraponto/3979834db4d82a1f866c80517fc8ec08 to your computer and use it in GitHub Desktop.
import React from 'react';
import axios from 'axios';
import spinner from '../assets/spinner.gif';
class Library extends React.Component {
constructor(props) {
super(props);
this.state = { books: [], loading: false }
}
// COMPONENT LIFECYCLE HOOKS
componentDidMount() {
this.setState({loading: true}, () => {
axios.get({})
.then(res => this.setState({books: res.data.books, loading: false}))
.catch(err => this.setState({loading: false}))
});
}
render() {
return (
<ul className="books">
{ this.state.loading && (<img src={spinner} alt="loading..." />) }
{ this.state.books.map(book => (<li className="book">{book.title}</li>)) }
</ul>
);
}
}
class ReduxLibrary extends React.Component {
componentDidMount() { this.loadBooks(); }
render() {
return (
<ul className="books">
{ this.props.loading && (<img src={spinner} alt="loading..." />) }
{ this.props.books.map(book => (<li className="book">{book.title}</li>)) }
</ul>
)
}
}
export default Library;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment