0

from the docs

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }
  render() {
    return <div ref={this.myRef} />;
  }
}

my question is will this.myRef.current refer to the actual DOM node or to the react virtual DOM's node?

laxman
  • 1,781
  • 4
  • 14
  • 32
  • actual DOM node - https://reactjs.org/docs/refs-and-the-dom.html you can use JS DOM api and test to verify `this.myRef.current.innerText="some text"` – Kunukn Oct 08 '18 at 08:50
  • https://stackoverflow.com/questions/44488816/in-react-do-refs-reference-the-virtual-dom-or-the-actual-dom – Just code Oct 08 '18 at 08:51
  • 1
    Good find, @Justcode! – T.J. Crowder Oct 08 '18 at 08:51
  • @Kunukn Is it anything different from this? "When the ref attribute is used on a custom class component, the ref object receives the mounted instance of the component as its current" as quoted in docs – laxman Oct 08 '18 at 08:59
  • 1
    @ClaxX the ref is actual DOM, you can try to insert this in your code example `componentDidMount(){console.log(this.myRef.current)}` It will print the DOM node or try with this code `componentDidMount(){this.myRef.current.innerText = "DOM node"}` – Kunukn Oct 08 '18 at 09:31

0 Answers0