{"componentChunkName":"component---src-templates-docs-js","path":"/docs/react-dom.html","result":{"data":{"markdownRemark":{"html":"<p>If you load React from a <code class=\"gatsby-code-text\">&lt;script&gt;</code> tag, these top-level APIs are available on the <code class=\"gatsby-code-text\">ReactDOM</code> global. If you use ES6 with npm, you can write <code class=\"gatsby-code-text\">import ReactDOM from &#39;react-dom&#39;</code>. If you use ES5 with npm, you can write <code class=\"gatsby-code-text\">var ReactDOM = require(&#39;react-dom&#39;)</code>.</p>\n<h2 id=\"overview\"><a href=\"#overview\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Overview </h2>\n<p>The <code class=\"gatsby-code-text\">react-dom</code> package provides DOM-specific methods that can be used at the top level of your app and as an escape hatch to get outside of the React model if you need to. Most of your components should not need to use this module.</p>\n<ul>\n<li><a href=\"#render\"><code class=\"gatsby-code-text\">render()</code></a></li>\n<li><a href=\"#hydrate\"><code class=\"gatsby-code-text\">hydrate()</code></a></li>\n<li><a href=\"#unmountcomponentatnode\"><code class=\"gatsby-code-text\">unmountComponentAtNode()</code></a></li>\n<li><a href=\"#finddomnode\"><code class=\"gatsby-code-text\">findDOMNode()</code></a></li>\n<li><a href=\"#createportal\"><code class=\"gatsby-code-text\">createPortal()</code></a></li>\n</ul>\n<h3 id=\"browser-support\"><a href=\"#browser-support\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Browser Support </h3>\n<p>React supports all popular browsers, including Internet Explorer 9 and above, although <a href=\"/docs/javascript-environment-requirements.html\">some polyfills are required</a> for older browsers such as IE 9 and IE 10.</p>\n<blockquote>\n<p>Note</p>\n<p>We don’t support older browsers that don’t support ES5 methods, but you may find that your apps do work in older browsers if polyfills such as <a href=\"https://github.com/es-shims/es5-shim\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">es5-shim and es5-sham</a> are included in the page. You’re on your own if you choose to take this path.</p>\n</blockquote>\n<hr>\n<h2 id=\"reference\"><a href=\"#reference\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reference </h2>\n<h3 id=\"render\"><a href=\"#render\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"gatsby-code-text\">render()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\">ReactDOM<span class=\"token punctuation\">.</span><span class=\"token function\">render</span><span class=\"token punctuation\">(</span>element<span class=\"token punctuation\">,</span> container<span class=\"token punctuation\">[</span><span class=\"token punctuation\">,</span> callback<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>Render a React element into the DOM in the supplied <code class=\"gatsby-code-text\">container</code> and return a <a href=\"/docs/more-about-refs.html\">reference</a> to the component (or returns <code class=\"gatsby-code-text\">null</code> for <a href=\"/docs/components-and-props.html#function-and-class-components\">stateless components</a>).</p>\n<p>If the React element was previously rendered into <code class=\"gatsby-code-text\">container</code>, this will perform an update on it and only mutate the DOM as necessary to reflect the latest React element.</p>\n<p>If the optional callback is provided, it will be executed after the component is rendered or updated.</p>\n<blockquote>\n<p>Note:</p>\n<p><code class=\"gatsby-code-text\">ReactDOM.render()</code> controls the contents of the container node you pass in. Any existing DOM elements inside are replaced when first called. Later calls use React’s DOM diffing algorithm for efficient updates.</p>\n<p><code class=\"gatsby-code-text\">ReactDOM.render()</code> does not modify the container node (only modifies the children of the container). It may be possible to insert a component to an existing DOM node without overwriting the existing children.</p>\n<p><code class=\"gatsby-code-text\">ReactDOM.render()</code> currently returns a reference to the root <code class=\"gatsby-code-text\">ReactComponent</code> instance. However, using this return value is legacy\nand should be avoided because future versions of React may render components asynchronously in some cases. If you need a reference to the root <code class=\"gatsby-code-text\">ReactComponent</code> instance, the preferred solution is to attach a\n<a href=\"/docs/more-about-refs.html#the-ref-callback-attribute\">callback ref</a> to the root element.</p>\n<p>Using <code class=\"gatsby-code-text\">ReactDOM.render()</code> to hydrate a server-rendered container is deprecated and will be removed in React 17. Use <a href=\"#hydrate\"><code class=\"gatsby-code-text\">hydrate()</code></a> instead.</p>\n</blockquote>\n<hr>\n<h3 id=\"hydrate\"><a href=\"#hydrate\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"gatsby-code-text\">hydrate()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\">ReactDOM<span class=\"token punctuation\">.</span><span class=\"token function\">hydrate</span><span class=\"token punctuation\">(</span>element<span class=\"token punctuation\">,</span> container<span class=\"token punctuation\">[</span><span class=\"token punctuation\">,</span> callback<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>Same as <a href=\"#render\"><code class=\"gatsby-code-text\">render()</code></a>, but is used to hydrate a container whose HTML contents were rendered by <a href=\"/docs/react-dom-server.html\"><code class=\"gatsby-code-text\">ReactDOMServer</code></a>. React will attempt to attach event listeners to the existing markup.</p>\n<p>React expects that the rendered content is identical between the server and the client. It can patch up differences in text content, but you should treat mismatches as bugs and fix them. In development mode, React warns about mismatches during hydration. There are no guarantees that attribute differences will be patched up in case of mismatches. This is important for performance reasons because in most apps, mismatches are rare, and so validating all markup would be prohibitively expensive.</p>\n<p>If a single element’s attribute or text content is unavoidably different between the server and the client (for example, a timestamp), you may silence the warning by adding <code class=\"gatsby-code-text\">suppressHydrationWarning={true}</code> to the element. It only works one level deep, and is intended to be an escape hatch. Don’t overuse it. Unless it’s text content, React still won’t attempt to patch it up, so it may remain inconsistent until future updates.</p>\n<p>If you intentionally need to render something different on the server and the client, you can do a two-pass rendering. Components that render something different on the client can read a state variable like <code class=\"gatsby-code-text\">this.state.isClient</code>, which you can set to <code class=\"gatsby-code-text\">true</code> in <code class=\"gatsby-code-text\">componentDidMount()</code>. This way the initial render pass will render the same content as the server, avoiding mismatches, but an additional pass will happen synchronously right after hydration. Note that this approach will make your components slower because they have to render twice, so use it with caution.</p>\n<p>Remember to be mindful of user experience on slow connections. The JavaScript code may load significantly later than the initial HTML render, so if you render something different in the client-only pass, the transition can be jarring. However, if executed well, it may be beneficial to render a “shell” of the application on the server, and only show some of the extra widgets on the client. To learn how to do this without getting the markup mismatch issues, refer to the explanation in the previous paragraph.</p>\n<hr>\n<h3 id=\"unmountcomponentatnode\"><a href=\"#unmountcomponentatnode\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"gatsby-code-text\">unmountComponentAtNode()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\">ReactDOM<span class=\"token punctuation\">.</span><span class=\"token function\">unmountComponentAtNode</span><span class=\"token punctuation\">(</span>container<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Remove a mounted React component from the DOM and clean up its event handlers and state. If no component was mounted in the container, calling this function does nothing. Returns <code class=\"gatsby-code-text\">true</code> if a component was unmounted and <code class=\"gatsby-code-text\">false</code> if there was no component to unmount.</p>\n<hr>\n<h3 id=\"finddomnode\"><a href=\"#finddomnode\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"gatsby-code-text\">findDOMNode()</code> </h3>\n<blockquote>\n<p>Note:</p>\n<p><code class=\"gatsby-code-text\">findDOMNode</code> is an escape hatch used to access the underlying DOM node. In most cases, use of this escape hatch is discouraged because it pierces the component abstraction. <a href=\"/docs/strict-mode.html#warning-about-deprecated-finddomnode-usage\">It has been deprecated in <code class=\"gatsby-code-text\">StrictMode</code>.</a></p>\n</blockquote>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\">ReactDOM<span class=\"token punctuation\">.</span><span class=\"token function\">findDOMNode</span><span class=\"token punctuation\">(</span>component<span class=\"token punctuation\">)</span></code></pre></div>\n<p>If this component has been mounted into the DOM, this returns the corresponding native browser DOM element. This method is useful for reading values out of the DOM, such as form field values and performing DOM measurements. <strong>In most cases, you can attach a ref to the DOM node and avoid using <code class=\"gatsby-code-text\">findDOMNode</code> at all.</strong></p>\n<p>When a component renders to <code class=\"gatsby-code-text\">null</code> or <code class=\"gatsby-code-text\">false</code>, <code class=\"gatsby-code-text\">findDOMNode</code> returns <code class=\"gatsby-code-text\">null</code>. When a component renders to a string, <code class=\"gatsby-code-text\">findDOMNode</code> returns a text DOM node containing that value. As of React 16, a component may return a fragment with multiple children, in which case <code class=\"gatsby-code-text\">findDOMNode</code> will return the DOM node corresponding to the first non-empty child.</p>\n<blockquote>\n<p>Note:</p>\n<p><code class=\"gatsby-code-text\">findDOMNode</code> only works on mounted components (that is, components that have been placed in the DOM). If you try to call this on a component that has not been mounted yet (like calling <code class=\"gatsby-code-text\">findDOMNode()</code> in <code class=\"gatsby-code-text\">render()</code> on a component that has yet to be created) an exception will be thrown.</p>\n<p><code class=\"gatsby-code-text\">findDOMNode</code> cannot be used on function components.</p>\n</blockquote>\n<hr>\n<h3 id=\"createportal\"><a href=\"#createportal\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"gatsby-code-text\">createPortal()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\">ReactDOM<span class=\"token punctuation\">.</span><span class=\"token function\">createPortal</span><span class=\"token punctuation\">(</span>child<span class=\"token punctuation\">,</span> container<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Creates a portal. Portals provide a way to <a href=\"/docs/portals.html\">render children into a DOM node that exists outside the hierarchy of the DOM component</a>.</p>","frontmatter":{"title":"ReactDOM","next":null,"prev":null},"fields":{"path":"content/docs/reference-react-dom.md","slug":"docs/react-dom.html"}}},"pageContext":{"slug":"docs/react-dom.html"}},"staticQueryHashes":[]}