• Latest
  • Trending
How to Improve DOM Structure with React Portal

How to Improve DOM Structure with React Portal

June 17, 2022
Bolt Opens Africa Hub in Nairobi, Kenya

Bolt Opens Africa Hub in Nairobi, Kenya

July 6, 2022
Google Translate adds 10 new African languages

Google Translate adds 10 new African languages

July 6, 2022
Ericsson invites participants to its ‘Together Apart Hackathon’ in Nigeria

Ericsson invites participants to its ‘Together Apart Hackathon’ in Nigeria

July 6, 2022
Ghana GIPC And OT Sign MoU with America’s led black-owned tech business

Ghana GIPC And OT Sign MoU with America’s led black-owned tech business

July 6, 2022
Nigeria, Mozambique approve SpaceX Internet Deployment

Nigeria, Mozambique approve SpaceX Internet Deployment

July 6, 2022
IXAfrica to Build Africa’s Largest Hyperscale-Ready Datacentre in Nairobi

IXAfrica to Build Africa’s Largest Hyperscale-Ready Datacentre in Nairobi

July 6, 2022
Uganda to Launch its First Low Earth Orbit Satellite in September

Uganda to Launch its First Low Earth Orbit Satellite in September

July 6, 2022
Africa Data Centres to Build second 20MW Data Centre in Cape Town

Africa Data Centres to Build second 20MW Data Centre in Cape Town

July 6, 2022
Kingston Announces FURY Beast RGB DDR5 Memory

Kingston Announces FURY Beast RGB DDR5 Memory

July 6, 2022
Akasa Launches the DuoDock MX Dual NVMe Dock

Akasa Launches the DuoDock MX Dual NVMe Dock

July 6, 2022
Areca ARC-1686 Entry-Level NVMe Hardware RAID Adapters

Areca ARC-1686 Entry-Level NVMe Hardware RAID Adapters

July 6, 2022
TeamGroup Xtreem DDR4-4000 CL15 2x 16 GB

TeamGroup Xtreem DDR4-4000 CL15 2x 16 GB

July 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Wednesday, 6 July, 2022
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

How to Improve DOM Structure with React Portal

by ITECHNEWS
June 17, 2022
in Data Science, Leading Stories
0 0
0
How to Improve DOM Structure with React Portal

What is it?

According to the official React documentation,

Portals provide a first-class way to render children into a DOM node that exists outside a DOM hierarchy of the parent component.

Take a look at this example – let’s say you have a Calculator component, inside which there is a Buttons component. It would look something like this:

YOU MAY ALSO LIKE

Bolt Opens Africa Hub in Nairobi, Kenya

Google Translate adds 10 new African languages

An example of nested components

The root component App along with all its child components will be rendered inside a single DOM node (most likely a div with an id of #root). With React Portal, a child component like Buttons can be rendered outside the div with an id of #root. You can imagine the structure of the DOM to look something like this:

DOM structure of nested components

Why should I know this?

In certain situations, it is necessary to render some JSX or a component outside the root element. The best example of such a situation would be a modal.

A modal would typically cover the entire screen. One would expect this to be reflected in the DOM structure as well. However, that’s not what happens in React. If a modal is nested inside a component, that is also how the DOM structure would look like. This can be confusing for people who use screen readers. From an accessibility standpoint, this is an issue.

Resolving this might be a bit easy to do in Vanilla JS and HTML, but harder to do in React. Since React applications have a single mount point, putting an element or component outside this mount point is a challenge. With React Portal, this becomes an easy task.

Although the element or component will be portaled to a different DOM node, it will still function as a regular React element. For our modal this means that it is in a different DOM node than the mount point. But it can still receive props and state and behave like a regular nested React element.

How do I use it?

Let’s use the modal example from the section above. Creating a React Portal and mounting it to a different DOM node can be done in 2 steps:

Step 1:

Create another DOM node for mounting the modal:

Creating a separate DOM node

The React application will be mounted to the div with an id of #root. And the modal will be mounted to the div with an id of #modal-root.

Step 2:

Create a Portal wrapper component:

Creating a wrapper component with React Portals

This wrapper component will take a child element and create a Portal for it. The useEffect hook mounts the element and cleans up when the component is unomunted.

Here is how it might look:

Wrapping the Modal in the wrapper component

Even though the Modal component is nested in the app hierarchy, it will look different in the DOM structure:

New DOM structure after using React Portals

In the real DOM structure, you won’t see the Modal component directly like this, but a div instead. This is simply a demonstration.

Source: Nihar Raote
Via: dev.to
Tags: How to Improve DOM Structure with React Portal
ShareTweetShare
Plugin Install : Subscribe Push Notification need OneSignal plugin to be installed.

Search

No Result
View All Result

Recent News

Bolt Opens Africa Hub in Nairobi, Kenya

Bolt Opens Africa Hub in Nairobi, Kenya

July 6, 2022
Google Translate adds 10 new African languages

Google Translate adds 10 new African languages

July 6, 2022
Ericsson invites participants to its ‘Together Apart Hackathon’ in Nigeria

Ericsson invites participants to its ‘Together Apart Hackathon’ in Nigeria

July 6, 2022

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Bolt Opens Africa Hub in Nairobi, Kenya

Bolt Opens Africa Hub in Nairobi, Kenya

July 6, 2022
Google Translate adds 10 new African languages

Google Translate adds 10 new African languages

July 6, 2022

Recent News

  • Bolt Opens Africa Hub in Nairobi, Kenya July 6, 2022
  • Google Translate adds 10 new African languages July 6, 2022
  • Ericsson invites participants to its ‘Together Apart Hackathon’ in Nigeria July 6, 2022
  • Ghana GIPC And OT Sign MoU with America’s led black-owned tech business July 6, 2022
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© 2021 iTechNewsOnline.Com - Powered by BackUpDataSystems

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© 2021 iTechNewsOnline.Com - Powered by BackUpDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Go to mobile version