Creating a React and Reactstrap Card Gallery

For this tutorial, you will be utilizing React, npm, Node.js, Bootstrap, and Reactstrap. To keep all of the development environments static, I would advise everyone to use the same text editor like myself, Visual Studio Code, for this tutorial. If you are not familiar with React, I would recommend that you try this tutorial here first.

The purpose of this tutorial is to be able to familiarize oneself with React while creating a card game menu gallery.

Prerequisites: 

If you don’t have Node.js installed, I would recommend you install it from the source. If you do not have npm, I would recommend installing it for the source as well.

Once you have Node.js setup and npm, you will be able to install the other libraries through the terminal within Visual Studio Code terminal. Before we get into that, let’s have you create a folder and name it CARDUI once you have that built click terminal and open up a new terminal.

Type:

The npm init command will initialize creating a package.json file.

It will take you through several questions. You can leave everything defaults except for the entry point being index.html. Once you run through everything it will ask you if this is okay, answer yes.

Next run the following command, which will install the create-react-app:

What is create-react-app?

Create-react-app lets you scaffold and quickly create React apps with no build configurations.

Run the following to create a new app, naming it cardui:

You will now need to change directories to cardui by typing the following in the terminal:

Now we need to install two more libraries. Bootstrap and Reactstrap. Run the following commands in the terminal to install them.

Now Open up the cardui folder and then open up the index.js file in the src folder and then add the following line into the imports:

This adds a link to the Bootstrap CSS.

Components:

Now besides App.js we will be creating and using three more components in this React app. In addition to these three components, you will be utilizing two non-component JavaScript files that will be used to populate data for the cards and set filters to organize them by type. One of two additional non-component JavaScript files will be in JavaScript object notation, and the other one will be an array. We will export both.

Let’s start by having you create a new folder in the src folder named components and creating three new JavaScript files in there, CardInfo.js, Filter.js, and MainMenu.js. Then make another folder in the src folder named non-components and create two JavaScript filed named cards.js and filters.js. One last folder that you need to create is the images. Create that folder in the public folder. We will be using eight different images for this example supplied from opengameart.org. They are all under the CC0 license and are created by beouliin.

Open up filters.js and type in the following into it:

The const FILTERS will hold the types our cards can be, and this will be the filter attribute we will use to filter our card gallery.

Next, open up cards.js and type in the following into it:

This will be the detailed information for the eight different types of cards we have. It doesn’t matter what the information in any of the portions is. These, json const variable and list const variable, are just a generic example to show you how to use a json and list within a React application. You can change anything you want and fiddle around with it, but if you make any changes to type you should make those changes to the FILTERS list as well because that is where the check and balances come into play later on.

Let us create an App.css. We will set the background color of the body and set a class type and set its background color as well. Normally you would set a variable within a component and set the properties for those elements within it, but this works just fine as well. It depends on how much you need to style, per component.

Here is an overview of the structure and file structure below. The first image is an outline of how the components relate to each and the second image is the file structure in my personal workplace.

As you can tell from the image below, it is a representation of the parents and child components and how each prop will be passed to its parent component.

Below is a representation of my personal work space. You should all have the same structure of folders and files, up to this point, if you are following along.

Now inside your cardui folder, open up the src folder and open up App.js.

Replace the original Class App Extends Component code with the following:

Here we are replacing the standard code that comes with creating react app in App.js with our own which will render MainMenu only, as MainMenu will house all the other components.

FILTER.JS COMPONENT:

Now let’s open up the Filter.js file. You will be importing react components, Bootstrap card, and button components and the filter attribute list. Type in the following imports into it:

The Filter component will render the all the card images and the side navigational filter. You will create two constant variable, card and filterButtons. The const cards will be a property that filters through the MainMenu state called cards for a specific type and maps individual keys to each card at rendering. Keys will be assigned using Math.random().toString(36).slice(2). There is a multitude of issues with generating keys in such a manner, but it will suit us perfectly fine for the scope of this tutorial. You can read more about the complexities of stringifying floating point numbers here.

The class of col-2 and m-1 will create a column of two and a margin of 1.

For the constant variable filterButtons, we will map the attribute types FILTERS from filters.js into a variable and use the fat arrow function to return a button that will have a key which will be the attribute type with an onclick event handler which will run a MainMenu method called cardFilterSelect, which updates the cardFilter state.

We use a combination of Reactstrap and Bootstrap for the quick scaffolding of the front-end. We use the Bootstrap classes for the grid and flexbox class elements and Reactstrap for the button and button groups.

It should look like the following:

CARDINFO.JS COMPONENT:

Next open up CardInfo.js and import React and Reactstrap.

The class CardInfo will extend Component and we will create two custom methods, then render the class out. The two methods that will be created are renderCard and renderInfo. Both will take a single parameter known as the card. RenderCard will return a card component with a card image and renderInfo will return a list of all the card information. We separate each item into a list item in an unordered list, assign headings and variables. Remember this component, CardInfo, will only render if it not null – which can be triggered by the Filter component.

MainMenu.JS COMPONENT:

Now I want you to go ahead and open up MainMenu.js. Here we will be creating and returning the navigational bar for our gallery as well as the Filter and CardInfo components and create several states that will be able to be lifted into the App.js file.

At the top import the following:

The first thing that gets called here is the constructor with the component props. We call super and pass props into it. Now we can start initializing the states and their default values within it.

Within the MainMenu class, we will create three states, cards for the CARDS const that we exported from the card.js, selectedCard which will be assigned the default value of null to and cardFilter which we will also assign null to. The states of these variables will change later as the application re-renders. For instance, selectedCard will change from null to the card.id that is selected.

Now we will create two methods named cardSelect which will take cardId and cardFilterSelect which will take filterName. Both of these will use the setState to tell the components that it needs to be re-rendered and have its state updated.

Next, we will render everything and export it out. In the render output we combine all the elements into one div. The Navbar is part of the Reactstrap library and makes up the Title in the Navigational Bar.

The Filer component creates the gallery of cards as well as the side bar navigation. Its cards state represents the current state of the cards json const variable from CARDS. The cardSelect uses the fat arrow function expression to assign the cardId its proper cardId when it re-renders during the setState of that method. The filter variable is assigned the cardFilter state and cardFilterSelect uses the fat arrow function expression to assign it the current filterName being used when it re-renders during the setState of that current method that is being used.

The CardInfo uses the filter() method check if it pass the test of card.id being equal to the selectedCard.

For more information about Reactstrap, I personally recommend looking over Bootstrap and Reactstrap. They are well documented libraries, Bootstrap more so than Reactstrap.

The final product should look something similar or along the lines as the following screenshot. You download the source code here.

Thanks for following along with this tutorial. Please feel free to leave your comment below if there are any thoughts that you’d like to share.

Share this article

Leave a Reply

avatar
  Subscribe  
Notify of