Styling With React

Styling your applications with CSS can be tedious. Luckily for us, others have built pre-styled web components in which we can place our react components. There are a number of pre-styled web components out there. It's likely you've already come across Bootstrap, Material, or possibly Semantic. Luckily for us, React Bootstrap and React Material Web Components, and Segment already exist for us. By using these pre-defined components, we can stand on the shoulders of giants, and riff off of their styling. While each of these are different in their own right, they're all more or less the same in their structure of how to install them.

 

Installation:

Each of these have their own installation guides. You just need to make sure that you include the proper links within your header in your public/index.html file.

 

Using:

There's no point in reinventing the wheel. With these packages, you need only to import the required material, and then use them as their own JSX elements. Let's start with adding a linear loader to our page to make it apparent that we're loading data (the remainder of the code is from the previous lesson):

 

Creating New Components:

Now that we have our loading bar set, let's futz with some of the pre-supplied component structure from the documentation. Reddit has a list like structure, but let's revamp it to have cards. First we'll need to take the basic card from the RMWC documentation and change it up! First we need to make sure that we import the Card data and the Typography data.

Now we've got a solid card to display our data!

Adding a Grid for Our Cards:

We have our cards set up, but they're acting as block elements. What we need to do is place them inside of a grid! Luckily for us, instead of having to use flexbox or some other methods of adding grid data, we can just import the RMWC Layout Grid system:

 

Creating a new Component:

We've added our RedditCard to our APICall.jsx, but the file is getting too big! So, let's create a brand new stateless component just for our card:

Now we've got our APICall.jsx a bit cleaned up: