Yogesh Chauhan's Blog

What does Containment mean in React?

in React on May 27, 2021

In all major programming languages there are few really great concepts like inheritance, abstraction, polymorphism, etc.

React has another concept called “composition”.

Now, the basic meaning of composition is nothing but a mixture of few ingredients which is actually works great since in React, the whole app is divided into small components and when they come all together, they make the app complete. But that’s just a basic understanding.

React has actually a powerful composition model. React recommends using composition rather than inheritance to reuse the code between different components.

So, what is Containment?

Some generic “boxes” components like Sidebar or a Dialog box in React are not aware of their children ahead of time. In those cases, React recommends that those components use their special children props to pass the children elements into their output.

Just like this:


function QuoteContainer(props) {
  return (
    <div className={'Quote Quote-' + props.color}>
      {props.children}
    </div>
  );
}


So that the other components can nest the JSX by passing the arbitrary children.

Just like this:


function Quote() {
  return (
    <QuoteContainer color="red">
      <h1>
        Some Quote
      </h1>
      <p>
        -by Yogesh Chauhan
      </p>
    </QuoteContainer>
  );
}


Here’s the working demo:

Pay attention to the QuoteContainer inside the Quote() component. Whatever you add inside QuoteContainer, it will be passed to the Quote(). The Quote() receives the props and renders them inside a div with classes that are defined in the stylesheet. At the end, the whole picture comes together and makes the final output.

Specialization in React

We want to use specialization in cases where we want to make some components act “specially”.

That’s where the composition comes into play.

Composition in React happens when a special component renders a generic component and then configures it with the passed props.

Let’s change the example above to understand this.

Specialization Demo


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to undo Git Commits on Pantheon?MiscellaneousControl rendering using CSS content-visibility propertyCSSHow to convert a function component into a class in React?ReactHow to install Gulp with WordPress?WordPressHow to disable right click and drag and drop of images using jQuery?jQueryHow to Install PHP Laravel on MacOS Catalina?PHP