How to Build a React Portfolio Website Powered by Cosmic JS


In this tutorial I'm going to demonstrate how to build a photography portfolio website using React, Styled-Components and Cosmic JS. Let's get started. 

TL;DR

View and install the demo
View the code on GitHub

Intro

Cosmic JS provides a good backend for your webapps. It is a fully-featured content management system (CMS) with numerous options that allows everyone on your team to collaborate to manage content. Cosmic JS provides many options for developers: Bucket Admins can assign different roles for their team members, create single or multiple relationships between Objects and much more. 

In this example I use:

  • the multiple objects relationship metafield
  • images / files
  • default objects input

ReactJS: easy and flexible JS library to create a client side app. The React CLI allows you to create single page apps with no configuration.

StyledComponents: alternative for Static CSS, allows you to build separate components to enhance your app and make it expandable.


Getting Started

1. In your Cosmic JS dashboard click "Add New Bucket".

 1.1 Install an App - Install an App button, assign the application name and "Save Bucket".  After that,  select the application from the list of applications and click "Install free". And you created all the objects necessary for the application. The last thing to do: Copy the API key from the Cosmic JS desktop> Basic settings> Bucket slug.

 1.2  From Scratch - start from scratch button, assign the application name and "Save bucket".  For this app, you need to create three objects type. Images, Categories and Sites. 

Images: Default inputs plus additional metafields called: "img"  as image/file  object type checked as required. 

Categories: Default inputs plus extra metafields called: "images" as multiple objects relationship (select limit Search by Images) and Image/file object called: "img". 

Sites: Default inputs type plus additional metafields called: "picture".


I've created the Bucket for my application, and am now ready to dive into the code.  Link for your Bucket Slug.


Code

Clone the repo from Github or you can create from scratch. This package shows all dependencies used for app:

After you've installed React and all dependencies, you are ready to write code.

All data is downloaded separately, a parameter is sent in the react-router. After moving to a single post, a single element is fetch and rendered in component.


A brief explanation about the file structure.

- Our first task is to create a file structure for the project. 

App.js default , folder component: 

buttons (all created buttons),

headers (all headers),

page (all page),

parts (all stateless  only const styled components),

section (class and functional components that manage data and import other component from parts file),

layout (app layout global style theme provider).

utils (color and theme object).


What's Inside

App.js  file that is located routing and layout import, state for interactive elements. Here we pass data that we can pick up in another component as props.


Section 

Category.js is rendering as a home page. This displays data downloaded as props from the app.js component.  It also imports styled components from the "parts" folder.

PartGrid.js - state component collects data from Cosmic JS. The page with photos in the selected category contains a link to a single photo.

Single.js - It is also a component containing state. Receives data for a single photo from Cosmic JS.

 

Layout.js -  contains theme provider and global style. Since we used the theme provider, we can connect CSS with React and use CSS variables depending on props from the component.

Nav.js - separate state component with own styled-components and state.

Parts 

In this folder there are only single tags HTML styling in CSS (styled-components) and exported as separate components.

Buttons and Headers

Individual HTML tags styling in styled-components): h1, button etc.

Page

Contact.js - State component that fetch data from Cosmic JS and display in app. 

Utils

All global variables that we can use in our app like colors, media query, font-size etc. Variables from the files we can pass as props to other components and use in styled-components.

Conclusion

We've just created a simple app using styled-components, React JS and Cosmic JS. At the beginning we saw how to create the Bucket for our app. We then created a boilerplate using the React CLI and installed dependencies. After creating the folder structure and styling components in CSS, we downloaded data from Cosmic JS to display in the application.

Cosmic JS has a great API for every application, coupled with many options for the developer and content editors that allow you to manage content seamlessly. Styled components allows you to create components that are independent, so an application can be created by many people without causing conflicts like (class name in css ) etc. In one command the React CLI allows us to create the boilerplate for our app. 

I encourage you to install the application, add your own components and extend the functionality. Have fun!

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

Was this article helpful?

Thank you! Your input helps us improve our articles and resources. If you have any other questions or comments, please contact support.

You may also like


The Cosmic JS team was honored to to speak at Toyota Connected for the Ember.js Dallas Meetup this week. Cosmic JS co-founder Tony Spiro presented "Building Modern Apps with API Services" to illustrate the growing trend of offloading core dev tasks onto best-in-class API services.
Revision History is available for all of your Cosmic JS Objects. This means that you have the power to go back in time to previous versions, save new versions without publishing live and more.
Video: Build a Cosmic JS-powered blog using Gatsby
In this tutorial, I’m going to give you an overview of Mux Extension, talk about the benefits of adding Mux to your Cosmic Bucket and show you how to install it to your Bucket in a few clicks.
Here we build a progressive web app in React.js with the power of Cosmic JS. It respond with status 200 and carries data when you disconnected to internet and in Chrome browser (both mobile and desktop) it gives an option to install the app on home screen.
A CMS API allows content creators and developers to work simultaneously creating the best content and building applications with popular technology.