How to Build a Blog using React Static and Cosmic

Community Articles
Community Articles How to Build a Blog using React Static and Cosmic

In this tutorial, I'm going to show you how to create a simple but blazing fast blog using React,  React Static, and Cosmic. Grab some coffee & let's build a cool app!

TL;DR

Demo
Download Source Code


Prerequisites

You need to have installed the latest stable version of Node JS and NPM. Let's get started with our technology stack. 


What is React Static?

React Static is a very fast and powerful progressive static site generator implemented based on React and its ecosystem. It's extremely lightweight, very fast, SEO-ready, and is the most React-friendly static-site library on the planet.


What about Cosmic?

Cosmic is a headless CMS which is used to build apps faster, together. It is very easy to store and retrieve data and is perfect for building apps like this.


Create React Static app

It is very easy to create React Static app. Just follow these steps:

Install the CLI tool:

$ npm i -g react-static
# or
$ yarn global add react-static

Create a new project!

react-static create


It will ask for name of project then give it as coffee-blog. After that it will ask for selecting template so you have to select basic.


Create new bucket in Cosmic

To create a blog app we will require data. For this we will be using the power of Cosmic. Create a free account on  Cosmic and create new empty bucket & name it as coffee-blog. For every blog it will have three object types:

  • Title
  • Content
  • Image

So add this property in bucket along with some data.  Follow this steps to create bucket in Cosmic.


Integrate Cosmic into React Static App

Create new file in coffee-blog project called config.js which will store the Cosmic Bucket, read_key & write_key.

Now we have to fetch data from Cosmic bucket, we are using graphql to do this. By default react static app will call jsonplaceholder api. So remove this code and replace below code into static.config.js.

We are not going to learn CSS in this tutorial so add below code inside the src/app.css file.


Inside the src/App.js file add below jsx code into the return function which will show the navigation bar.

For creating landing page where we are showing list of blog posts, change the src/pages/index.js with the below code. Here we are getting posts data that we have fetch using withRouteData HOC.

Create new file under src/pages called post.js which will have individual post. So when we click on any post from homepage then this component will render to show post details. Add below code in it.


Now we have created homepage and a post component & also fetch data from Cosmic using Graphql query.  So now run the below command to see the amazing coffee-blog.

$ npm run start

Open http://localhost:3000 to see your blog.

To build application in production mode you just need to run

$ npm run build

To test the production build before publishing, run below command

$ npm run serve


Deploy it

We can now deploy our application on any hosting platform. I recommend deploying it on Netlify because it supports project structures like this and can quickly create deployments.


Conclusion

In this article I have demonstrated you how to create a blogging application using React Static and Cosmic. The Cosmic Community has a lot of examples on how to handle integration with email functions, download functions, and third party platforms.

I really hope that you enjoyed this little app, and please do not hesitate to send me your thoughts or comments about what could I have done better.

If you have any comments or questions about building apps with Cosmic, reach out to us on Twitter and join the conversation on Slack.


You may also like


Thank you to everyone who joined us for our Halloween edition of our Wednesday Webinar series. In this presentation, we discussed making localization for websites less spooky using Gatsby and Cosmic.
Here I am going to explain the complete process to implement an Angular 6 blog, which uses Cosmic as back-end.
Step by step guide to build a professional developer's portfolio using React, Cosmic, and Semantic-UI
Cosmic Extensions make it possible to extend the functionality of Cosmic for both the developer and editor. A recent example built by the Cosmic Community is the Google Analytics Extension, which allows team members to easily see analytics insights directly in their Cosmic Bucket.
This is a JAMstack app with the power of Cosmic. In JAmstack, Vue.js is used as Javascript part, Cosmic as API and Nuxt.js as Markup.
Learn how I built the Twitter Search Extension to search and save Tweets to your Bucket.

Get Started with Cosmic

Build personal projects for free. Add your team at unbeatable prices.
Start Building Talk to Sales