DEV Community

Cover image for compose in styled-system
Chetan
Chetan

Posted on

5

compose in styled-system

As per the document

The compose utility is used to combine multiple style functions together into one. This utility can help improve performance when using multiple style props functions on the same component.

In this post let us build a Button component using the styled-components and styled-system as shown below. Let us build this component using border, color, shadow, space & typography functions from the styled-system.

import styled from 'styled-components';
import { border, color, shadow, space, typography } from 'styled-system';

const Buttons = styled.div(
border,
color,
shadow,
space,
typography
);

export default Buttons;

So when we use multiple functions like border, color, shadow, space & typography, this will invoke multiple functions on render which will hamper the performance of app. It is even worse when the whole component is wrapped with state.

Here comes the compose function to the rescue which is the super hero 💠 of styled-system API. The compose function will combine the multiple functions into one, which will invoke only one function on render [saved by the compose function 😊]

import styled from 'styled-components';
import { compose, border, color, shadow, space, typography } from 'styled-system';

const Buttons = styled.div(
compose(
border,
color,
shadow,
space,
typography
)
);

export default Buttons;

So using the above component, you can define any kind of a <Button />

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay