--- title: findDOMNode --- This API will be removed in a future major version of React. `findDOMNode` finds the browser DOM node for a React [class component](/apis/react/Component) instance. ``` const domNode = findDOMNode(componentInstance) ``` --- ## Usage {/*usage*/} ### Finding the root DOM node of a class component {/*finding-the-root-dom-node-of-a-class-component*/} This API will be removed in a future major version of React. Call `findDOMNode` with a [class component](/apis/react/Component) instance (usually, `this`) to find the DOM node it has rendered. ```js {3} class AutoselectingInput extends Component { componentDidMount() { const input = findDOMNode(this); input.select() } render() { return } } ``` Here, the `input` variable will be set to the `` DOM element. This lets you do something with it. For example, when clicking "Show example" below mounts the input, [`input.select()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select) selects all text in the input: ```js App.js import { useState } from 'react'; import AutoselectingInput from './AutoselectingInput.js'; export default function App() { const [show, setShow] = useState(false); return ( <>
{show && } ); } ``` ```js AutoselectingInput.js active import { Component } from 'react'; import { findDOMNode } from 'react-dom'; class AutoselectingInput extends Component { componentDidMount() { const input = findDOMNode(this); input.select() } render() { return } } export default AutoselectingInput; ```
Using `findDOMNode` leads to fragile code. [See the alternatives.](#alternatives) --- ## Alternatives {/*alternatives*/} ### Reading component's own DOM node from a ref {/*reading-components-own-dom-node-from-a-ref*/} Code using `findDOMNode` is fragile because the connection between the JSX node and the code manipulating the corresponding DOM node is not explicit. For example, try wrapping `` from this example into a `
`: ```js App.js import { useState } from 'react'; import AutoselectingInput from './AutoselectingInput.js'; export default function App() { const [show, setShow] = useState(false); return ( <>
{show && } ); } ``` ```js AutoselectingInput.js active import { Component } from 'react'; import { findDOMNode } from 'react-dom'; class AutoselectingInput extends Component { componentDidMount() { const input = findDOMNode(this); input.select() } render() { return } } export default AutoselectingInput; ```
This will break the code because now, `findDOMNode(this)` finds the `
` DOM node, but the code expects an `` DOM node. To avoid these kinds of problems, use [`createRef`](/apis/react/createRef) to manage a specific DOM node. In this example, `findDOMNode` is no longer used. Instead, `inputRef = createRef(null)` is defined as an instance field on the class. To read the DOM node from it, you can use `this.inputRef.current`. To attach it to the JSX, you render ``. You have connected the code using the DOM node to its JSX: ```js App.js import { useState } from 'react'; import AutoselectingInput from './AutoselectingInput.js'; export default function App() { const [show, setShow] = useState(false); return ( <>
{show && } ); } ``` ```js AutoselectingInput.js active import { createRef, Component } from 'react'; class AutoselectingInput extends Component { inputRef = createRef(null); componentDidMount() { const input = this.inputRef.current; input.select() } render() { return ( ); } } export default AutoselectingInput; ```
In modern React without class components, the equivalent code would call [`useRef`](/apis/react/useRef) instead: ```js App.js import { useState } from 'react'; import AutoselectingInput from './AutoselectingInput.js'; export default function App() { const [show, setShow] = useState(false); return ( <>
{show && } ); } ``` ```js AutoselectingInput.js active import { useRef, useEffect } from 'react'; export default function AutoselectingInput() { const inputRef = useRef(null); useEffect(() => { const input = inputRef.current; input.select(); }, []); return } ```
[Read more about manipulating the DOM with refs.](/learn/manipulating-the-dom-with-refs) --- ### Reading a child component's DOM node from a forwarded ref {/*reading-a-child-components-dom-node-from-a-forwarded-ref*/} In this example, `findDOMNode(this)` finds a DOM node that belongs to another component. The `AutoselectingInput` renders `MyInput`, which is your own component that renders a browser ``. ```js App.js import { useState } from 'react'; import AutoselectingInput from './AutoselectingInput.js'; export default function App() { const [show, setShow] = useState(false); return ( <>
{show && } ); } ``` ```js AutoselectingInput.js active import { Component } from 'react'; import { findDOMNode } from 'react-dom'; import MyInput from './MyInput.js'; class AutoselectingInput extends Component { componentDidMount() { const input = findDOMNode(this); input.select() } render() { return ; } } export default AutoselectingInput; ``` ```js MyInput.js export default function MyInput() { return ; } ```
Notice that calling `findDOMNode(this)` inside `AutoselectingInput` still gives you the DOM ``--even though the JSX for this `` is hidden inside the `MyInput` component. This seems convenient for the above example, but it leads to fragile code. Imagine that you wanted to edit `MyInput` later and add a wrapper `
` around it. This would break the code of `AutoselectingInput` (which expects to find an `` DOM node). To replace `findDOMNode` in this example, the two components need to coordinate: 1. `AutoSelectingInput` should declare a ref, like [in the earlier example](#reading-components-own-dom-node-from-a-ref), and pass it to ``. 2. `MyInput` should be declared with [`forwardRef`](/apis/react/forwardRef) to read the passed ref, and pass it down to the `` node. This version does that, so it no longer needs `findDOMNode`: ```js App.js import { useState } from 'react'; import AutoselectingInput from './AutoselectingInput.js'; export default function App() { const [show, setShow] = useState(false); return ( <>
{show && } ); } ``` ```js AutoselectingInput.js active import { createRef, Component } from 'react'; import MyInput from './MyInput.js'; class AutoselectingInput extends Component { inputRef = createRef(null); componentDidMount() { const input = this.inputRef.current; input.select() } render() { return ( ); } } export default AutoselectingInput; ``` ```js MyInput.js import { forwardRef } from 'react'; const MyInput = forwardRef(function MyInput(props, ref) { return ; }); export default MyInput; ```
Here is how this code would look like with function components instead of classes: ```js App.js import { useState } from 'react'; import AutoselectingInput from './AutoselectingInput.js'; export default function App() { const [show, setShow] = useState(false); return ( <>
{show && } ); } ``` ```js AutoselectingInput.js active import { useRef, useEffect } from 'react'; import MyInput from './MyInput.js'; export default function AutoselectingInput() { const inputRef = useRef(null); useEffect(() => { const input = inputRef.current; input.select(); }, []); return } ``` ```js MyInput.js import { forwardRef } from 'react'; const MyInput = forwardRef(function MyInput(props, ref) { return ; }); export default MyInput; ```
--- ### Measuring position and size of the children {/*measuring-position-and-size-of-the-children*/} Sometimes a component needs to know the position and size of its children. This makes it tempting to find the children with `findDOMNode(this)`, and then use DOM methods like [`getBoundingClientRect`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) for measurements. There is currently no direct equivalent for this use case, which is why `findDOMNode` is deprecated but is not yet removed completely from React. In the meantime, you can try rendering a wrapper `
` node around the content as a workaround, and getting a ref to that node. However, extra wrappers can sometimes break styling. ```js
{children}
``` This also applies to focusing and scrolling to arbitrary children. --- ## Reference {/*reference*/} ### `findDOMNode(componentInstance)` {/*finddomnode*/} This API will be removed in a future major version of React. Call `findDOMNode` to find the browser DOM node for a given React [class component](/apis/react/Component) instance. ```js const domNode = findDOMNode(componentInstance); ``` [See examples above.](#usage) #### Parameters {/*parameters*/} * `componentInstance`: An instance of the [`Component`](/apis/react/Component) subclass. For example, `this` inside a class component. #### Returns {/*returns*/} `findDOMNode` returns the first closest browser DOM node within the given `componentInstance`. When a component renders to `null`, or renders `false`, `findDOMNode` returns `null`. When a component renders to a string, `findDOMNode` returns a text DOM node containing that value. #### Caveats {/*caveats*/} * A component may return an array or a [Fragment](/apis/react/Fragment) with multiple children. In that case `findDOMNode`, will return the DOM node corresponding to the first non-empty child. * `findDOMNode` 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 `findDOMNode()` in `render()` on a component that has yet to be created), an exception will be thrown. * `findDOMNode` only returns the result at the time of your call. If a child component renders a different node later, there is no way for you to be notified of this change. * `findDOMNode` accepts a class component instance, so it can't be used with function components.