The original from: https://dmitripavlutin.com/7-architectural-attributes-of-a-reliable-react-component/

Combination of i.

A composite component is a combination of smaller, specific components

Composition is a way to create larger components by joining components together. Composition is at the heart of React.

Fortunately, the composition is easy to understand. Take a group of small pieces, join them together, and create a bigger one.

Let’s look at a more general pattern for front-end application composition. The application consists of header, bottom footer, sidebar on the left, and valid content in the middle:

<div id="root"></div>
Copy the code
function Application({ children }) {
  return (
    <div className="application box">
      <Label>Application</Label>
      {children}
    </div>
  );
}

function Header() {
  return (
    <div className="header box">
      <Label>Header</Label>
    </div>
  )
}

function Footer() {
  return (
    <div className="header box">
      <Label>Footer</Label>
    </div>
  )
}

function Sidebar({ children }) {
  return (
    <div className="sidebar box">
      <Label>Sidebar</Label>
      {children}
    </div>
  );
}

function Content({ children }) {
  return (
    <div className="content box">
      <Label>Content</Label>
      {children}
    </div>
  )
}

function Menu() {
  return (
    <div className="menu box">
      <Label>Menu</Label>
      <div className="description">
        <div className="text shorter" />
        <div className="text" />
        <div className="text shorter" />
        <div className="text shorter" />
      </div>
    </div>
  );
}

function Article() {
  return (
    <div className="article box">
      <Label>Article</Label>
      <div className="description">
          <div className="text shorter" /> <div className="text longer" />
          <div className="text shorter" /> <div className="text" />
          <div className="text shorter" /> <div className="text" />
          <div className="text" /> <div className="text shorter" />
          <div className="text shorter" /> <div className="text" />
          <div className="text" /> <div className="text shorter" />
          <div className="text shorter" /> <div className="text longer" />
          <div className="text shorter" /> <div className="longer" />
          <div className="text shorter" /> <div className="text" />
          <div className="text" /> <div className="text shorter" />
          <div className="text shorter" /> <div className="text" />
      </div>
    </div>
  );
}

function Label({ children }) {
  return <div className="label">&lt; {children}&gt; </div> } const app = ( <Application> <Header /> <Sidebar> <Menu /> </Sidebar> <Content> <Article /> </Content> <Footer /> </Application> ); ReactDOM.render(app, document.getElementById('root')); 
Copy the code

The skeleton section shows how good combinations build applications. Organizing code in this way is expressive and easy to understand.

The React component composition is natural. The library uses a descriptive paradigm that does not inhibit the expressiveness of composition.


is a combination of

,

,

and

.

has only one child

, and

also has

.





So what does composition have to do with single responsibility and encapsulation? Let’s take a look:

The single Responsibility principle (SRP) describes how to divide components according to requirements, encapsulation describes how to organize these components, and composition describes how to glue the whole system together.

Single responsibility

An important aspect of composition is the ability to form complex components from specific components. This divide and conquer approach helps complex components that are composed to comply with SRP principles.

Reviewing the previous code snippet,

is responsible for rendering the header, footer, sidebar, and body area. Understand that this responsibility is split into four sub-responsibilities, each implemented by a corresponding sub-component.

In other words, the benefit of composition is that components like

conform to the single responsibility principle by allowing the sub-components to implement a single responsibility separately.

reusability

Using composite components also has the advantage of reusability, allowing common logic to be reused.

For example,

and

share common logic:

Const instance1 = (<Composed1> /* Composed1 component specific code... */ * Common code... */ </Composed1> ); Const instance2 = (<Composed2> /* generic code... */ /* Composed2 component specific code... */ </Composed2> );Copy the code

If CTRL +C/ CTRL +V is popular, how do you make components reuse common logic?

First, encapsulate the generic code in the new component

. Second,

and

should use combinations to include

; The code implementation is as follows:



const instance1 = (  
  <Composed1>
    <Piece1 />
    <Common />
  </Composed1>
);
const instance2 = (  
  <Composed2>
    <Common />
    <Piece2 />
  </Composed2>
);
Copy the code

Reusable components conform to the Don’t Repeat Yourself (DRY) principle. This beneficial practice saves time and energy.

flexibility

In React, a composite component can control its children, usually through the children property. This brings another benefit — flexibility.

For example, one component displays prompts based on the user’s device. This requirement can be flexibly implemented using composition:

function ByDevice({ children: { mobile, other } }) {  
  returnUtils.isMobile() ? mobile : other; } <ByDevice>{{mobile: <div> Mobile device detected! </div>, other: <div> non-mobile devices </div>}}</ByDevice>Copy the code

effectiveness

The user interface is a composite, layered structure. This combination of components is an efficient way to build a UI.

II. Reusability

A reusable component that can be written once and used many times

Imagine a software development world that reinvented the wheel all the time.

People writing code cannot use any existing libraries or tools. You can’t even use any code you’ve written in the same application.

Is it possible to write an application in a reasonable amount of time in this environment? No way.

Bring in reusability — making things work, not reinventing how to make them work.

Reuse within applications

According to the Don’t Repeat Yourself (DRY) principle, each piece of knowledge should be separate, unambiguous, and authoritative in the system. The DRY principle is designed to avoid duplication.

Code duplication increases complexity and maintenance costs without significant value. An update to a piece of logic forces you to modify all its duplicate copies in your application.

The duplication problem is solved by reusable components. Writing once and using it many times is an effective and time-saving strategy.

But enjoying reusability is not free. Components can be said to be reusable only if they comply with the single responsibility principle and proper encapsulation.

Compliance with the single responsibility principle is required:

Reusing a component really means reusing its responsibilities

Therefore, components that have only one responsibility are the easiest to reuse.

When a component improperly has multiple responsibilities, its reusability is severely limited. When you just want to reuse one responsibility, you are faced with responsibilities created by other unneeded implementations.

Asked for a banana, got it — the whole jungle followed.

Properly packaged components do not get stuck with dependencies. The hidden internals and purposeful props make the component well suited for reuse in multiple situations.

Reusing third-party libraries

It’s a beautiful, sunny day, you turn on your computer, and within a few minutes of launching your editor, you take a look at today’s requirements — add a new feature to your app, roll up your sleeves and get ready to work…

However, there is a high probability that what you are trying to do has actually been solved. Due to React’s popularity and great open source community, it’s wise to search for existing solutions.

Check out the Brillout /awesome- React-Components repository and you’ll find an edited menu of reusable components.

A good library will undoubtedly have a structural impact and promote best practices. In my experience, the most influential are React-Router and Redux.

React-router uses declarative routing to plan the structure of a one-page application. Use

to associate a URL with a component. The route then renders the corresponding component when the user accesses the matching URL.

Redux and React-Redux introduce one-way and predictable application state management. With them, asynchronous and impure code (such as HTTP requests) can be extracted from components to conform to the single responsibility principle and create components that are pure or almost pure.

Here’s a checklist to determine if third-party libraries are useful:

  • Documentation: Check that the library has a meaningful readme.md file and detailed documentation
  • Tested: A notable feature of trusted libraries is high code coverage
  • Maintenance: How often does the library author create new features, fix bugs, and perform routine maintenance


(end)


Please indicate the source of reprint



Long press the QR code or search fewelife to follow us