Best Practices for Styling React Components
Check out the various styling options available along with their related best practices.
Jun 26, 2019 • 4 Minute Read
Introduction
In any React app, components are the basic building blocks of that application. Styling these components is also critical because the client is going to interact with the app and needs to have a good overall experience in terms of look and feel.
There are different ways of styling our components, each having its own advantages and disadvantages.
We'll look at the various options options we have, as well as some of the best practices related to the same.
Inline CSS
We can add inline CSS, which are specified as attributes and are passed to the elements. These are specified as an object with a key as camelCased style name & value being the actual style value (and not as a string).
Below is an example of the same:
const myStyle = {
color: '#ffffff',
backgroundColor: '#000000',
};
function MyFirstComponent() {
return <div style={myStyle}>My First React Component!</div>;
This would get rendered by the browser as below:
<div style="color: #ffffff; backgrond-color: #000000;">My First React Component!</div>
CSS in JS
In this approach, we add a style element to the overall DOM, instead of attaching properties to the DOM node (as is done with the first approach, i.e. Inline styles).
Below is an example of the same:
import styled from 'styled-components';
const MyTitle = styled.div`
color: blue,
background: yellow
`
<MyTitle>My First CSS-in-JS React component!</MyTitle>
This would get rendered by the browser as below:
<style>
.hash999s99 {
background-color: black;
color: white;
}
</style>
<div class="hash999s99">My First CSS-in-JS React component!</div>
These are actually styled components and seem to be one of the best practices, as they only impact the specific component where they get rendered and do not affect any other place in the app.
Using styled components can be a very nice way of organizing our React components. We do not have to make our JSX code dirty with lots of div/span elements. We can simply render the components with their own styles. Also, since this approach would not have any inline styles, our code becomes easy to read. And, more importantly, we can make changes to our CSS anytime, without worrying about if it would have an impact on any other component.
CSS Modules
CSS Modules can be very useful to scope of CSS class names locally. Below is an example of how the same. We'll define two files:
- MyBtnComponent.css - This is our CSS module where we'll just define our normal CSS code.
- MyBtnComponent.js - This is our React Component which would render the button that'll use our CSS class defined above.
/*MyBtnComponent.css*/
.btn {
color: #ffffff;
padding: 0.5rem;
margin-top: 0.5rem;
border-radius: 4px;
background-color: #000000;
}
/*MyBtnComponent.js*/
import React from 'react';
import styles from './MyBtnComponent.css';
class MyBtnComponent extends React.Component {
render() {
return (
<button className={styles.btn}>
Click for CSS Modules !
</button>
);
}
export default MyBtnComponent;
The generated CSS would look like below:
.MyBtnComponent_btn_1EEOu {
color: #ffffff;
padding: 0.5rem;
margin-top: 0.5rem;
border-radius: 4px;
background-color: #000000;
}
Essentially, in the CSS module approach, we have a normal CSS file with basic/local CSS class names which go through a CSS Modules Compiler and we get an altered CSS version with renamed CSS class names.
Thus, "btn" is the local class name and the same gets converted to a global class name: "MyBtnComponent_btn_1EEOu".
All one needs to do is define the .btn{} inside the CSS module and refer this using styles.btn inside the component.
Conclusion
If we look at the pros and cons of the various approaches discussed, it seems like styling components within the components itself would be one of the best practices that can be followed. The right approach, though, can vary depending on your app specific needs.