title: How does React.useState triggers re-render?
body: <pre class="lang-js prettyprint-override"><code>import { useState } from 'react';
  
  function Example() {
    const [count, setCount] = useState(0);
  
    return (
      &lt;div&gt;
        &lt;p&gt;You clicked {count} times&lt;/p&gt;
        &lt;button onClick={() =&gt; setCount(count + 1)}&gt;
          Click me
        &lt;/button&gt;
      &lt;/div&gt;
    );
  }
  </code></pre>
  
  
  
  <p>In the above example whenever <code>setCount(count + 1)</code> is invoked a re-render  happens. I am curious to learn the flow.</p>
  
  <p>I tried looking into the source code. I could not find any reference of <code>useState</code> or other hooks at <a href="https://github.com/facebook/react/search?q=useState&amp;unscoped_q=useState" rel="noreferrer">github.com/facebook/react</a>.</p>
  
  <p>I installed <code>react@next</code> via <code>npm i react@next</code> and found the following at <code>node_modules/react/cjs/react.development.js</code></p>
  
  <pre class="lang-js prettyprint-override"><code>function useState(initialState) {
    var dispatcher = resolveDispatcher();
    return dispatcher.useState(initialState);
  }
  </code></pre>
  
  
  
  <p>On tracing back for <code>dispatcher.useState()</code>, I could only find the following ...</p>
  
  <pre class="lang-js prettyprint-override"><code>function resolveDispatcher() {
    var dispatcher = ReactCurrentOwner.currentDispatcher;
    !(dispatcher !== null) ? invariant(false, 'Hooks can only be called inside the body of a function component.') : void 0;
    return dispatcher;
  }
  </code></pre>
  
  
  
  <pre class="lang-js prettyprint-override"><code>var ReactCurrentOwner = {
    /**
     * @internal
     * @type {ReactComponent}
     */
    current: null,
    currentDispatcher: null
  };
  </code></pre>
  
  
  
  <p>I wonder where can I find <code>dispatcher.useState()</code> implementation and learn how it triggers re-render when <s><code>setState</code></s> <code>setCount</code> is invoked.</p>
  
  <p>Any pointer would be helpful.</p>
  
  <p>Thanks! </p>