-
-
Save wbobeirne/828ed66529293baa450495e3d967f730 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import API from 'lib/api'; | |
export default class PostForm extends React.Component<{}, State> { | |
state: State = { | |
posts: null, | |
isFetching: false, | |
error: null, | |
}; | |
// As soon as this component mounts, start fetching posts | |
componentDidMount() { | |
this.fetchPosts(); | |
} | |
// Fetch posts from the API and store them in state | |
private fetchPosts = () => { | |
this.setState({ isFetching: true }); | |
API.fetchPosts() | |
.then(posts => { | |
this.setState({ | |
posts, | |
isFetching: false, | |
}); | |
}) | |
.catch(err => { | |
this.setState({ | |
error: err.message, | |
isFetching: false, | |
}) | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment