DEV Community

Discussion on: How to create an efficient Modal component in React using Hooks and Portals

 
tripol profile image
Ekanem

1) You can place the document.getElementById ('root') in a variable and add a null check. For example:

const portalDiv = document.getElementById('root')!

2) Pass the portalDiv as the second parameter AFTER the </> closing fragment