-- online Search!

Creating Complex Components

by kirupa   |   filed under Learning React

TL;DR

Components are composable, so you can combine small ones into more complex ones. We turn a color palette card into React by first breaking the visual into a parent/child visual hierarchy, then deciding which pieces deserve to be components. That gives us Card, Square, and Label, each doing just one thing.

  • Start by identifying every visual element, from the obvious ones down to the less obvious, and arrange them into a tree until nothing divides further. Try to ignore implementation details like HTML and CSS at this stage.
  • A good component does just one thing. If it does too much, break it up, and if it does too little (like the empty white region around the label), skip making it a component.
  • The component hierarchy helps you define your code, not how the finished product looks. For visual details, always go back to your comps, redlines, or screenshots.
  • Each component starts as a function that returns null, which is how a component says it has nothing to show yet, before we fill them in with inline styles.

In a previous article, we learned about components and all the awesome things that they do. We learned that components are the primary way for our visual elements to behave like little reusable bricks that contain all of the HTML, JavaScript, and styling needed to run themselves. Beyond reusability, there is another major advantage components bring to the table. They allow for composability. You can combine components to create more complex components.

In this tutorial, we will look at what all of this means. More specifically, we will look at two things:

  1. The boring stuff you need to know about how to identify components when you look at a bunch of visual elements.
  2. The boring technical stuff you need to know about how components use other components.

Ok, what you are going to learn isn't actually that boring. I am just setting your expectations really low :P

Onwards!

Learning React by Kirupa Chinnathambi

OMG! A React Book Written by Kirupa?!!

To kick your React skills up a few notches, everything you see here and more (with all its casual clarity!) is available in both paperback and digital editions.

BUY ON AMAZON

From Visuals to Components

The various examples we've looked at so far have been pretty basic. They were great for highlighting technical concepts, but they weren't great for preparing you for the real world:

In the real world, what you'll be asked to implement in React will never be so simple as a list of names or...colorful blocks of vowels. Instead, you'll be given a visual of some complex user interface. That visual can take many forms - such as a scribble, diagram, screenshot, video, redline, comp, etc. It is up to you to bring all of those static pixels to life, and we are going to get some hands-on practice in doing just that.

What we are going to do is build a simple color palette card:

A color palette card with a large square of coral color at the top and its hex value, #FF6663, printed in a white area below it

If you are not sure what these are, these are small rectangular cards that help you match a color with a particular type of paint. You'll frequently see them in home improvement stores or anywhere paint is sold. Your designer friend probably has a giant closet dedicated to them in his/her place. Anyway, our mission is to re-create one of these cards using React.

There are several ways to go about this, but I am going to show you a very systematic approach that will help you simplify and make sense of even the most complex user interfaces. This approach involves two steps:

  1. Identify the major visual elements
  2. Figure out what the components will be

Both of these steps sound really complex, but as we walk through this, you'll see that it is nothing to be worried about.

Identifying the Major Visual Elements

The first step is to identify all of the visual elements we are dealing with. No visual element is too minor to omit - at least, not initially. The easiest way to start identifying the relevant pieces is to start with the obvious visual elements and then dive into the less obvious ones.

The first thing you will see in our example is the card itself:

Within the card, you'll see that there are two distinct regions. The top region is a square area that displays a particular color. The bottom region is a white area that displays a hex value.

Let's call out these two visual elements and arrange them into a tree-like structure as shown below:

Arranging your visuals into this tree-like structure (aka a visual hierarchy) is a good way to get a better feel for how your visual elements are grouped. The goal of this exercise is to identify the important visual elements and break them into a parent/child arrangement until you can divide them no further.

Note: Try to Ignore Implementation Details

While it is hard, do not think of implementation details yet. Don't focus on dividing your visual elements based on what combination of HTML and CSS would be required. There is plenty of time for that later!

Continuing on, we can see that our colorful square isn't something that we can divide further. That doesn't mean we are done, though. We can further divide the label from the white region that surrounds it. Right now, our visual hierarchy looks as follows with our label and white region occupying a separate spot in our tree:

At this point, we have nothing else to divide any further. We are done with identifying and dividing up our visual elements, so the next step is to use what we've found here to help us identify the components.

Identifying the Components

This is where things get a little interesting. We need to figure out which of the visual elements we've identified will be turned into a component and which ones will not. Not every visual element will need to be turned into a component, and we certainly don't want to create only a few extremely complex components either. There needs to be a balance:

There is an art to figuring out what visual elements become part of a component and which ones don't. The general rule is that our components should do just one thing. If you find that your potential component will end up doing too many things, you probably want to break your component into multiple components. On the flipside, if your potential component does too little, you probably want to skip making that visual element a component altogether.

Let's try to figure out which elements would make for good components in our example. From looking at our visual hierarchy, right off the bat, both the card and the colored square seem like they fit the bill for making a great component. The card acts as the outer container, and the colored square simply displays a color.

That just puts a question mark around our label and the white region it is surrounded by:

The important part here is the label itself. Without it, we can't see the hex value. That leaves just the white region. The purpose it serves is negligible. It is simply empty space, and the responsibility for that can easily be handed off to our label itself. Brace yourself for what I am about to say next. Sadly, our white rectangular region will not be turned into a component.

At this point, we have identified our three components, and the component hierarchy looks as follows:

An important thing to note is that the component hierarchy has more to do with helping us define our code than it does with how the finished product will look. You'll notice that it looks a bit different than the visual hierarchy we started off with. For visual details, you should always refer to your source material (aka your visual comps, redlines, screenshots, and other related items). For figuring out which components to create, you should use the component hierarchy.

Ok, now that we've identified our components and the relationship between all of them, it is time to start bringing our color palette card to life.

Creating the Components

This is the easy part...sort of! It is time for us to start writing some code. Open up a React project (if you don't have one, follow along with Easily Set Up Your React Dev Environment to create one with Vite). Because we are going to be styling our components using inline styles, the starter CSS that the template gives us isn't needed. Open the src/index.css file, and delete everything inside it.

Next, open src/App.jsx and replace everything inside it with the following. This will be the starting point for our example:

function App() {
  return (
    <div>

    </div>
  );
}

export default App;

There isn't much going on here. We have an App component that returns an empty div, and the main.jsx file that came with our project takes care of displaying it on the page.

After you've done this, it is time to define our three components. The names we will go with for our components will be Card, Label, and Square. Go ahead and add the following lines above the App component:

function Square() {
  return null;
}

function Label() {
  return null;
}

function Card() {
  return null;
}

Each of our components is a function that returns something. For now, we have them return null, which is how a component says "I have nothing to show right now." In the following sections, we will fix that by filling them in.

The Card Component

We are going to start at the top of our component hierarchy and focus on our Card component first. This component will act as the container that our Square and Label components will live in.

To implement it, go ahead and make the following highlighted modifications:

function Card() {
  const cardStyle = {
    height: 200,
    width: 150,
    padding: 0,
    backgroundColor: "#FFF",
    filter: "drop-shadow(0px 0px 5px #666)",
  };

  return <div style={cardStyle}></div>;
}

While this seems like a lot of changes, the bulk of the lines are going into styling the output of our Card component via the cardStyle object. The rest of the changes are pretty unimpressive. We return a div element, and that element's style attribute is set to our cardStyle object.

Vendor Prefixes in Style Objects

You may come across older code that uses a vendor-prefixed property, such as WebkitFilter. Notice the capitalization. Instead of the first letter being lowercase like webkitFilter, the W is capitalized. That isn't how other CSS properties are represented in React, so keep that in mind if you ever need to specify one. (Today, a property like filter works in all modern browsers without a prefix.)

Now, to see our Card component in action, we need to display it by using it inside our App component. To make that happen, go ahead and make the following highlighted change:

function App() {
  return (
    <div>
      <Card />
    </div>
  );
}

All we are doing is using our Card component just like it was an HTML element. If everything worked out properly, you'll see the following if you save your file and look at your browser:

The outline of a white card with a soft drop shadow around it

Yes, it is just the outline of our color palette card, but that is definitely more than what we started out with just a few moments ago!

The Square Component

It's time to go one level down in our component hierarchy and look at our Square component. This is a pretty straightforward one, so make the following highlighted changes:

function Square() {
  const squareStyle = {
    height: 150,
    backgroundColor: "#FF6663",
  };

  return <div style={squareStyle}></div>;
}

Just like with our Card component, we are returning a div element whose style attribute is set to a style object that defines how this component looks. To see our Square component in action, we need to get it onto our page just like we did with the Card component earlier. The difference this time around is that we won't be using the Square component from our App component. Instead, we'll use the Square component from inside the Card component. To see what I mean, go back to our Card component, and make the following change:

function Card() {
  const cardStyle = {
    height: 200,
    width: 150,
    padding: 0,
    backgroundColor: "#FFF",
    filter: "drop-shadow(0px 0px 5px #666)",
  };

  return (
    <div style={cardStyle}>
      <Square />
    </div>
  );
}

At this point, if you check your browser, you'll see a colorful square making an appearance:

The white card now has a coral colored square filling its top portion

The cool thing to call out is that we used our Square component from inside the Card component! This is an example of component composability where one component relies on the output of another component. The final thing you see is the result of these two components colluding with each other. Isn't collusion just beautiful...at least in this context?

The Label Component

The last component that remains is our Label. Go ahead and make the following highlighted changes:

function Label() {
  const labelStyle = {
    fontFamily: "sans-serif",
    fontWeight: "bold",
    padding: 13,
    margin: 0,
  };

  return <p style={labelStyle}>#FF6663</p>;
}

The pattern of what we are doing should be routine to you by now. We have a style object that we assign to the element we return. The element we return is a p element whose content is the string #FF6663. To have what we return ultimately make it to the page, we need to use our Label component inside our Card component. Go ahead and make the following highlighted change:

function Card() {
  const cardStyle = {
    height: 200,
    width: 150,
    padding: 0,
    backgroundColor: "#FFF",
    filter: "drop-shadow(0px 0px 5px #666)",
  };

  return (
    <div style={cardStyle}>
      <Square />
      <Label />
    </div>
  );
}

Notice that our Label component lives just under the Square component we added to our Card component earlier. If you check your browser now, you should see something that looks as follows:

The finished color palette card with a coral square on top and the text #FF6663 below it

Yes, that's right! Our color palette card is done and visible thanks to the efforts of our Card, Square, and Label components. That doesn't mean we are done yet, though. There are a few more things to cover.

Passing Props, Again!

In our current example, we hard-coded the color value that is used by our Square and Label components. That is an odd thing to do...which may or may not have been done deliberately for dramatic effect, but fixing it is straightforward. It just involves us specifying a prop name and reading it from the object our component receives. We've seen all this before. What is different is the number of times we will have to do this.

There is no way to specify a prop on a parent component and have all of its descendants automatically gain access to that prop. (Well, there is a way, and we'll look at it when we get to Sharing Data with Context. It's overkill for what we are doing here.) The normal way to pass a prop to a child component is to have each intermediate parent component pass it on as well. To see this in action, take a look at the highlighted changes to our current code where we move away from a hard-coded color and define our card's color using a color prop instead:

function Square({ color }) {
  const squareStyle = {
    height: 150,
    backgroundColor: color,
  };

  return <div style={squareStyle}></div>;
}

function Label({ color }) {
  const labelStyle = {
    fontFamily: "sans-serif",
    fontWeight: "bold",
    padding: 13,
    margin: 0,
  };

  return <p style={labelStyle}>{color}</p>;
}

function Card({ color }) {
  const cardStyle = {
    height: 200,
    width: 150,
    padding: 0,
    backgroundColor: "#FFF",
    filter: "drop-shadow(0px 0px 5px #666)",
  };

  return (
    <div style={cardStyle}>
      <Square color={color} />
      <Label color={color} />
    </div>
  );
}

function App() {
  return (
    <div>
      <Card color="#FF6663" />
    </div>
  );
}

export default App;

Each component receives its props as an object, and by writing { color } in the function's parameter list, we pull the color prop right out of that object. (This is JavaScript's destructuring at work, and we'll see more of it throughout this series.) Once you have made this change, you can specify any hex color you want as part of using the Card component:

function App() {
  return (
    <div>
      <Card color="#FFA737" />
    </div>
  );
}

The resulting color palette card will feature the color you specified:

The same color palette card, now showing an orange square and the text #FFA737

Now, let's go back to the changes we made. Even though the color prop is only consumed by the Square and Label components, the parent Card component is responsible for passing it on to them. For even more deeply nested situations, you'll have more intermediate components that will be responsible for transferring props. It gets worse. When you have multiple props that you would like to pass around multiple levels of components, the amount of typing (or copying and pasting) you do increases a lot as well. There are ways to mitigate this, and we'll look at those in the very next tutorial.

Because our Card takes its color from a prop, drawing multiple cards is as easy as using it multiple times. Say we do the following:

function App() {
  return (
    <div style={{ display: "flex", gap: 20 }}>
      <Card color="#FF6663" />
      <Card color="#FFA737" />
      <Card color="#1C89BF" />
    </div>
  );
}

We still have just one Card component, and we are using it three times with a different color each time. The result is a small collection of paint cards:

Three color palette cards side by side: coral, orange, and blue, each labeled with its hex value

That's what reusability looks like!

Giving Each Component Its Own File

Everything in our example lives inside App.jsx to make it easy to follow along. Real projects don't usually do that. A common convention is to put each component in its own file, and then export it so other files can import it. For example, our Square component would live in a file called Square.jsx:

function Square({ color }) {
  const squareStyle = {
    height: 150,
    backgroundColor: color,
  };

  return <div style={squareStyle}></div>;
}

export default Square;

The Card.jsx file would then import both Square and Label from their files, and our App.jsx file would import Card the same way:

import Square from "./Square.jsx";
import Label from "./Label.jsx";

function Card({ color }) {
  const cardStyle = {
    height: 200,
    width: 150,
    padding: 0,
    backgroundColor: "#FFF",
    filter: "drop-shadow(0px 0px 5px #666)",
  };

  return (
    <div style={cardStyle}>
      <Square color={color} />
      <Label color={color} />
    </div>
  );
}

export default Card;

Nothing about how the components work changes. We have just moved them around. If your files don't need to be this tiny, that is OK, too. Group things in a way that makes sense to you.

Why Component Composability Rocks

When we are heads-down in React, we often tend to forget that what we are ultimately creating is just plain and boring HTML, CSS, and JavaScript. The generated HTML for our color palette card looks as follows:

<div id="root">
  <div>
    <div style="height: 200px;
                width: 150px;
                padding: 0px;
                background-color: rgb(255, 255, 255);
                filter: drop-shadow(rgb(102, 102, 102) 0px 0px 5px);">
      <div style="height: 150px;
                  background-color: rgb(255, 102, 99);">
      </div>
      <p style="font-family: sans-serif;
                font-weight: bold;
                padding: 13px;
                margin: 0px;">
        #FF6663</p>
    </div>
  </div>
</div>

This markup has no idea of how it got there. It doesn't know about which components were responsible for what. It doesn't care about component composability or the frustrating way we had to transfer the color prop from parent to child. That brings up an important point to make.

If we had to generalize the end result of what components do, all they do is return blobs of HTML to whatever used them. Each component returns some HTML to another component. All of this HTML keeps accumulating until a giant blob of HTML is pushed (very efficiently) to our page. That simplicity is why component reuse and composability works so well. Each blob of HTML works independently from other blobs of HTML - especially if the styling travels along with the component like it does here. This allows you to easily create visual elements from other visual elements without having to worry about anything. ANYTHING! Isn't that pretty freaking awesome?

Conclusion

As you may have realized by now, we are slowly shifting focus towards the more advanced scenarios that React thrives in. Actually, advanced isn't the right word. The correct word is realistic. In this tutorial, we started by learning how to look at a piece of UI and identify the components in a way that you can later implement. That is a situation you will find yourself in all the time. While the approach we employed seemed really formal, as you get more experienced with creating things in React, you can ratchet down the formality. If you can quickly identify the components and their parent/child relationships without creating a visual and component hierarchy, then that is one more sign that you are getting really good at working with React!

Identifying the components is only one part of the equation. The other part is bringing those components to life. Most of the technical stuff we saw here was just a minor extension of what we've already seen earlier. We looked at one level of components in an earlier tutorial, and here we looked at how to work with multiple levels of components. We looked at how to pass props between one parent and one child before, and here we looked at how to pass props through multiple parents and children. We even drew multiple color palette cards to the screen, which was the groundbreaking thing I promised I'd get to eventually!

Passing a prop through layer after layer of components is the one part that wasn't a whole lot of fun. We'll fix that in the next tutorial.

Next tutorial: Transferring Properties

Before you go, here are a few things you can do:

  1. Ask a question

    Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright © KIRUPA 2026 //--