title: Why use Ref when we can use querySelector?
body: <p>I am new in the React world and was learning about Refs. The React doc says that:</p>
  
  <blockquote>
    <p>Refs provide a way to access DOM nodes or React elements created in
    the render method.</p>
  </blockquote>
  
  <p>I think that we can do the same thing with the <code>querySelector</code> function.<br>
  The React doc gives an example of how can we use Refs to focus on the input element.<br>
  <strong>Here's the code -></strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="true">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>class CustomTextInput extends React.Component {
    constructor(props) {
      super(props);
      // create a ref to store the textInput DOM element
      this.textInput = React.createRef();
      this.focusTextInput = this.focusTextInput.bind(this);
    }
  
    focusTextInput() {
      // Explicitly focus the text input using the raw DOM API
      // Note: we're accessing "current" to get the DOM node
      this.textInput.current.focus();
    }
  
    render() {
      // tell React that we want to associate the &lt;input&gt; ref
      // with the `textInput` that we created in the constructor
      return (
        &lt;div&gt;
          &lt;input
            type="text"
            ref={this.textInput} /&gt;
          &lt;input
            type="button"
            value="Focus the text input"
            onClick={this.focusTextInput}
          /&gt;
        &lt;/div&gt;
      );
    }
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Doing the same thing with querySelector</strong> -></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="true">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>class CustomTextInput extends React.Component {
  constructor(props) {
      super(props);
      this.textInput = React.createRef();
    }
  
    focusTextInput = () =&gt; {
      const input = document.querySelector("input");
      input.focus();
    };
  
    render() {
      return (
        &lt;div&gt;
          &lt;input type="text" ref={this.textInput} /&gt;
          &lt;button type="button" onClick={this.focusTextInput}&gt;
            Focus the Text Input
          &lt;/button&gt;
        &lt;/div&gt;
      );
    }
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>