Register to get access to free programming courses with interactive exercises

Nested Components JS: React

In real React applications, there are many more components. Some of them are independent, some are used only as part of others.

One of the ways to assemble components is children which you already know. It makes no difference if the children are built-in React components or if they're separately written components.

class Alert extends React.Component {
  render() {
    const { children } = this.props;
    return (
      <div className="alert alert-primary">
        {children}
      </div>
    );
  }
}

const vdom = (
  <Alert>
    <p>Paragraph 1</p>
    <hr />
    <p className="mb-0">Paragraph 2</p>
  </Alert>
);

const root = createRoot(document.getElementById('react-root'));
root.render(vdom);

In some situations, only certain components specially created for a component need to be passed into it. For example, the Card component up to now was implemented in such a way that it only accepted props as input. In reality, this solution is less than fab. There's no customization at all, you can pass only what was originally intended, and only in string format. Any complicated content is out of the question. The correct approach would look like this:

<Card>
  <CardImgTop src="path/to/image" />
  <CardBody>
    <CardTitle>Body</CardTitle>
  </CardBody>
</Card>

In situations where composition isn't required, you can simply take and use any third-party components within your own.

See the Pen js_react_nested_components by Hexlet (@hexlet) on CodePen.

You can nest any components you want as many times as you want. But here lies one danger. The desire to build a “perfect architecture” pushes developers to plan in advance how to break down the application into components and implement them immediately. It's important to understand that nesting itself is complicates understand, because it means you'll have to keep jumping from one component to another. In addition, a rigid structure will tie you down, refactoring isn't easy, and the desire to do it will be greatly diminished because of how bound you might feel to your solution. Be pragmatic. The optimal way to add new components is to wait for when working with the current component to become too complex to work with because of the volume and number of variables you have to deal with at one time. And even in this case, it's often sufficient to allocate additional rendering functions within the component itself, for example renderItem.

State

One of the most common questions from those who are just beginning to get acquainted with React is how to distribute the state to components. The short answer is that you can't. In almost all situations, separating the state will make the code and working with it more complicated. The right approach is to create a root component that contains the entire state within itself, and all underlying components receive their data as props. The state itself should be as flat as possible, like a relational database. Then you can safely apply normalization and perform updates without issue.

Sometimes, situations can arise where the properties needed deep down have to be dragged through many intermediate components that don't use these props themselves. This is another reason to try not to get carried away with deep nesting. However, in the next course you'll learn about Redux, which largely solves this problem (and many others).

Callbacks

There's also another difficulty that may arise from what we've mentioned above: what if an event occurs in an underlying component that has no state of its own? Without Redux, there's basically only one way out. The root component has to pass callbacks into internal components, and those components in turn pass them on as needed.

See the Pen js_react_nested_components_callbacks by Hexlet (@hexlet) on CodePen.


Hexlet Experts

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs

From a novice to a developer. Get a job or your money back!

Frontend Developer icon
Profession
beginner
Development of front-end components for web applications
start anytime 10 months

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.