Are You Drowning In A Pile Of Styled React Components?

But beware! You may step into the too-many-imports-trap or fall into the maelstrom of abstraction, too.

Frank Zickert | Quantum Machine Learning
codeburst
Published in
6 min readNov 22, 2019

--

Photo by Jan Mellström on Unsplash

With React, you can write your web-app in a functional way. Rather than changing a global state until you reached the desired outcome, with React, your app consists of a hierarchy of stateless components.

A component is nothing but a function. A function whose returned result only depends on its internal logic and the properties you pass into it. It has no side-effects. You don’t have to care about some outside state that is cumbersome to keep track of.

This applies to the style of a component, too. I am sure, you’ve heard of styled-components. Most probably, you use this library to style your components. And you should. Because it lets you style your component the React way. The functional way. Rather than putting all styles in global, hard-to-manage CSS, you can style your components locally.

But if you’re not careful, you easily drown in a pile of styled React components. Let’s have a look!

You attach a CSS literal to a basic HTML-element with the default helper function styled. This is a low-level factory that provides helper methods in the form of styled.tagname. The tagname is any valid HTML-tag.

In this example, we add a horizontal margin to a div which equally distributes to the left and the right.

A single one component for styling ain’t too bad…

“What if you want to share the same style across two components?” you ask?Not much of a problem.

If one component extends the other, you can inherit the style. If you like, you can override it (the width for instance).

--

--

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Written by Frank Zickert | Quantum Machine Learning

You're interested in quantum computing and machine learning. But you don't know how to get started? Let me help

Responses (2)

Write a response