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

DWQA QuestionsCategory: Web Design HTMLWhich is preferred option with in callback refs and findDOMNode()?
Editor Staff asked 1 week ago

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

1 Answers
Editor Staff answered 1 week 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 {
  constructor(props){
    super(props);
    this.node = createRef();
  }
  componentDidMount() {
    this.node.current.scrollIntoView();
  }

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