Which is preferred option with in callback refs and findDOMNode()?

Technology CommunityCategory: ReactWhich is preferred option with in callback refs and findDOMNode()?
VietMX Staff asked 3 years ago

It is preferred to use callback refs over findDOMNode() API because findDOMNode() prevents 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 is

class MyComponent extends Component {
  componentDidMount() {
    this.node.scrollIntoView();
  }

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