About Node.js

I welcome you all to the article about Node.js where I would like to introduce this platform and show some example. The article is written primarily because of the lack of resources and articles in…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How To Style React Components

React has become one, if not the most popular UI library in the developer community. It was deployed as an open source project by Facebook in 2013. The engineers at Facebook made it very easy to start using react by providing developers a unique tool called create-react-app. This tool gives you a head start in using react, it bypasses the setup and configuration process and spins up a basic react application.

In order to use the create-react-app tool you need to complete the following steps:

Once you complete these steps you should be ready to start using create-react-app. The tool provides a basic react application with minimum styling, but in this blog I will explain some of the ways you can add styling to your application.

You can add styling to individual elements in a react application by using the style property that all react elements have. The style property is expecting an object, the keys represent the css properties you want to change. The code below can illustrate this.

On our left we have a functional react component and we are trying to style the h2 tag using the inline style method. We are passing an object to the style property with the keys of color and backgroundColor. The second key might be confusing to some who have worked with or have css knowledge. In css the property used to change the background color of an element is called background-color, but in react we use camelCase as a result we have backgroundColor. The reason for this is that even though it might look like we are writing css we are technically writing Javascript. That also explains why the values provided to the properties are wrapped in double quotations.

The code above displays the following to the browser.

image from browser

This method of styling is more in tune with people that have worked with css before. You can give each element a className which is similar to the class attribute in html to style specific or groups of elements. The reason we use className instead of class is because class is a reserved word in react, meaning that this word is built into react provides some specific functionality.

But how do you use css in react? First you create an external stylesheet and convention is to use the same name as the component you are trying to style. The next step is to import the external stylesheet into your component. Below I have the images of the <App /> component and its corresponding css stylesheet.

App.css

As you can see in the App.js file specifically on line 2 we importing the App.css file that contains your typical css. This method of styling is better suited for applying multiple css property and it keeps the code more modular, meaning that it keeps your component file more clean, readable and manageable.

For those of you who might not know what SASS is I have a blog that goes into detail of what SASS is, some of its core features and why you should consider using it in your projects. In a nutshell SASS stands for “Syntactically Awesome Style Sheets” and it’s a CSS preprocessor that adds extra functionality to your CSS.

React makes it very easy to use any CSS preprocessor, in the case of using SASS we need to install a package in order to use it within a react application. run the following command on your terminal npm install node-sass.

To begin using SASS and all of it’s amazing feature we just need to make a couple of changes to our current files.

First we need to change the file extension of our App.css file to App.scss. This basically tells react to treat the file a scss file.

Second in our App.js file we need to change the extension of our import for the external stylesheet to from “./App.css” to “./App.scss”. Now you can take advantage of the awesomeness of sass.

I hope after reading my blog you have a better understanding on how you can add styling to your react components. Build a small react application and experiment, in my opinion this is the best way to learn.

Thanks for taking time to read my blog.

Add a comment

Related posts:

Digital transformation of the Luxury Automobile Industry

The global luxury automotive industry is one of the most valuable industries. It is emerging in countries like China, the United States, India, and some parts of Europe. This market is valued at…

Resources

The ASSURE Model helps teachers integrate technology with intention. It helps them to move beyond low-level use of technology such as “substitution” and “augmentation” to more advanced “modification”…

3. The Blues

My husband had to shower me because I was in so much pain and he had to empty my urine bag every 2–3 hours. My sleeping position was restricted to one side of the bed so I could strap the bag to a…