Styling Your React.js Application Using Emotion

Styling a React application can be challenging, especially if you want to keep your styles organized and maintainable. To help simplify this process, the Emotion library provides a higher-level abstraction on top of CSS.

What Is Emotion?

Emotion is a library for styling React applications that provides a simple and efficient way to manage your styles. It allows you to write CSS in JavaScript and provides a flexible API for styling your components.

One of the main benefits of using Emotion to style your React application is that it provides a more efficient way to manage your styles. For example, you can use identical class names in multiple components without the risk of naming collisions that arisewhen working with CSS/SASS.

4

The Emotion library applies your styles only to the components that use them rather than the entire page. This can help you avoid conflicts with other styles on the page and make your code more modular and reusable.

How to Install Emotion

To add the Emotion library to your React application, run the following terminal command:

The Emotion library should now be installed in your project and ready to use to style your React application.

Someone typing casually on a laptop next to an open diary and an empty teacup.

Styling Using Emotion’s css Prop

Once you’ve installed the Emotion library, you’ll be able to use thecssprop to style your React application. Thecssprop is similar to the style prop as you’re able to pass styles to it in the form of strings or regular JavaScript objects.

To style your application using thecssprop, you must import it from the@emotion/reactlibrary, then define your styles:

An open laptop sitting on a windowsill with a residential view in the background.

The first line of code,/** @jsxImportSource @emotion/react */, is a particular comment you should add to the JSX file to indicate that the Emotion library should be used as the JSX pragma for that file.

In JSX, a pragma is a function that transforms the JSX syntax into regular JavaScript. By default, React uses theReact.createElementfunction as the JSX pragma. However, with the@jsxImportSourcecomment, you can specify a different pragma.

Meta AI being used on a laptop and cellphone

In this case, the@emotion/reactpragma tells JSX to use thejsxfunction from the Emotion library to transform the JSX code. By adding the pragma comment to a JSX file, you could use the Emotion library’s CSS-in-JS features in your components.

The button component renders a button with some custom styling. Here, thecssprop adds the custom styling to the button element.

BBC iPlayer showing on a smart TV.

Thecssprop also supports nested styling. Nested styling allows you to write styles that are nested within each other.

For example:

In this example, the declaration of the hover style uses the nested styling feature of thecssprop. The background and font color in the code block above will change whenever you hover over the button.

Passing Object Styles to the css Prop

Thecssprop also accepts regular JavaScript object styles. When styling several components, utilizing object styles allows you to reuse styles in your components.

To pass object styles to thecssprop, define the styles as a JavaScript object and pass it to the prop:

Note that the CSS property names are camelCased instead of hyphenated. This is because the styles are defined as JavaScript objects, which use camelCase naming conventions.

Styling Using the Styled Components

The Emotion library also makes use of styled components when styling React applications.Using styled componentsis similar to React components, asides from the fact that they contain styles out of the box. To create styled components, you will use thestyledfunction.

Thestyledfunction allows you to create reusable styled components. To use thestyledfunction, import it from theemotion/styledlibrary.

To get the@emotion/styledlibrary in your application, you will install it into your project. You can do this by running the following command in your project’s terminal:

After installing the@emotion/styledlibrary, import thestyledfunction and define your styles:

In the code block above, a styled componentButtonis created. You can use this Button in your React application like any other React component.

Rendering theAppcomponent will display a button with the styles defined in theButtoncomponent on your screen.

Thestyledfunction also accepts string styles. It looks different from the object styles approach but functions similarly.

Efficient Styling With Emotion

Emotion is a powerful library for styling React components that provides a simple and efficient way to manage your styles. Whether you are a beginner or an experienced developer, Emotion can help to simplify the process of styling your React application and make it easier to maintain and scale your code.

So if you’re looking for a more efficient and flexible way to style your React components, consider Emotion.

Get consistent, attractive alerts across your React site with this library.

The best features aren’t the ones being advertised.

Unlock a world of entertainment possibilities with this clever TV hack.

It’s not super flashy, but it can help to keep your computer up and running.

Obsidian finally feels complete.

Don’t let aging hardware force you into buying expensive upgrades.

Technology Explained

PC & Mobile