The findDOMNode warning means React can’t find the DOM node you asked for because it’s no longer managed by React.

Common Causes and Fixes:

  1. Component Unmounted Before findDOMNode Call:

    • Diagnosis: This is the most frequent culprit. You’re calling findDOMNode in a lifecycle method like componentDidUpdate or componentDidMount after the component has already been unmounted or is in the process of unmounting. This often happens when a parent component conditionally renders its children, and the child is removed before your findDOMNode call executes.
    • Check: Look for logic where a parent component might remove a child that you’re trying to access. Debugging with console.log statements immediately before the findDOMNode call, showing the component’s this.state or this.props related to its existence, can help.
    • Fix: Ensure findDOMNode is only called when the component is guaranteed to be mounted. The best practice is to use useRef in functional components or React.createRef() in class components and access the DOM node via this.myRef.current. If you must use findDOMNode (which you should avoid), add a check:
      const node = findDOMNode(this);
      if (node) {
          // Your DOM manipulation logic
      }
      
    • Why it works: The if (node) check prevents errors by only executing the subsequent DOM manipulation if findDOMNode actually returned a valid DOM element, meaning the component is still mounted and managed by React.
  2. findDOMNode Called on a Non-React Component Instance:

    • Diagnosis: You’re trying to find the DOM node of something that isn’t a React component instance. This could be a plain JavaScript object, a primitive value, or even a DOM node itself passed incorrectly.
    • Check: Verify the this or the argument passed to findDOMNode is indeed a React component instance. In class components, this refers to the component instance. If you’re passing another value, ensure it’s the correct component instance.
    • Fix: Ensure you are calling findDOMNode with a valid React component instance. If you’re passing an argument, make sure it’s a React.Component or React.PureComponent instance.
      // Correct usage:
      class MyComponent extends React.Component {
          componentDidMount() {
              const node = findDOMNode(this); // 'this' is the component instance
              // ...
          }
          // ...
      }
      
      // Incorrect usage example:
      // const someObject = {};
      // findDOMNode(someObject); // Will cause issues
      
    • Why it works: findDOMNode is designed to traverse the React component tree to find the underlying DOM element associated with a specific React component instance. Passing anything else breaks this traversal.
  3. Using findDOMNode with Fragments:

    • Diagnosis: findDOMNode cannot be used directly on a Fragment. Fragments return an array of children, and findDOMNode expects a single component instance.
    • Check: If your component renders a <React.Fragment> or <>...</>, and you’re trying to get a DOM node from the Fragment itself, this is the issue.
    • Fix: Instead of trying to get a DOM node from the Fragment, get the DOM node from one of its children that is a DOM element or a component with a DOM element.
      // Instead of:
      // const fragmentNode = findDOMNode(this); // If this.render() returns <></>
      
      // Do this:
      class MyComponent extends React.Component {
          render() {
              return (
                  <>
                      <span ref={this.mySpanRef} />
                      <p>Some text</p>
                  </>
              );
          }
          componentDidMount() {
              // Access the span's DOM node directly
              console.log(this.mySpanRef.current);
          }
      }
      
    • Why it works: Fragments are a syntactic sugar that don’t produce their own DOM node. By targeting a specific DOM-producing element within the Fragment, you provide findDOMNode (or preferably, refs) with a valid target.
  4. Refs are a Better Alternative:

    • Diagnosis: You are using findDOMNode for a task that is now better handled by React’s recommended ref API. findDOMNode is a legacy API, and its use is discouraged because it bypasses React’s declarative nature and can lead to unpredictable behavior, especially in concurrent mode.
    • Check: Review every instance of findDOMNode. If you’re using it to access a DOM element that your component renders, you should be using a ref.
    • Fix: Migrate to React.createRef() for class components or useRef() for functional components.
      // In a class component:
      class MyComponent extends React.Component {
          constructor(props) {
              super(props);
              this.myDivRef = React.createRef(); // Create a ref
          }
      
          componentDidMount() {
              // Access the DOM node via the ref's .current property
              if (this.myDivRef.current) {
                  this.myDivRef.current.style.backgroundColor = 'red';
              }
          }
      
          render() {
              return <div ref={this.myDivRef}>Hello</div>; // Attach the ref
          }
      }
      
      // In a functional component:
      import React, { useRef, useEffect } from 'react';
      
      function MyFunctionalComponent() {
          const myDivRef = useRef(null); // Create a ref
      
          useEffect(() => {
              // Access the DOM node via the ref's .current property
              if (myDivRef.current) {
                  myDivRef.current.style.backgroundColor = 'blue';
              }
          }, []); // Run this effect after the component mounts
      
          return <div ref={myDivRef}>Hello</div>; // Attach the ref
      }
      
    • Why it works: Refs provide a direct and stable way to access DOM nodes or component instances managed by React. They are designed to work seamlessly with React’s rendering lifecycle and are the intended mechanism for imperative DOM access.
  5. Third-Party Libraries or Older Code:

    • Diagnosis: You might be using a third-party library that internally uses findDOMNode, or you’re working with older React code that hasn’t been updated.
    • Check: If the warning originates from within a library’s code, check if there’s an updated version of the library that resolves the deprecation. If it’s your own older code, treat it as a call to refactor.
    • Fix: Update the third-party library to its latest version. If it’s your code, refactor it to use refs. If you cannot update a library and cannot refactor your own code immediately, you can try to wrap the problematic component in a new component that uses refs to access its DOM, but this is a workaround and not a true fix.
    • Why it works: Newer library versions often adopt modern React APIs. Refactoring your code ensures it adheres to current best practices, making it more robust and compatible with future React versions.

The next error you’ll likely encounter if you’ve fixed all findDOMNode issues is a warning about using componentWillReceiveProps or componentWillUpdate if those lifecycle methods are still present in your class components.

Want structured learning?

Take the full React course →