How to Use React Fragments and Portals

Understand React Fragments

React Fragments are a way to group a list of children elements without adding extra nodes to the DOM. This is useful when you want to return multiple elements from a component's render method without adding extra nodes to the DOM. React Fragments are also used to improve the readability of the code, as they allow you to group related elements together. To create a React Fragment, you need to use the React.Fragment component. This component takes a single child element, which can be a React element, an array of React elements, or a string. You can also pass an object with the key property to give the fragment a unique key. To use React Fragments, you need to import the React package from the react module. You can learn more about React Fragments on the React documentation.

Create a React Fragment

React Fragments let you group a list of children without adding extra nodes to the DOM. To create a React Fragment, you need to use the React.Fragment component. This component can be used to wrap a list of elements without adding extra nodes to the DOM. To create a React Fragment, you need to use the React.Fragment component and pass it a list of elements as children. For example, the following code creates a React Fragment with two <p> elements:

<React.Fragment>
  <p>This is the first element.</p>
  <p>This is the second element.</p>
</React.Fragment>

You can also use the shorthand syntax for creating a React Fragment, which is to use an empty tag. For example, the following code creates the same React Fragment as the previous example:

<>
  <p>This is the first element.</p>
  <p>This is the second element.</p>
</>

React Fragments are a great way to group a list of elements without adding extra nodes to the DOM. For more information on React Fragments, you can check out the React documentation.

Understand React Portals

React Portals are a powerful way to render children components into a DOM node that exists outside the DOM hierarchy of the parent component. This allows for components to be rendered into a different part of the page, such as a modal or a sidebar, without having to re-render the entire page. React Portals are created using the ReactDOM.createPortal() method, which takes two arguments: the child component to be rendered and the DOM node where it should be rendered. To use React Portals, you must first understand how React works and how it renders components. Once you understand the basics of React, you can create a React Portal by using the ReactDOM.createPortal() method. To create a React Portal, you must first create a DOM node where the child component will be rendered. This can be done using the document.createElement() method. Once the DOM node is created, you can pass it as the second argument to the ReactDOM.createPortal() method. The first argument should be the child component that you want to render. After the React Portal is created, you can use it to render the child component into the DOM node. To use React Portals together with React Fragments, you can use the React.Fragment component as the first argument to the ReactDOM.createPortal() method. This will allow you to render multiple components into the same DOM node.

Create a React Portal

React Portals are a powerful way to render children components into a DOM node that exists outside the DOM hierarchy of the parent component. This is useful for modals, tooltips, and other components that need to be rendered outside of the parent component. To create a React Portal, you will need to use the ReactDOM.createPortal() method. This method takes two arguments: the child component and the DOM node where you want to render the child component. For example, if you wanted to render a modal component into a <div> with an id of modal-root, you would use the following code:

const modalRoot = document.getElementById('modal-root');
ReactDOM.createPortal(, modalRoot);

The ReactDOM.createPortal() method will render the MyModal component into the modal-root <div>. You can also use the ReactDOM.createPortal() method to render a component into a portal that is created on the fly. To do this, you will need to use the ReactDOM.createPortal() method in combination with the ReactDOM.createElement() method. For example, if you wanted to render a modal component into a new <div> with an id of modal-root, you would use the following code:

const modalRoot = document.createElement('div');
modalRoot.id = 'modal-root';
ReactDOM.createPortal(, modalRoot);

Once you have created the portal, you can use it to render any component you want. React Portals are a great way to render components outside of the parent component, and they can be used to create powerful modals, tooltips, and other components. For more information on React Portals, you can check out the official React documentation.

Use React Fragments and Portals Together

React Fragments and Portals can be used together to create powerful and dynamic user interfaces. React Fragments allow you to group multiple elements together without adding extra nodes to the DOM, while React Portals provide a way to render a component outside of its parent component. When used together, React Fragments and Portals can be used to create complex and dynamic user interfaces. To use React Fragments and Portals together, you will need to create a React Fragment and a React Portal. First, create a React Fragment by wrapping multiple elements in a <React.Fragment> tag. Then, create a React Portal by using the ReactDOM.createPortal() method. This method takes two arguments: the React element to be rendered and the DOM node where the element should be rendered. Finally, you can use the ReactDOM.render() method to render the React Fragment and the React Portal together. This will allow you to create complex and dynamic user interfaces with React Fragments and Portals.

Useful Links