Compound Elements In React

No Comments

Compound elements assist builders construct extra expressive and versatile APIs to share state and logic inside elements. This tutorial explains how this may be achieved with the assistance of utilizing the Context API and React to construct elements by utilizing this superior sample.

Be aware: So as to have the ability to comply with alongside, you’ll want a fundamental understanding of React and the way the Context API works.

What Is A Compound Element?

Compound elements might be stated to be a sample that encloses the state and the conduct of a gaggle of elements however nonetheless provides the rendering management of its variable elements again to the exterior consumer.

From the definition above, being attentive to the key phrases: state and conduct. This helps us perceive that compound element take care of state (i.e. how state behaves throughout a element which is enclosed by an exterior consumer being the father or mother of the element).

The target of compound elements is to supply a extra expressive and versatile API for communication between the father or mother and the kid elements.

Consider it just like the <choose> and <choice> tags in HTML:

<choice worth=”volvo”>Volvo</choice>
<choice worth=”mercedes”>Mercedes</choice>
<choice worth=”audi”>Audi</choice>

The choose tag works along with the choice tag which is used for a drop-down menu to pick gadgets in HTML. Right here the <choose> manages the state of the UI, then the <choice> parts are configured on how the <choose> ought to work. Compound elements in React are used to construct a declarative UI element which helps to keep away from prop drilling.

Prop drilling is passing props down a number of baby elements. That is additionally what they name a “code scent”. The worst a part of prop drilling being that when the father or mother element re-renders, the kid elements can even re-render and trigger a domino impact on the element. A very good resolution could be to make use of the React Context API which we can even look into later.

Making use of Compound Elements In React

This part explains the packages we will make use of in our software which undertake the compound element sample of constructing elements in React. This instance is a Menu element from the @attain UI bundle.

import {
} from “@attain/menu-button”;
import “@attain/menu-button/types.css”;

Right here’s a approach you should utilize the Menu element:

operate Instance() {
return (
<MenuLink to=”view”>View</MenuLink>

The instance code above is likely one of the implementations of compound elements wherein you get to see that the Menu, MenuButton,MenuList, MenuItem and MenuLink have been all imported from @attain/menu-button. Versus exporting a single element, ReachUI exports a father or mother element which is Menu accompanying its youngsters elements that are the MenuButton, MenuList, MenuItem and the MenuLink.

When Ought to You Make Use Of Compound Elements?

As a React developer, it is best to make use of compound elements whenever you wish to:

Resolve points associated to constructing reusable elements;
Improvement of extremely cohesive elements with minimal coupling;
Higher methods to share logic between elements.

Professionals And Cons Of Compound Elements

A compound element is an superior React sample so as to add to your React developer toolkit. On this part, I’ll state the professionals and cons of utilizing compound elements and what I’ve discovered from constructing elements utilizing this sample of growth.


Separation Of Concern
Having all of the UI state logic within the father or mother element and speaking that internally to all of the baby elements makes for a transparent division of accountability.

Lowered Complexity
Versus prop drilling to cross down properties to their particular elements, baby props go to their respective baby elements utilizing the compound element sample.


One of many main cons of constructing elements in React with the compound element sample is that solely direct youngsters of the father or mother element may have entry to the props, that means we will’t wrap any of those elements in one other element.

export default operate FlyoutMenu() {
return (
{/* This breaks */}
<FlyOut.Toggle />

An answer to this problem could be to make use of the versatile compound element sample to implicitly share state utilizing the React.createContext API.

Context API makes it potential to cross React state by way of nested elements when constructing utilizing the compound element sample of constructing elements in React. That is potential as a result of context offers a technique to cross information down the element tree with out having to cross props down manually at each degree. Making use of Context API offers a great deal of flexibility to the end-user.

Sustaining Compound Elements In React

Compound elements present a extra versatile technique to share state inside React purposes, so making use of compound elements in your React purposes makes it simpler to keep up and truly debug your apps.

Constructing A Demo

On this article, we’re going to construct an accordion element in React utilizing the compound elements sample. The element we’re going to be constructing on this tutorial could be a custom-made accordion element that’s versatile and shares state throughout the element by utilizing the Context API.

Let’s go!

To start with, let’s create a React app by utilizing the next:

npx create-react-app accordionComponent
cd accordionComponent
npm begin


yarn create react-app accordionComponent
cd accordionComponent
yarn begin

The instructions above create a React app, change the listing to the React undertaking, and begin up the event server.

Be aware: On this tutorial, we shall be making use of styled-components to assist type our elements.

Use the command beneath to put in styled-components:

yarn add styled-components


npm set up –save styled-components

Within the src folder, create a brand new folder referred to as elements. That is the place all our elements would reside. Inside the elements folder, create two new recordsdata: accordion.js and accordion.types.js.

The accordion.types.js file comprises our styling for the Accordion element (our styling was finished utilizing styled-components).

import styled from “styled-components”;

export const Container = styled.div`
show: flex;
border-bottom: 8px strong #222;

Above is an instance of styling elements utilizing the css-in-js library referred to as styled-components.

Inside the accordion.types.js file, add the remaining types:

export const Body = styled.div`
margin-bottom: 40px;
export const Internal = styled.div`
show: flex;
padding: 70px 45px;
flex-direction: column;
max-width: 815px;
margin: auto;
export const Title = styled.h1`
font-size: 40px;
line-height: 1.1;
margin-top: 0;
margin-bottom: 8px;
shade: black;
text-align: heart;
export const Merchandise = styled.div`
shade: white;
margin: auto;
margin-bottom: 10px;
max-width: 728px;
width: 100%;
&:first-of-type {
margin-top: 3em;
&:last-of-type {
margin-bottom: 0;
export const Header = styled.div`
show: flex;
flex-direction: space-between;
cursor: pointer;
margin-bottom: 1px;
font-size: 26px;
font-weight: regular;
background: #303030;
padding: 0.8em 1.2em 0.8em 1.2em;
user-select: none;
align-items: heart;
img {
filter: brightness(0) invert(1);
width: 24px;
user-select: none;
@media (max-width: 600px) {
width: 16px;
export const Physique = styled.div`
font-size: 26px;
font-weight: regular;
line-height: regular;
background: #303030;
white-space: pre-wrap;
user-select: none;
overflow: hidden;
&.closed {
max-height: 0;
overflow: hidden;
transition: max-height 0.25ms cubic-bezier(0.5, 0, 0.1, 1);
&.open {
max-height: 0px;
transition: max-height 0.25ms cubic-bezier(0.5, 0, 0.1, 1);
span {
show: block;
padding: 0.8em 2.2em 0.8em 1.2em;

Let’s begin constructing our accordion element. Within the accordion.js file, let’s add the next code:

import React, { useState, useContext, createContext } from “react”;
import {
} from “./accordion.types”;

Above, we’re importing the useState, useContext and the createContext hooks which can assist us to construct our accordion element utilizing compound elements.

The React documentation explains that context helps present a technique to cross information by way of the element tree with out having to cross props down manually at each degree.

what we now have imported earlier in our accordion.js file, you’ll discover that we additionally imported our types as elements which can assist us construct our elements sooner.

We are going to go forward and create our context for the element which can share information with the elements that want them:

const ToggleContext = createContext();
export default operate Accordion({ youngsters, …restProps }) {
return (
<Container {…restProps}>

The Container and the Internal elements from the above code snippet are from our ./accordion.types.js file wherein we created types for our elements utilizing the styled-components (from the css-in-js library). The Container element homes the entire Accordion we’re constructing by utilizing compound elements.

Right here we’re making a context object utilizing the createContext() technique, so when React renders a element that subscribes to this Context object, it can learn the present context worth from the closest matching Supplier above it within the tree.

Then we’re additionally creating our base element which is the Accordion; it takes the kids and any restProps. That is our father or mother element which homes the kids elements of the Accordion.

Let’s create different youngsters elements throughout the accordion.js file:

Accordion.Title = operate AccordionTitle({ youngsters, …restProps }) {
return <Title {…restProps}>{youngsters}</Title>;
Accordion.Body = operate AccordionFrame({ youngsters, …restProps }) {
return <Body {…restProps}>{youngsters}</Body>;

Discover the . after the father or mother Accordion element; that is used to attach the kid element to its father or mother element.

Let’s proceed. Now add the next to the accordion.js file:

Accordion.Merchandise = operate AccordionItem({ youngsters, …restProps }) {
const [toggleShow, setToggleShow] = useState(true);
return (
<ToggleContext.Supplier worth={{ toggleShow, setToggleShow }}>
<Merchandise {…restProps}>{youngsters}</Merchandise>
Accordion.ItemHeader = operate AccordionHeader({ youngsters, …restProps }) {
const { isShown, toggleIsShown } = useContext(ToggleContext);
return (
<Header onClick={() => toggleIsShown(!isShown)} {…restProps}>
Accordion.Physique = operate AccordionHeader({ youngsters, …restProps }) {
const { isShown } = useContext(ToggleContext);
return (
<Physique className={isShown ? “open” : “shut”}>

So right here we’re making a Physique, Header and Merchandise element that are all youngsters of the father or mother element Accordion. That is the place it’d begin to get difficult. Additionally, discover that every baby element created right here additionally receives a youngsters prop and restprops.

From the Merchandise baby element, we initialized our state utilizing the useState hook and set it true. Then additionally keep in mind that we created a ToggleContext on the high degree of accordion.js file which is a Context Object, and when React renders a element that subscribes to this Context object, it can learn the present context worth from the closest matching Supplier above it within the tree.

Each Context object comes with a Supplier React element that enables consuming elements to subscribe to context adjustments.

The supplier element accepts a worth prop to be handed to consuming elements which can be descendants of this supplier, and right here we’re passing the present state worth which is the toggleShow and technique to set the worth of the present state setToggleShow. They’re the worth that determines how our context object will share state round our element with out prop drilling.

Then in our header baby element of the Accordion, we’re destructing the values of the context object, then altering the present state of the toggleShow on click on. So what we are attempting to do is to cover or present our accordion when the Header is clicked on.

In our Accordion.Physique element, we’re additionally destructing the toggleShow which is the present state of the element, then relying on the worth of toggleShow, we will both conceal the physique or present the contents of the Accordion.Physique element.

In order that’s all for our accordion.js file.

Now that is the place we get to see how every little thing we now have discovered about Context and Compound elements come collectively. However earlier than that, let’s create a brand new file referred to as information.json and paste the content material beneath into it:

“id”: 1,
“header”: “What is Netflix?”,
“body”: “Netflix is a streaming service that offers a wide variety of award-winning TV programs, films, anime, documentaries and more – on thousands of internet-connected devices.nnYou can watch as much as you want, whenever you want, without a single advert – all for one low monthly price. There’s always something new to discover, and new TV programs and films are added every week!”
“id”: 2,
“header”: “How much does Netflix cost?”,
“body”: “Watch Netflix on your smartphone, tablet, smart TV, laptop or streaming device, all for one low fixed monthly fee. Plans start from £5.99 a month. No extra costs or contracts.”
“id”: 3,
“header”: “Where can I watch?”,
“body”: “Watch anywhere, anytime, on an unlimited number of devices. Sign in with your Netflix account to watch instantly on the web at from your personal computer or on any internet-connected device that offers the Netflix app, including smart TVs, smartphones, tablets, streaming media players and game consoles.nnYou can also download your favorite programs with the iOS, Android, or Windows 10 app. Use downloads to watch while you’re on the go and without an internet connection. Take Netflix with you anywhere.”
“id”: 4,
“header”: “How do I cancel?”,
“body”: “Netflix is flexible. There are no annoying contracts and no commitments. You can easily cancel your account online with two clicks. There are no cancellation fees – start or stop your account at any time.”
“id”: 5,
“header”: “What can I watch on Netflix?”,
“body”: “Netflix has an extensive library of feature films, documentaries, TV programs, anime, award-winning Netflix originals, and more. Watch as much as you want, any time you want.”

That is the information we shall be working with to be able to take a look at our accordion element.

So let’s preserve going. We’re virtually by way of and I imagine you could have discovered lots from following this text.

On this part, we’re going to carry collectively every little thing we now have been engaged on and studying about compound elements to have the ability to use it in our App.js file to make use of the operate to show the information we have already got on the internet web page. Additionally discover that there was no use of state throughout the App.js; all we did was cross down information to the particular elements and Context API took care of each different factor.

Now on to the ultimate half. In your App.js, do the next:

import React from “react”;
import Accordion from “./elements/Accordion”;
import faqData from “./information”;
export default operate App() {
return (
<Accordion.Title>Incessantly Requested Questions</Accordion.Title>
{ => (
<Accordion.Merchandise key={}>

In your App.js file, we imported our Compound Element Accordion from the file path, then additionally imported our dummy information, mapped by way of the dummy information to be able to get the person gadgets in our information file, then displayed them in accordance with the respective element, additionally you’ll discover that every one we needed to do was to cross the kids to the respective element, the Context API takes care of making certain that it reaches the proper element and there was no prop drilling.

That is what our remaining product ought to seem like:

Different To Compound Elements

A substitute for utilizing compound elements could be to utilize the Render Props API. The time period Render Prop in React refers to a method for sharing code between React elements utilizing a prop whose worth is a operate. A element with a render prop takes a operate that returns a React ingredient and calls it as a substitute of implementing its personal render logic.

To cross information from a element right down to a baby element that wants the information might outcome to prop drilling when you could have elements nested inside one another. That is the benefit of utilizing Context to share information between elements over utilizing the render prop technique.


On this article, we discovered about one of many superior patterns of React which is the compound element sample. It’s an superior technique to construct reusable elements in React by utilizing the compound element sample to construct your element provides you plenty of flexibility in your element. You’ll be able to nonetheless decide to utilize Render Prop if flexibility shouldn’t be what your element requires in the intervening time.

Compound elements are most useful in constructing design techniques. We additionally went by way of the method of sharing the state throughout the elements utilizing the Context API.

The code for this tutorial might be discovered on Codesandbox.

    About Marketing Solution Australia

    We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

    Request a free quote

    We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

    Subscribe to our newsletter!

    More from our blog

    See all posts

    Leave a Comment