Skip to content

Instantly share code, notes, and snippets.

@astrotim
Created December 28, 2017 23:33
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 astrotim/9ea6ddbab10bbf0af9071a28c0e535b7 to your computer and use it in GitHub Desktop.
Save astrotim/9ea6ddbab10bbf0af9071a28c0e535b7 to your computer and use it in GitHub Desktop.
import React from 'react';
+import { createClient } from 'contentful';
class Home extends React.Component {
+ state = {
+ posts: []
+ };
+
+ componentWillMount() {
+ const client = createClient({
+ space: process.env.REACT_APP_SPACE_ID,
+ accessToken: process.env.REACT_APP_ACCESS_TOKEN
+ });
+
+ client
+ .getEntries({})
+ .then(response => {
+ this.setState({
+ posts: response.items
+ });
+ })
+ .catch(console.error);
+ }
+
render() {
- return <p>Home</p>;
+ if (!this.state.posts.length) return <p>No posts found.</p>;
+
+ return this.state.posts.map(post => {
+ console.log(post);
+ return <p>Post</p>;
+ });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment