fervor [>]CODING & CURIOSITY
FERVOR LEARNING SYSTEMTUTORIALS
← Design principles

Design principles / 3 MIN READ

Component Choice

Should it be a component?

From the original Fervor library. Examples may use older package versions.

To make a component or not to make a component.

How to Decide Whether to Create a New Component or Include Content Within an Existing Component in React

Step 1: Assess the complexity of the content

First, determine whether the content you’re adding to your application is simple or complex. Simple content might include static text, images, or basic styling. Complex content might involve user interactions, state management, or dynamic updates.

  • If the content is simple, you can often include it within the existing component.
  • If the content is complex, it’s better to create a separate component for it.

Step 2: Evaluate reusability

Consider whether the content you’re adding will be reused in other parts of your application.

  • If the content is unique and not likely to be reused, you can include it within the existing component.
  • If the content will be reused in multiple places, create a separate component to follow the DRY (Don’t Repeat Yourself) principle and make your codebase more modular.

Step 3: Consider maintainability

Think about how easy it will be to maintain and update the content in the future.

  • If the content is simple and unlikely to change frequently, you can include it within the existing component.
  • If the content is complex or requires frequent updates, creating a separate component can make it easier to maintain and test.

Step 4: Make a decision

Based on the factors above, decide whether to create a new component or include the content within an existing component.

  • If the content is simple, unique, and unlikely to change frequently, include it within the existing component.
  • If the content is complex, reusable, or requires frequent updates, create a separate component.

Example Scenario

Imagine you’re building a Header component for a website, and you need to decide whether to create separate components for the title, menu, and logo.

  1. Assess the complexity of the content: The title, menu, and logo are relatively simple, containing only text or images.
  2. Evaluate reusability: The title and logo are unique to the Header and unlikely to be reused elsewhere. The menu might be reused in other parts of the application.
  3. Consider maintainability: The title and logo are unlikely to change frequently, while the menu might require updates if new pages are added to the site.
  4. Make a decision: Based on the factors above, you can include the title and logo within the Header component. However, you might consider creating a separate Menu component since it could be reused and might require updates.

Remember that these guidelines are not set in stone, and you should always consider the specific requirements of your application when making decisions about your component structure.

Now go make that component or… don’t

Keep your curiosity going.Explore more Design principles →
287 TUTORIALS · 22 TOPICSREADY