A React Portal allows you to render components outside the normal DOM hierarchy, typically into a different DOM node.
How to Use React Portals
1. Create a New React App
If you don’t have a React app yet, create one:
npx create-react-app react-portals-demo cd react-portals-demo npm start
2. Create a Portal Root in public/index.html
Find index.html
inside the public
folder and add a new div
inside <body>
:
<div id="modal-root"></div>
This is where our modal will be rendered using React Portal.