Sunday, April 14, 2024
HomeSoftware EngineeringThriller Containers - A Newbie's Information to React Fragments

Thriller Containers – A Newbie’s Information to React Fragments

When returning a number of components from a part’s render technique, they have to be wrapped in a single guardian DOM node:

// Wants a <div> wrapper
return (
    <ChildA />
    <ChildB />

This further wrapper <div> within the DOM is usually undesirable. Enter React fragments – a option to group components with out including further nodes.

Brief Syntax

The only fragment syntax is:

return (
    <ChildA />
    <ChildB />

The <></> syntax declares a React fragment. Fragments allow you to skip the wrapper <div>.

Keyed Fragments

Fragments can be keyed to provide little one components a context:

perform Mum or dad() {
  const objects = ['A', 'B', 'C'];
  return (
      { => <Little one key={merchandise} />)} 

const MyFragment = React.Fragment;

Keyed fragments are helpful for record objects that want a context.


Fragments had been launched to cut back further DOM nodes. Some advantages are:

  • Keep away from wrapper nodes in DOM tree
  • Semantically group elements collectively
  • Key record objects with out including wrappers

This improves render effectivity and semantics.

Utilization Suggestions

  • Use quick syntax for inline part teams
  • Key fragments to supply record merchandise context
  • Favor fragments over wrapper divs
  • Don’t overuse – attempt to maintain elements logically grouped

Fragments are a device for cleaner, extra readable part timber.


  • Fragments allow you to group components with out a DOM node
  • Gives shorter syntax vs wrapper divs
  • Keyed fragments present context for lists
  • Improves render effectivity and semantics
  • Use judiciously based on use case

React fragments are a key device for constructing part hierarchies. No extra thriller containers!



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments