The new Cosmic dashboard is now available in private beta 🎉
Get it now →

Component-based Website Development with React and the Cosmic CMS

Community Articles
Community Articles Component-based Website Development with React and the Cosmic CMS

A common approach to website development is to build components that can be reused across different web pages. The benefit to this approach is that you end up doing less duplicate work by managing the components rather than managing duplicate content across different pages.  Popular JavaScript frameworks like React, Vue and Angular utilize this concept on the application code level to much success.  How can we benefit from this approach on the content management level?

In this tutorial I'm going to show you how to build a component-based website using React and the Cosmic CMS. The goal is to save developers and content managers time and provide a scalable content model.


Build the Content Model

* Prereqs: This article assumes you have an understanding of Cosmic concepts: Object Types, Objects and Metafields.  If you need to brush up on these concepts, go to the Getting Started guide.

Using Cosmic Object Types and Metafields, we can create composable components that can be used across website pages.  For our example we will create three Object Types:

1. Pages
2. Sections (components used in Pages)
3. Team Members (used in the Team Section)

Let's look at each Object Type:


Pages

All Pages will include the following:
1. Title (Text)
2. Content (HTML Textarea)
3. Sections (Multiple Objects Relationship Metafield)

The Multiple Objects Relationship Metafield will be used to manage the Page Sections.


Team Members

Metafields:
1. Name (Text Metafield)
2. Image (File Metafield)
3. Quote (Plain Textarea Metafield)


Sections

The Sections Object Type will be more dynamic and allow for different Metafields.

Team Section

Metafields:
1. Headline
2. Multiple Object Relationship Metafield (Team)
- Name
- Image
- Quote

Add your team members:

Signup Section

Metafields:
1. Headline (Text Metafield)
2. Image (File Metafield)
3. Content (HTML Metafield)
4. CTA (Text Metafield)


Add Sections to Pages
Now that we have our Sections set up, we can add them to our Pages:


Code it up

Now that we have our content saved, let's do some coding. Download the React starter:

git clone https://github.com/cosmicjs/react-starter

Now go into the Page component and edit it to look like this:

As you can see, we've added Section List on our Page Object. Our Section List and Section components looks like this:

Output of the Section component is determined by the data passed via the section prop.


In Conclusion

The Cosmic CMS is a great solution for building component-based websites.  In our example, we created a Page Object with a Multiple Object Metafield connected to a Sections Object Type to add our Section components. The benefit to this approach is that we can now manage the section content in one place and reuse it across pages without having to go into each page to edit duplicate data.

I hope you found this useful. If you have any further questions, join our community on Slack and reach out to us on Twitter.

Photo by Lance Anderson on Unsplash

You may also like


A primary benefit of managing content API-first is that your content is transformed into portable and scalable content objects, available via API endpoint to create workflows with the third-party applications that your team uses.
This article will point out some of the advantages that a headless CMS offers. Hopefully, your pain point is covered in here or it sparks an idea for a new way of thinking about what you can do with a CMS.
To demonstrate how easy it is to get started with Cosmic, we'll utilize the Cosmic CLI to quickstart an Angular Ecommerce App. This Angular ecommerce app allows you to process orders from customers and accept Stripe payments for goods and services. Manage inventory in the Cosmic Dashboard.
This article walks through an approach to build a web application using Angular and Cosmic to help generate marketing leads for your team.
Cosmic is a fantastic headless CMS for powering your website, but if you write regular articles or often update site text, you might find it challenging to always come into the backend and make those changes (especially when you're on the go).
Build a fully functioning messenger that can send SMS messages to application users. We will discuss Server side rendering of a React front, and leveraging a simple api in order to make protected requests to Cosmic cloud services.

Get started with Cosmic

Personal projects are free. Scale up as you grow.
Start building Talk to sales