Ways to style a React Component

React Js components have become building blocks of UI these days. If your an absolute beginner to React like me, have you ever wondered how to style an React component, then this article if for you. Having the same question on my mind, I started exploring all possible ways of styling an React component. In this article, we will go through the various ways of styling a React component by developing an ToDoApp.

Akhil Reddy Mallidi
#ByCodeGarage
16 min readJul 14, 2019

--

By the end of the article, we will be developing an ToDoApp using React js . First we should plan our project like how many components our app should have, what are all the elements that should be there in each component. I will skip how to create an React App.

If you want know about how to create an React App, go through my previous article : Getting started with React — for Beginners.

Let’s have a look of our ToDoApp, we will get finally.

To see the live version of our ToDoApp, follow this link- Link for the live version of ToDoApp.

Strictly for PC’s, bcoz i haven’t used any bootstrap or media queries to make it align for small screen devices. You can play around it :)

Let’s get started. Initially, i have divided our ToDoApp in two main components. One for navbar and another component for handling Tasks. Our react project directory looks somewhat like this.

Project Directory of our React App

Now create a folder named components in the src folder. These components folder will hold all of our app components in it. In that components folder create two folders namely Navbar and Tasks. And those two folders should also contain and .js and .css files with any name you like. I will go with our Component name. It is the best practice to go with component name, so that you won’t get confused with those files while handling complex projects. Now our project looks like this.

Project directory of our app

First we will develop an logic for our ToDoApp and then we will style our app by going through all ways of styling an React component.

Our Navbar should contain only the name of our app. Let’s start coding it in Navbar.js in Navbar component. We will style it next section of this article.

Importing our Navbar component, into App.js and viewing it after it is being rendered.

Start the development server using this command:

You will get output some thing like this in the browser after our Navbar component being rendered in the App.js.

Preview of our App

Now, let’s jump to our Tasks component. Create an another component TaskAdd, with .js and .css in it and also Tasks.js and Task.js along with .css files in the Tasks component. Our Tasks component looks some what like this.

Task component overview

I have designed our Tasks component, in such a way that our App.js passes an reference of array that contains tasks remaining to Tasks.js which in turn maps every task in the array to Task.js, where Task.js is a functional component which will display every individual task.

TaskAdd component will holds elements like input field and also an button, where we can get the value of the task and add it to tasks remain array after clicking that button. Let’s start coding TaskAdd.js component.

Now we will be importing our TaskAdd component into our App.js. Also we will be adding an button in App.js after the Navbar, by which we will be handling the Night Mode and Light Mode of our ToDoApp.

The preview of our ToDoApp looks some what like this when rendered in App.js component.

Preview of our ToDoApp

Let’s create a state in App component, where it contains addTask which will be used to store the value that was inputed by the input field, tasksRemain is an array that holds the values of tasks remaining and darkMode contains boolean value true or false, that will be triggered using Mode button.

Here,I am hardcoding two tasks in taskRemain array. You can leave it empty if you want. To catch the value that was typed in the input field, we will now create a function in the App component, that handles the catching of the value that was typed.

By triggering an event, we are assigning the task to addTask variable of the state. Now we will write a function that will push the addTask value to taskRemain array. This function will we triggered after clicking only Add button.

Now pass these two functions reference to the TaskAdd component. So that we will trigger them when an event occurs in the TaskAdd component.

Now using props in TaskAdd component we will assign the references of these two persons to the events. addTaskHandler() will be triggered when an onClick event occurs in the button element. onChangeHandler() will be triggered when an onChange event occurs in the input field.

To display all the the tasks, that are pushed into the tasksRemain array. Now we will pass the reference of tasksRemain array to Tasks component. In Tasks component we will access every task in the array and pass them as individual elements to the Task component, where we will display every task. We are now writing an removeTaskHandler(), in App component which takes index value of an element as a parameter and then removes that task from the list.

Now we are coding our Tasks component that will displays elements in the remaining tasks array.

While iterating over an array, we will have an index to each element in the array. Also we are assigning an key to the elements, because an unique value needs to there as a key for identifying every element uniquely. Task component is as follows.

Let’s look at our app :)

Preview of our App

Hurrah..!! Done with the logic of our app. Let’s start styling our app. Styling can be done for a React component in four ways.

CSS Stylesheets

Like normal styling of an HTML element we can also style the elements in our React component using stylesheets. Creating classes that holds the styles that we want to render on our html element in a .css file. Then importing that .css file into our React component and initializing to the className attribute of that element will solves our problem. Let’s start styling the Navbar component of our ToDoApp.

Preview of our app

Here i have applied style to the html tag directly (header). So no need of initializing to the className attribute of that element. Just importing .css file into the Navbar component gets our job done.

Navbar component part is done. Final code of Navbar component looks like this. Have a look of our app after applying style to Navbar component.

Preview of our app

Now let’s apply the similar type of styling for the two sections that we have observed in above picture. Such that TaskAdd should be at the left position. Next to that, right side Tasks Remaining should be displayed. Now add the style which includes float property set to left in the TaskAdd.css file to meet our requirements.

Here we have used class, so we need to import this .css file in the component and also should be assigned to the className attribute of the element where we want our styled class to be triggered.

Lets add the similar type of styling for Tasks components. so that all the remaining tasks will be shown at the right side of our app.

Now importing that .css file into our Tasks component. Let’s start importing now.

Let’s have a look of our ToDoApp after applying style to these two components.

Preview of our app

Now it looks some what pretty, when compared to previous version. Let’s add an inline style to the mode button which is highlighted in the above picture.

Inline CSS

React allows to style our component using Inline CSS. Attributes of normal CSS are passed as Javascript objects and assigned to the style attribute of the element we are trying to style. Since they are passed as Javascript object, CSS attributes they are written in the form of CamelCase . For example, background-color is written as backgroundColor.

Now let’s add an inline style to the Mode button in the App component.

In the similar manner, add inline styles to some remaining elements in our components. Lets add an background color to our App component, because it will be helpful while we are handling Dark Mode to switch colors in our app.

Similarly, add style to the TaskAdd component’s input field, heading and button.

Similary add inline style to the heading and un-ordered list of the Tasks component.

Similary now we are adding inline style to text and the completed button in the Task component.

Let’s have a look of our ToDoApp after applying these inline style’s to our components.

Preview of our App

Conditional styling or Dynamic styling

It is not a different type of styling, but React allows us to apply style to our components based on some conditionals or we can pass class names to the elements in our component dynamically. Let’s add these conditional styling to our app to make it look more prettier.

Now start with the dark mode option. When an onClick event is triggered near Mode button element in the App component, then the background color, font color and also the should display the mode dynamically. So that the same button can be used to turn our app into dark mode and can also turn our app back to light mode. We will do that by conditonal styling.

The above conditional styling changes the background color based on the boolean value darkMode. If it is false, light mode is triggered which is set as default. In case if it is true, if enters the conditional statement and reset the style properties in it.

To display the mode near the button dynamically, such that it will display Light Mode when our app is in dark mode and vice versa. We will achieve it by using ternary variable. Let’s have a look.

Now let’s make the button to change its color based on the Mode. We will achieve it by dynamically adding classes to the element based on some conditionals.

Create classes in the App.css file for background color, bold and font-color and import the .css file into our App component.

Here we added bold, white-font, black-font, orange-bg and also green-bg. Also we have imported the Google font and applied as common property. Let’s add these classes dynamically to the HTML element in our App component.

Let’s have a look of our app in both modes after applying these styles to it.

Preview of our App in Light Mode
Preview of our App in Dark Mode

We had an error in our App. Anyone observed?? It a tiny one..!!

If every task is removed by marking it as completed, then it is showing nothing. We should add any conditional statement to which checks the length of the tasksRemain array. If it is zero, then it should display No Tasks. This will make our app more user friendly. Also add an alert type background for every task based on the mode. It makes our app to look more attractive. We will achieve this by adding classes dynamically.

Also add these alert type background styles for Task component. Here we added only for No Tasks indication.

Tadaaa..!! We have completed styling of our app. You can have your own ToDoApp for your daily use that was developed using React. Let’s have a look of final version of our app.

Preview of our app in Dark Mode
Preview of our App in Light Mode
Preview of an App when there are no tasks

There are three more ways to style an React component. I haven’t used in the project. But we will go through them now.

Styled Components

Styled-components is a library for React. That helps us to use component-level styles in our app, where code will be a mixture of Javascript and CSS. For using styled components in our app, we need to install it first using this command.

Styling a component using styled components looks some what like this.

Component styled using Styled Components

CSS Modules

CSS Modules is similar to styling an React component, like CSS stylesheets. In CSS Modules, the classes we had created are locally scoped. That feature we will not get in CSS external stylesheets. This helps a lot that these CSS classes won’t clash with each other or overide with another, eventhough they are declared with same name. By configuring CSS Modules, in the config folder the class names we have defined will be created something like [APPNAME__CLASSNAME__HASHCODE ] based upon the rule we declared.

To know more about how to configure CSS Modules go through this article- Article

Radium

Radium is library for React. The only cons we had while applying Inline CSS is we can’t apply psuedo selectors or any media queries. This Radium library helps us to apply psuedo selectors and also media queries in our Javascript objects and can pass as Inline styles to our React component.

To use Radium in our project, we should install it first. Install Radium library using this command.

We will appy psuedo selectors to the elements somewhat like this. And we will wrap our component with Radium while exporting to apply these selectors.

We had gone through all the ways of styling an React component. There is no any particular rule that states to follow any specific type of styling. It’s one personal choice to follow a specific way of styling an React component.

Github Repository link- https://github.com/itzzmeakhi/ToDoApp/tree/master

Live ToDoApp linkhttp://bit.ly/react-ToDoApp .

Strictly for PC’s, bcoz i haven’t used any bootstrap or media queries to make it align for small screen devices. You can play around it :)

If you are trying to deploy your app to Github pages, follow this guide

Hope you liked this article :)

Do share the story and clap it if you liked it.

Let me know if you have anything to ask :)

Know more about me- itzzmeakhi.me

--

--

Akhil Reddy Mallidi
#ByCodeGarage

I seek out new knowledge and actively develop new skills. Loves to write. (http://www.itzzmeakhi.dev)