diff --git a/src/content/reference/react-dom/createPortal.md b/src/content/reference/react-dom/createPortal.md index 0107c20d3e1..b805a43acd2 100644 --- a/src/content/reference/react-dom/createPortal.md +++ b/src/content/reference/react-dom/createPortal.md @@ -29,13 +29,17 @@ To create a portal, call `createPortal`, passing some JSX, and the DOM node wher ```js import { createPortal } from 'react-dom'; +const portalNode = document.createElement('div') +portalNode.id = 'portal' +document.appendChild(portalNode) + // ...

This child is placed in the parent div.

{createPortal( -

This child is placed in the document body.

, - document.body +

This child is placed at the end of document body in a portal-specific DOM node.

, + portalNode )}
``` @@ -70,16 +74,20 @@ A portal only changes the physical placement of the DOM node. In every other way To create a portal, render the result of `createPortal` with some JSX and the DOM node where it should go: -```js [[1, 8, "

This child is placed in the document body.

"], [2, 9, "document.body"]] +```js [[1, 11, "

This child is placed at the end of document body.

"], [2, 12, "portalNode"]] import { createPortal } from 'react-dom'; +const portalNode = document.createElement('div') +portalNode.id = 'portal' +document.appendChild(portalNode) + function MyComponent() { return (

This child is placed in the parent div.

{createPortal( -

This child is placed in the document body.

, - document.body +

This child is placed at the end of document body.

, + portalNode )}
); @@ -88,20 +96,24 @@ function MyComponent() { React will put the DOM nodes for the JSX you passed inside of the DOM node you provided. -Without a portal, the second `

` would be placed inside the parent `

`, but the portal "teleported" it into the [`document.body`:](https://developer.mozilla.org/en-US/docs/Web/API/Document/body) +Without a portal, the second `

` would be placed inside the parent `

`, but the portal "teleported" it into the end of [`document.body`:](https://developer.mozilla.org/en-US/docs/Web/API/Document/body) in a portal-specific node. ```js import { createPortal } from 'react-dom'; +const portalNode = document.createElement('div') +portalNode.id = 'portal' +document.appendChild(portalNode) + export default function MyComponent() { return (

This child is placed in the parent div.

{createPortal( -

This child is placed in the document body.

, - document.body +

This child is placed at the end of document body.

, + portalNode )}
); @@ -179,6 +191,10 @@ import { useState } from 'react'; import { createPortal } from 'react-dom'; import ModalContent from './ModalContent.js'; +const portalNode = document.createElement('div') +portalNode.id = 'portal' +document.appendChild(portalNode) + export default function PortalExample() { const [showModal, setShowModal] = useState(false); return ( @@ -188,7 +204,7 @@ export default function PortalExample() { {showModal && createPortal( setShowModal(false)} />, - document.body + portalNode )} );