admin

I'm a Full-stack developer

Thẻ

Part 3: React Fragments
Ngày đăng: 18/06/2023

In this part, I will show you about good benefits when using fragments in React.


Previous articles:

PART 1: REACT PROPS AND STATE
PART 2: THE HOOKS ARE USED POPULARLY IN REACT


What are fragments?


Fragments to group a list of children without adding extra nodes to the DOM, and has no effect on the resulting DOM. It is the same as if the elements were not grouped.


You need to use either or a shorter syntax having an empty tag (<></>).


Below is an example of how to use fragments inside the UsernameWithAvatar component.

const UsernameWithAvatar({username, description, photoUrl}) {
  return (
      <>
        <h2>{username}</h2>
        <p>{description}</p>
        <img src={photoUrl} alt="Username with avatar"/>
      </>
    );
}


Why fragments are better than container divs?

  • Fragments are a bit faster and use less memory by not creating an extra DOM node. This only has a real benefit on very large and deep trees.
  • Some CSS mechanisms like Flexbox and CSS Grid have a special parent-child relationship, and adding divs in the middle makes it hard to keep the desired layout.
  • The DOM Inspector is less cluttered.


Wish you all good study !!!

Đề xuất

TypeScript Design Pattern - Adapter
admin08/08/2023

TypeScript Design Pattern - Adapter
This design pattern acts as a bridge between two different interfaces.
TypeScript Design Pattern - Singleton
admin07/08/2023

TypeScript Design Pattern - Singleton
The singleton ensures only a single install is created in a class and provides a method to access this instance everywhere in a codebase.
TypeScript Design Pattern - Builder
admin07/08/2023

TypeScript Design Pattern - Builder
TypeScript Design Pattern - Builder
Mới nhất

Design Patterns
admin07/08/2023

Design Patterns
The design pattern does not be a specific programming language. Almost programming languages might apply design patterns that to resolve a problem repeat.
TypeScript Design Pattern - Bridge
admin08/08/2023

TypeScript Design Pattern - Bridge
Decouple an abstraction from its implementation so that the two can vary independently.
NodeJS Verify and Decode Cognito JWT Tokens
admin12/06/2023

NodeJS Verify and Decode Cognito JWT Tokens
In this article, I will show you how to verify and decode the Cognito JWT Tokens token.