0 votes
in ReactJS by
Which is preferred option with in callback refs and findDOMNode() in ReactJS? Provide Example of it?

1 Answer

0 votes
by

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} />;
  }
}

Related questions

0 votes
asked Nov 3, 2023 in ReactJS by AdilsonLima
0 votes
asked Nov 4, 2023 in ReactJS by AdilsonLima
...