Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for React Portals: Intro
Hari Kotha
Hari Kotha

Posted on • Edited on

     

React Portals: Intro

Image description

This article will cover -

  • Basic intro to Portals in React.js
  • Why & How to use react portals

React Portals

Renders react components outside of root DOM element

What is a Portal

React renders all its components inside a single root DOM element (ideally of id = "root").

If instead, we have a use-case where we want to render a component outside of this root element, we use a concept calledReact Portals.

How to create Portals

Ideally, to bootstrap a react app, we use ReactDOM.render method to render our react app to root element.

<divid="root"></div>
Enter fullscreen modeExit fullscreen mode
ReactDOM.render(<SomeReactComponent/>,document.getElementById('root)
Enter fullscreen modeExit fullscreen mode

Similarly, To create a portal we use ReactDOM.createPortal method which takes two parameters

  1. React Component or JSX
  2. HTML DOM element
<divid="root"></div><divid="portal"></div>
Enter fullscreen modeExit fullscreen mode
ReactDOM.createPortal(<h1>Portal goes here</h1>,document.getElementById('portal'))
Enter fullscreen modeExit fullscreen mode

Why to use React Portals

We can use Portals while creating a Modal or Popup as these occupy space outside of the normal element ordering/stacking. Modals sometimes cover the whole page for example take a look at the below code -

<Wrapperstyle={{maxWidth:'250px',zIndex:1,position:'relative'}}><Modalopenstyle={{zIndex:1000,postion:'fixed',top:0,left:0}}><p>Some content inside Modal</p></Modal></Wrapper><OtherWrapperstyle={{zIndex:100}}><div>some other content</div></OtherWrapper>
Enter fullscreen modeExit fullscreen mode

As per the example above,Modal is being rendered inside aWrapper that has max-width limitation and a z-index set to 1. That means even though Modal has a z-index of 1000, it cannot be on top of everything becauseOtherWrapper has a higher z-index thanWrapper.

Hence, Modal(z-index=1000) will be still under OtherWrapper(zIndex=100) as Modal is inside Wrapper(z-index=1).

To overcome these kinds of issues, we can implement a Portal to render Modal -

<body><divid="root"></div><divid="portal-root"></div></body>
Enter fullscreen modeExit fullscreen mode
return ReactDOM.createPortal(<div>        Modal content goes here</div>,    document.getElementById('portal-root'))
Enter fullscreen modeExit fullscreen mode

This will render the Modal component insideportal-root DOM element outside the root element. However, this is will be kept as usual in React component tree & events are listened & executed as before.

Thanks for checking!
Stay safe :)

References:
https://reactjs.org/docs/portals.html

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Engineer.Works on JavaScript | React.js | Node.js
  • Location
    Hyderabad
  • Pronouns
    He/Him
  • Work
    Software Engineer
  • Joined

More fromHari Kotha

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp