You get a bonus - 1 coin for daily activity. Now you have 1 coin

Which is preferable, callback refs or findDOMNode()? in REACT

Practice



It is preferable to use callback refs over the

findDOMNode()
API. Because
findDOMNode()
gets in the way of certain improvements in React in the future.

The legacy approach of using

findDOMNode
:
class MyComponent extends Component {
  componentDidMount() {
    findDOMNode(this).scrollIntoView()
  }

  render() {
    return <div />
  }
}

The recommended approach:


class MyComponent extends Component {
  constructor(props){
    super(props);
    this.node = createRef();
  }
  componentDidMount() {
    this.node.current.scrollIntoView();
  }

  render() {
    return <div ref={this.node} />
  }
}


created: 2020-02-23
updated: 2026-03-10
248



Was this answer useful?
Choose a quick rating so we can improve the next answer for you.
How satisfied are you?


Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Scripting client side JavaScript, jqvery, BackBone"

Terms: Scripting client side JavaScript, jqvery, BackBone