These docs are old and won’t be updated. Go toreact.dev for the new React docs.
These new documentation pages teach modern React:
Thereact-dom package provides DOM-specific methods that can be used at the top level of your app and as an escape hatch to get outside the React model if you need to.
import*as ReactDOMfrom'react-dom';If you use ES5 with npm, you can write:
var ReactDOM=require('react-dom');Thereact-dom package also provides modules specific to client and server apps:
Thereact-dom package exports these methods:
Thesereact-dom methods are also exported, but are considered legacy:
Note:
Both
renderandhydratehave been replaced with newclient methods in React 18. These methods will warn that your app will behave as if it’s running React 17 (learn morehere).
React supports all modern browsers, althoughsome polyfills are required for older versions.
Note
We do not support older browsers that don’t support ES5 methods or microtasks such as Internet Explorer. You may find that your apps do work in older browsers if polyfills such ases5-shim and es5-sham are included in the page, but you’re on your own if you choose to take this path.
createPortal()This content is out of date.
Read the new React documentation for
createPortal.
createPortal(child, container)Creates a portal. Portals provide a way torender children into a DOM node that exists outside the hierarchy of the DOM component.
flushSync()This content is out of date.
Read the new React documentation for
flushSync.
flushSync(callback)Force React to flush any updates inside the provided callback synchronously. This ensures that the DOM is updated immediately.
// Force this state update to be synchronous.flushSync(()=>{setCount(count+1);});// By this point, DOM is updated.Note:
flushSynccan significantly hurt performance. Use sparingly.
flushSyncmay force pending Suspense boundaries to show theirfallbackstate.
flushSyncmay also run pending effects and synchronously apply any updates they contain before returning.
flushSyncmay also flush updates outside the callback when necessary to flush the updates inside the callback. For example, if there are pending updates from a click, React may flush those before flushing the updates inside the callback.
render()This content is out of date.
Read the new React documentation for
render.
render(element, container[, callback])Note:
renderhas been replaced withcreateRootin React 18. SeecreateRoot for more info.
Render a React element into the DOM in the suppliedcontainer and return areference to the component (or returnsnull forstateless components).
If the React element was previously rendered intocontainer, this will perform an update on it and only mutate the DOM as necessary to reflect the latest React element.
If the optional callback is provided, it will be executed after the component is rendered or updated.
Note:
render()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.
render()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.
render()currently returns a reference to the rootReactComponentinstance. However, using this return value is legacyand should be avoided because future versions of React may render components asynchronously in some cases. If you need a reference to the rootReactComponentinstance, the preferred solution is to attach acallback ref to the root element.Using
render()to hydrate a server-rendered container is deprecated. UsehydrateRoot()instead.
hydrate()This content is out of date.
Read the new React documentation for
hydrate.
hydrate(element, container[, callback])Note:
hydratehas been replaced withhydrateRootin React 18. SeehydrateRoot for more info.
Same asrender(), but is used to hydrate a container whose HTML contents were rendered byReactDOMServer. React will attempt to attach event listeners to the existing markup.
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.
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 addingsuppressHydrationWarning={true} 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.
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 likethis.state.isClient, which you can set totrue incomponentDidMount(). 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.
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.
unmountComponentAtNode()This content is out of date.
Read the new React documentation for
unmountComponentAtNode.
unmountComponentAtNode(container)Note:
unmountComponentAtNodehas been replaced withroot.unmount()in React 18. SeecreateRoot for more info.
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. Returnstrue if a component was unmounted andfalse if there was no component to unmount.
findDOMNode()This content is out of date.
Read the new React documentation for
findDOMNode.
Note:
findDOMNodeis 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.It has been deprecated inStrictMode.
findDOMNode(component)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.In most cases, you can attach a ref to the DOM node and avoid usingfindDOMNode at all.
When a component renders tonull orfalse,findDOMNode returnsnull. When a component renders to a string,findDOMNode returns a text DOM node containing that value. As of React 16, a component may return a fragment with multiple children, in which casefindDOMNode will return the DOM node corresponding to the first non-empty child.
Note:
findDOMNodeonly 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 callingfindDOMNode()inrender()on a component that has yet to be created) an exception will be thrown.
findDOMNodecannot be used on function components.