Contentful app startup
how to get some Contentful data on a React app
Last updated
how to get some Contentful data on a React app
Last updated
From the very beginning on the command line, let us create a project folder, say jonolist
:
% mkdir jonolist
Then, inside the folder, we initialize the project:
% npm init
We hit return (or Enter) to accept the defaults and then install the following npm packages: contentful, react, react-markdown and react-router-dom:
% npm install --save contentful react react-dom react-markdown react-router-dom react-scripts
contentful relays the data entries to our app
react displays the data
react-dom allows us to use JSX syntax
react-markdown converts the Markdown from the entries into HTML tags
react-router-dom gives our single-page app the appearance of a "website" by having "routes" or, in layman's terms, pages
react-scripts lets us see our stuff while coding (via npm start
)
Going into Contentful, we would like to build a minimal content model as a test:
From the tabs, select "Content model"
The "Content model" is a blueprint of we want to represent
e.g. a content model could be "Country"
Look for a button that says "Add content type"
Fill in the blanks as shown above
Name - what to call our content model, e.g. Article
Api identifier - a computer-readable version of the name (alphanumeric with dashes)
Description - optional helper text
Next we will fill in fields (each content model record's data)
e.g. for our content model Article, our fields could include:
Name - the article's name (not to be confused with "Article")
e.g. "5 reasons why I love Contentful"
Content - the article text
Author - your name ;)
use your intuition to fill these out, taking note of things like validation and appearance
repeat this for as many fields as necessary
Finally, we will create a sample content (data set)
Go to the Content tab
Look for a button that says "Add entry"
Select the content type (i.e. the content model we just created)
Fill in as many required fields as possible
Publish the entry
We can now use our Content (i.e. entry) in a user interface!
Still in Contentful, let's obtain our access tokens that will allow our app to talk to Contentful:
Once logged in, go to Settings > API Keys
Go into the space (likely called "Blank 1" on Free accounts)
We will see a screen like this:
Take note of these two:
Space ID (our content's space name)
Contentful Delivery API - access token (our "passkey")
We would like to keep our Space ID and, most importantly, our access token secret, so we will create an .env
file on the root of our project folder:
Ensure that the variables begin with at least REACT_APP_
or they will not work!
Also, remove the brackets on the right side of each variable assignment, e.g.:
The minimal app will contain the following folder-file hierarchy:
The five files in the root /
folder need little explanation:
.env
- explained earlier in this article
.gitignore
- folders and files to ignore:
/node_modules
/build
.env
- important as you do not wish to push this to a public repository
README.md
- documentation
package.json
and package-lock.json
- third-party scripts used
The three files in the public
folder also need little explanation:
index.html
- the foundation HTML of a React app (we almost never touch this!)
manifest.json
- for progressive web app (PWA) stuff (leave alone for now)
robots.txt
- for search engine optimization (SEO) stuff (also leave alone for now)
Finally, the files in the src
folder involve the actual exciting user interface (UI) programming:
index.js
- the foundation JS of a React app (we almost never touch this!)
App.css
- just one stylesheet could do ;)
App.js
- where the magic finally happens
from here, we can create new files that this file can import!
the code below uses GraphQL (a querying language) whose syntax takes some getting used to)
Within the return
brackets, we can then pass props down to sub-components and increase the complexity of our app!
To run the app on your local machine, simply use the command:
NPM will then automatically launch localhost:3000
(or similar) to your browser; assuming you have entered sample content in Contentful, you should see a list of entries ;)
Play around with the GraphQL query and enjoy building!