Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to extend a component in ReactJS

Understanding Components in ReactJS

A ReactJS application is like a big Lego castle. Each Lego block constitutes a component, and when combined together, they form a complete application. Similarly, in ReactJS, each part of the application user interface is a component.

What Does It Mean to Extend a Component?

Imagine you have a basic Lego piece. You want to make it more interesting, so you add some extra features like wheels or wings. This is similar to extending a component in ReactJS. You take a base component and add more functionality or properties to it. The process of extending a component allows you to create a new component that inherits properties from the base component.

Base Component and Child Component

In the ReactJS world, the original component is often referred to as the 'base component', while the extended component is termed the 'child component'.

For instance, consider a simple base component named BaseComponent.

class BaseComponent extends React.Component {
  render() {
    return <h1>Hello, I am a base component.</h1>;
  }
}

How to Extend a Component?

To extend a component, we create a new component that extends from the base component. Let's call this new component ChildComponent.

class ChildComponent extends BaseComponent {
  render() {
    return <h2>And I am an extended component!</h2>;
  }
}

In the example above, ChildComponent is extending BaseComponent. But if we render ChildComponent, it will only display its own message, and not the message from BaseComponent. This is because the render() function in ChildComponent has overridden the render() function in BaseComponent.

Reusing Base Component's Render Function

To display messages from both components, we need to modify the ChildComponent to call the BaseComponent's render() function. We can do this with the super keyword.

class ChildComponent extends BaseComponent {
  render() {
    return (
      <div>
        {super.render()}
        <h2>And I am an extended component!</h2>
      </div>
    );
  }
}

In this updated ChildComponent, the super.render() calls the render() function of BaseComponent. As a result, when ChildComponent is rendered, it displays both messages.

Why Extend a Component?

The biggest advantage of extending components is code reusability. It promotes the DRY (Don't Repeat Yourself) principle. If several components share common features, these shared features can be placed in a base component. Then, other components can extend this base component to reuse the common features.

A Word of Caution

While extending components can be useful, it should be used judiciously. Overuse can lead to complex inheritance hierarchies that are difficult to understand and maintain. As a rule of thumb, prefer composition over inheritance. That means, instead of making complex components by extending other components, try to build complex components by composing simpler ones.

Conclusion

Imagine yourself as a master Lego builder. Each block you have is a tool at your disposal. You can use a basic block, or you can customize a block to be more complex by adding features to it. Similarly, in ReactJS, you have the power to create simple components or extend them to create complex ones. However, remember to use this power wisely. Make your castle strong and beautiful, but also easy to understand and maintain. Happy building!