How to build a React Redux application with API-first CMS Contentful

When I first started to re-design my company website I was very excited to use ReactJS to build the front-end application in the React way. But soon I realised, in the excitement of using the new JS library I completely forgot about the content part. Who will send me dynamic contents?

And seriously this time I was not in a mood to develop a backend app with Rails or NodeJS. Rather I wanted someone else to handle the role of a typical Content Management System (CMS).

React + Redux + Contentful — the concept

The idea is very simple! There will be a CMS who will serve data through APIs and we will consume those API data in our React application.

While I was looking for a ready-made CMS, I came across Contentful. Contentful is a API-first CMS and they says ‘Editors get a CMS, Developers don’t have to deal with one’.

And we will manage our application’s data flow with Redux. Redux maintains the state of an entire application in a single immutable state tree (object), which can’t be changed directly. When something changes, a new object is created (using actions and reducers).

So Let’s get our hands dirty with a small blog application powered by — — React-Redux-Contentful.

Getting Started

We can start by creating a project folder and initializing a `package.json` file with `npm init`. But to speed up initial setup I’ve created that for you. Just simply follow the github link and checkout the repo.

After checkout go to the project folder and install the dependencies with

npm install

Then start the app with npm run start and go to http://localhost:8080 .

We will write most of our codes in src directory. Now let’s setup a basic homepage by creating a new file ./components/posts_index.js and adding a basic React component.

import React, { Component } from 'react';
class PostsIndex extends Component {
render() {
return <div> Our blog posts will come here.</div>;
}
}
export default PostsIndex;

Import the PostsIndex component ./routes.js and add it as a IndexRoute.

import PostsIndex from './components/posts_index';
export default (
<Route path="/" component={App}>
<IndexRoute component={PostsIndex}/>
</Route>
);

To make these changes visible, update the return statement of ./components/app.js with the following

return (
<div>{this.props.children}</div>
);

Now go to the browser, refresh the page and see the changes.

Fetch blog contents

Next we’ll integrate with Contentful so we can fetch and show blog posts dynamically.

Steps to setup your Contentful account

  1. Signup at Contentful (you can choose Free package, that will be sufficient for now) and create an empty space.
  2. Create a new Content Type and add few fields to it. I’ve created a content type post with the following fields: title, description and featuredImage.
  3. Add some contents for the defined content type.
  4. Get your API access_token and space_id .

Back to our app

First install the following npm packages: axios and redux-promise.

npm install axios redux-promise --save

Import and use redux-promise middleware in ./index.js file.

import promise from 'redux-promise';
const createStoreWithMiddleware = applyMiddleware(promise)(createStore);

Making API calls

Now we will add fetchPosts action creator in ./actions/index.js file and use axios to make a GET request to Contentful server.

import axios from 'axios';
export const FETCH_POSTS = 'FETCH_POSTS';
const API_BASE_URL = 'https://cdn.contentful.com';
const API_SPACE_ID = 'buyxxxxxxx';
const API_TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
export function fetchPosts() {
const request = axios.get(`${API_BASE_URL}/spaces/${API_SPACE_ID}/entries?access_token=${API_KEY}&content_type=post`);
return {
type: FETCH_POSTS,
payload: request
};
}

fetchPosts action creator will return an action , which is nothing but a plain JavaScript object with a type property. In Redux Actions describe the fact that something happened, but don’t specify how the application’s state changes in response. This is the job of reducers.

To do that we will add a reducer in ./reducers/posts_reducer.js and handle the response we got from the action.

import { FETCH_POSTS } from '../actions/index';
const INITIAL_STATE = { all: [] };
export default function(state = INITIAL_STATE, action) {
switch(action.type) {
case FETCH_POSTS:
return { ...state, all: action.payload.data.items };
default:
return state;
}
}

Next we will update the application state in ./reducers/index.js file.

import { combineReducers } from 'redux';
import PostsReducer from './posts_reducer';
const rootReducer = combineReducers({
posts: PostsReducer
});
export default rootReducer;

Displaying posts

To get our app working, now we’ll promote the PostsIndex component to a Redux container in ./components/posts_index.js file.

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { fetchPosts } from '../actions/index';
class PostsIndex extends Component {
componentWillMount() {
this.props.fetchPosts();
}
renderPosts() {
return this.props.posts.map((post, index) => {
return (
<article key={post.sys.id}>
<h3>{post.fields.title}</h3>
<p>{post.fields.description}</p>
</article>
);
});
}
render() {
return (
<div>
<h2>Blog Posts</h2>
{this.renderPosts()}
</div>
);
}
}
function mapStateToProps(state) {
return { posts: state.posts.all };
}
export default connect(mapStateToProps, { fetchPosts })(PostsIndex);

Then go to the browser, refresh the page and check the results.

Now the existing UI may look very dull, only few headlines followed by some paragraphs. To give our app a proper layout, I’ve already included Bootstrap in this project. You can simply make use of Bootstrap CSS classes by adding a className attribute to the HTML tags. And I leave that part to you.


Wrap Up

React is a very powerful library that makes it easy to build universal apps. Supercharged with Redux and combined with Contentful, you can quickly develop CMS powered blogs and websites.

Though in our application we only used Content Delivery APIs of Contentful. Make sure to check out four content APIs which can be used for different use cases like adding a new blog post, updating existing posts, fetching media contents etc.


The finished code of this tutorial is available on basic branch of this Github repository. I’ve done some extra coding and added a simple HTML5 template to the app, and also deployed it on Heroku. Deployed code is available in master branch of this Github repo.



Thanks for reading!
I Hope you liked this approach of building a server-less application with React. If you have any questions or suggestions regarding this article, feel free to comment down below.
Finally if you found this article helpful, recommend it, share it and support it. Peace!