Skip to content

Instantly share code, notes, and snippets.

@wbobeirne
Created May 7, 2019 22:50
Show Gist options
  • Save wbobeirne/828ed66529293baa450495e3d967f730 to your computer and use it in GitHub Desktop.
Save wbobeirne/828ed66529293baa450495e3d967f730 to your computer and use it in GitHub Desktop.
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