<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[React]]></title><description><![CDATA[A JavaScript library for building user interfaces]]></description><link>https://reactjs.org</link><generator>RSS for Node</generator><lastBuildDate>Sat, 01 Sep 2018 03:45:12 GMT</lastBuildDate><item><title><![CDATA[React v16.4.2: Server-side vulnerability fix]]></title><description><![CDATA[<p>We discovered a minor vulnerability that might affect some apps using ReactDOMServer. We are releasing a patch version for every affected React minor release so that you can upgrade with no friction. Read on for more details.</p>
<h2 id="short-description"><a href="#short-description" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Short Description</h2>
<p>Today, we are releasing a fix for a vulnerability we discovered in the <code class="gatsby-code-text">react-dom/server</code> implementation. It was introduced with the version 16.0.0 and has existed in all subsequent releases until today.</p>
<p>This vulnerability <strong>can only affect some server-rendered React apps.</strong> Purely client-rendered apps are <strong>not</strong> affected. Additionally, we expect that most server-rendered apps don’t contain the vulnerable pattern described below. Nevertheless, we recommend to follow the mitigation instructions at the earliest opportunity.</p>
<p>While we were investigating this vulnerability, we found similar vulnerabilities in a few other popular front-end libraries. We have coordinated this release together with <a href="https://github.com/vuejs/vue/releases/tag/v2.5.17">Vue</a> and <a href="https://github.com/developit/preact-render-to-string/releases/tag/3.7.1">Preact</a> releases fixing the same issue. The tracking number for this vulnerability is <code class="gatsby-code-text">CVE-2018-6341</code>.</p>
<h2 id="mitigation"><a href="#mitigation" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Mitigation</h2>
<p><strong>We have prepared a patch release with a fix for every affected minor version.</strong></p>
<h3 id="160x"><a href="#160x" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>16.0.x</h3>
<p>If you’re using <code class="gatsby-code-text">react-dom/server</code> with this version:</p>
<ul>
<li><code class="gatsby-code-text">react-dom@16.0.0</code></li>
</ul>
<p>Update to this version instead:</p>
<ul>
<li><code class="gatsby-code-text">react-dom@16.0.1</code> <strong>(contains the mitigation)</strong></li>
</ul>
<h3 id="161x"><a href="#161x" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>16.1.x</h3>
<p>If you’re using <code class="gatsby-code-text">react-dom/server</code> with one of these versions:</p>
<ul>
<li><code class="gatsby-code-text">react-dom@16.1.0</code></li>
<li><code class="gatsby-code-text">react-dom@16.1.1</code></li>
</ul>
<p>Update to this version instead:</p>
<ul>
<li><code class="gatsby-code-text">react-dom@16.1.2</code> <strong>(contains the mitigation)</strong></li>
</ul>
<h3 id="162x"><a href="#162x" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>16.2.x</h3>
<p>If you’re using <code class="gatsby-code-text">react-dom/server</code> with this version:</p>
<ul>
<li><code class="gatsby-code-text">react-dom@16.2.0</code></li>
</ul>
<p>Update to this version instead:</p>
<ul>
<li><code class="gatsby-code-text">react-dom@16.2.1</code> <strong>(contains the mitigation)</strong></li>
</ul>
<h3 id="163x"><a href="#163x" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>16.3.x</h3>
<p>If you’re using <code class="gatsby-code-text">react-dom/server</code> with one of these versions:</p>
<ul>
<li><code class="gatsby-code-text">react-dom@16.3.0</code></li>
<li><code class="gatsby-code-text">react-dom@16.3.1</code></li>
<li><code class="gatsby-code-text">react-dom@16.3.2</code></li>
</ul>
<p>Update to this version instead:</p>
<ul>
<li><code class="gatsby-code-text">react-dom@16.3.3</code> <strong>(contains the mitigation)</strong></li>
</ul>
<h3 id="164x"><a href="#164x" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>16.4.x</h3>
<p>If you’re using <code class="gatsby-code-text">react-dom/server</code> with one of these versions:</p>
<ul>
<li><code class="gatsby-code-text">react-dom@16.4.0</code></li>
<li><code class="gatsby-code-text">react-dom@16.4.1</code></li>
</ul>
<p>Update to this version instead:</p>
<ul>
<li><code class="gatsby-code-text">react-dom@16.4.2</code> <strong>(contains the mitigation)</strong></li>
</ul>
<p>If you’re using a newer version of <code class="gatsby-code-text">react-dom</code>, no action is required.</p>
<p>Note that only the <code class="gatsby-code-text">react-dom</code> package needs to be updated.</p>
<h2 id="detailed-description"><a href="#detailed-description" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Detailed Description</h2>
<p>Your app might be affected by this vulnerability only if both of these two conditions are true:</p>
<ul>
<li>Your app is <strong>being rendered to HTML using <a href="/docs/react-dom-server.html">ReactDOMServer API</a></strong>, and</li>
<li>Your app <strong>includes a user-supplied attribute name in an HTML tag.</strong></li>
</ul>
<p>Specifically, the vulnerable pattern looks like this:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">let</span> props <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="gatsby-highlight-code-line">props<span class="token punctuation">[</span>userProvidedData<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">;</span>
</span><span class="token keyword">let</span> element <span class="token operator">=</span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token spread"><span class="token punctuation">{</span><span class="token punctuation">...</span><span class="token attr-value">props</span><span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span><span class="token punctuation">;</span>
<span class="token keyword">let</span> html <span class="token operator">=</span> ReactDOMServer<span class="token punctuation">.</span><span class="token function">renderToString</span><span class="token punctuation">(</span>element<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
      </div>
<p>In order to exploit it, the attacker would need to craft a special attribute name that triggers an <a href="https://en.wikipedia.org/wiki/Cross-site_scripting">XSS</a> vulnerability. For example:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">let</span> userProvidedData <span class="token operator">=</span> <span class="token string">'>&lt;/div>&lt;script>alert("hi")&lt;/script>'</span><span class="token punctuation">;</span></code></pre>
      </div>
<p>In the vulnerable versions of <code class="gatsby-code-text">react-dom/server</code>, the output would let the attacker inject arbitrary markup:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-html"><code class="gatsby-code-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">></span></span><span class="token script language-javascript"><span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"hi"</span><span class="token punctuation">)</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span></code></pre>
      </div>
<p>In the versions after the vulnerability was <a href="https://github.com/facebook/react/pull/13302">fixed</a> (and before it was introduced), attributes with invalid names are skipped:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-html"><code class="gatsby-code-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></code></pre>
      </div>
<p>You would also see a warning about an invalid attribute name.</p>
<p>Note that <strong>we expect attribute names based on user input to be very rare in practice.</strong> It doesn’t serve any common practical use case, and has other potential security implications that React can’t guard against.</p>
<h2 id="installation"><a href="#installation" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Installation</h2>
<p>React v16.4.2 is available on the npm registry.</p>
<p>To install React 16 with Yarn, run:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-bash"><code class="gatsby-code-bash">yarn add react@^16.4.2 react-dom@^16.4.2</code></pre>
      </div>
<p>To install React 16 with npm, run:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-bash"><code class="gatsby-code-bash"><span class="token function">npm</span> <span class="token function">install</span> --save react@^16.4.2 react-dom@^16.4.2</code></pre>
      </div>
<p>We also provide UMD builds of React via a CDN:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-html"><code class="gatsby-code-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">crossorigin</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>https://unpkg.com/react@16/umd/react.production.min.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script language-javascript"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">crossorigin</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>https://unpkg.com/react-dom@16/umd/react-dom.production.min.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script language-javascript"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span></code></pre>
      </div>
<p>Refer to the documentation for <a href="/docs/installation.html">detailed installation instructions</a>.</p>
<h2 id="changelog"><a href="#changelog" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Changelog</h2>
<h3 id="react-dom-server"><a href="#react-dom-server" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>React DOM Server</h3>
<ul>
<li>
<p>Fix a potential XSS vulnerability when the attacker controls an attribute name (<code class="gatsby-code-text">CVE-2018-6341</code>). This fix is available in the latest <code class="gatsby-code-text">react-dom@16.4.2</code>, as well as in previous affected minor versions: <code class="gatsby-code-text">react-dom@16.0.1</code>, <code class="gatsby-code-text">react-dom@16.1.2</code>, <code class="gatsby-code-text">react-dom@16.2.1</code>, and <code class="gatsby-code-text">react-dom@16.3.3</code>. (<a href="https://github.com/gaearon">@gaearon</a> in <a href="https://github.com/facebook/react/pull/13302">#13302</a>)</p>
</li>
<li>
<p>Fix a crash in the server renderer when an attribute is called <code class="gatsby-code-text">hasOwnProperty</code>. This fix is only available in <code class="gatsby-code-text">react-dom@16.4.2</code>. (<a href="https://github.com/gaearon">@gaearon</a> in <a href="https://github.com/facebook/react/pull/13303">#13303</a>)</p>
</li>
</ul>]]></description><link>https://reactjs.org/blog/2018/08/01/react-v-16-4-2.html</link><guid isPermaLink="false">https://reactjs.org/blog/2018/08/01/react-v-16-4-2.html</guid><pubDate>Wed, 01 Aug 2018 00:00:00 GMT</pubDate></item><item><title><![CDATA[You Probably Don't Need Derived State]]></title><description><![CDATA[<p>React 16.4 included a <a href="/blog/2018/05/23/react-v-16-4.html#bugfix-for-getderivedstatefromprops">bugfix for getDerivedStateFromProps</a> which caused some existing bugs in React components to reproduce more consistently. If this release exposed a case where your application was using an anti-pattern and didn’t work properly after the fix, we’re sorry for the churn. In this post, we will explain some common anti-patterns with derived state and our preferred alternatives.</p>
<p>For a long time, the lifecycle <code class="gatsby-code-text">componentWillReceiveProps</code> was the only way to update state in response to a change in props without an additional render. In version 16.3, <a href="/blog/2018/03/29/react-v-16-3.html#component-lifecycle-changes">we introduced a replacement lifecycle, <code class="gatsby-code-text">getDerivedStateFromProps</code></a> to solve the same use cases in a safer way. At the same time, we’ve realized that people have many misconceptions about how to use both methods, and we’ve found anti-patterns that result in subtle and confusing bugs. The <code class="gatsby-code-text">getDerivedStateFromProps</code> bugfix in 16.4 <a href="https://github.com/facebook/react/issues/12898">makes derived state more predictable</a>, so the results of misusing it are easier to notice.</p>
<blockquote>
<p>Note</p>
<p>All of the anti-patterns described in this post apply to both the older <code class="gatsby-code-text">componentWillReceiveProps</code> and the newer <code class="gatsby-code-text">getDerivedStateFromProps</code>.</p>
</blockquote>
<p> This blog post will cover the following topics:</p>
<ul>
<li><a href="#when-to-use-derived-state">When to use derived state</a></li>
<li>
<p><a href="#common-bugs-when-using-derived-state">Common bugs when using derived state</a></p>
<ul>
<li><a href="#anti-pattern-unconditionally-copying-props-to-state">Anti-pattern: Unconditionally copying props to state</a></li>
<li><a href="#anti-pattern-erasing-state-when-props-change">Anti-pattern: Erasing state when props change</a></li>
</ul>
</li>
<li><a href="#preferred-solutions">Preferred solutions</a></li>
<li><a href="#what-about-memoization">What about memoization?</a></li>
</ul>
<h2 id="when-to-use-derived-state"><a href="#when-to-use-derived-state" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>When to Use Derived State</h2>
<p><code class="gatsby-code-text">getDerivedStateFromProps</code> exists for only one purpose. It enables a component to update its internal state as the result of <strong>changes in props</strong>. Our previous blog post provided some examples, like <a href="/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props">recording the current scroll direction based on a changing offset prop</a> or <a href="/blog/2018/03/27/update-on-async-rendering.html#fetching-external-data-when-props-change">loading external data specified by a source prop</a>.</p>
<p>We did not provide many examples, because as a general rule, <strong>derived state should be used sparingly</strong>. All problems with derived state that we have seen can be ultimately reduced to either (1) unconditionally updating state from props or (2) updating state whenever props and state don’t match. (We’ll go over both in more detail below.)</p>
<ul>
<li>If you’re using derived state to memoize some computation based only on the current props, you don’t need derived state. See <a href="#what-about-memoization">What about memoization?</a> below.</li>
<li>If you’re updating derived state unconditionally or updating it whenever props and state don’t match, your component likely resets its state too frequently. Read on for more details.</li>
</ul>
<h2 id="common-bugs-when-using-derived-state"><a href="#common-bugs-when-using-derived-state" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Common Bugs When Using Derived State</h2>
<p>The terms <a href="/docs/forms.html#controlled-components">“controlled”</a> and <a href="/docs/uncontrolled-components.html">“uncontrolled”</a> usually refer to form inputs, but they can also describe where any component’s data lives. Data passed in as props can be thought of as <strong>controlled</strong> (because the parent component <em>controls</em> that data). Data that exists only in internal state can be thought of as <strong>uncontrolled</strong> (because the parent can’t directly change it).</p>
<p>The most common mistake with derived state is mixing these two; when a derived state value is also updated by <code class="gatsby-code-text">setState</code> calls, there isn’t a single source of truth for the data. The <a href="/blog/2018/03/27/update-on-async-rendering.html#fetching-external-data-when-props-change">external data loading example</a> mentioned above may sound similar, but it’s different in a few important ways. In the loading example, there is a clear source of truth for both the “source” prop and the “loading” state. When the source prop changes, the loading state should <strong>always</strong> be overridden. Conversely, the state is overridden only when the prop <strong>changes</strong> and is otherwise managed by the component.</p>
<p>Problems arise when any of these constraints are changed. This typically comes in two forms. Let’s take a look at both.</p>
<h3 id="anti-pattern-unconditionally-copying-props-to-state"><a href="#anti-pattern-unconditionally-copying-props-to-state" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Anti-pattern: Unconditionally copying props to state</h3>
<p>A common misconception is that <code class="gatsby-code-text">getDerivedStateFromProps</code> and <code class="gatsby-code-text">componentWillReceiveProps</code> are only called when props “change”. These lifecycles are called any time a parent component rerenders, regardless of whether the props are “different” from before. Because of this, it has always been unsafe to <em>unconditionally</em> override state using either of these lifecycles. <strong>Doing so will cause state updates to be lost.</strong></p>
<p>Let’s consider an example to demonstrate the problem. Here is a <code class="gatsby-code-text">EmailInput</code> component that “mirrors” an email prop in state:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">class</span> <span class="token class-name">EmailInput</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span>
  state <span class="token operator">=</span> <span class="token punctuation">{</span> email<span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>email <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">onChange</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>handleChange<span class="token punctuation">}</span></span> <span class="token attr-name">value</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>email<span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token function-variable function">handleChange</span> <span class="token operator">=</span> event <span class="token operator">=></span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span> email<span class="token punctuation">:</span> event<span class="token punctuation">.</span>target<span class="token punctuation">.</span>value <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token function">componentWillReceiveProps</span><span class="token punctuation">(</span>nextProps<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// This will erase any local state updates!</span>
    <span class="token comment">// Do not do this.</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span> email<span class="token punctuation">:</span> nextProps<span class="token punctuation">.</span>email <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>At first, this component might look okay. State is initialized to the value specified by props and updated when we type into the <code class="gatsby-code-text">&lt;input&gt;</code>. But if our component’s parent rerenders, anything we’ve typed into the <code class="gatsby-code-text">&lt;input&gt;</code> will be lost! (<a href="https://codesandbox.io/s/m3w9zn1z8x">See this demo for an example.</a>) This holds true even if we were to compare <code class="gatsby-code-text">nextProps.email !== this.state.email</code> before resetting.</p>
<p>In this simple example, adding <code class="gatsby-code-text">shouldComponentUpdate</code> to rerender only when the email prop has changed could fix this. However in practice, components usually accept multiple props; another prop changing would still cause a rerender and improper reset. Function and object props are also often created inline, making it hard to implement a <code class="gatsby-code-text">shouldComponentUpdate</code> that reliably returns true only when a material change has happened. <a href="https://codesandbox.io/s/jl0w6r9w59">Here is a demo that shows that happening.</a> As a result, <code class="gatsby-code-text">shouldComponentUpdate</code> is best used as a performance optimization, not to ensure correctness of derived state.</p>
<p>Hopefully it’s clear by now why <strong>it is a bad idea to unconditionally copy props to state</strong>. Before reviewing possible solutions, let’s look at a related problematic pattern: what if we were to only update the state when the email prop changes?</p>
<h3 id="anti-pattern-erasing-state-when-props-change"><a href="#anti-pattern-erasing-state-when-props-change" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Anti-pattern: Erasing state when props change</h3>
<p>Continuing the example above, we could avoid accidentally erasing state by only updating it when <code class="gatsby-code-text">props.email</code> changes:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">class</span> <span class="token class-name">EmailInput</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span>
  state <span class="token operator">=</span> <span class="token punctuation">{</span>
    email<span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>email
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token function">componentWillReceiveProps</span><span class="token punctuation">(</span>nextProps<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// Any time props.email changes, update state.</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>nextProps<span class="token punctuation">.</span>email <span class="token operator">!==</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>email<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
        email<span class="token punctuation">:</span> nextProps<span class="token punctuation">.</span>email
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  
  <span class="token comment">// ...</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<blockquote>
<p>Note</p>
<p>Even though the example above shows <code class="gatsby-code-text">componentWillReceiveProps</code>, the same anti-pattern applies to <code class="gatsby-code-text">getDerivedStateFromProps</code>.</p>
</blockquote>
<p>We’ve just made a big improvement. Now our component will erase what we’ve typed only when the props actually change.</p>
<p>There is still a subtle problem. Imagine a password manager app using the above input component. When navigating between details for two accounts with the same email, the input would fail to reset. This is because the prop value passed to the component would be the same for both accounts! This would be a surprise to the user, as an unsaved change to one account would appear to affect other accounts that happened to share the same email. (<a href="https://codesandbox.io/s/mz2lnkjkrx">See demo here.</a>)</p>
<p>This design is fundamentally flawed, but it’s also an easy mistake to make. (<a href="https://twitter.com/brian_d_vaughn/status/959600888242307072">I’ve made it myself!</a>) Fortunately there are two alternatives that work better. The key to both is that <strong>for any piece of data, you need to pick a single component that owns it as the source of truth, and avoid duplicating it in other components.</strong> Let’s take a look at each of the alternatives.</p>
<h2 id="preferred-solutions"><a href="#preferred-solutions" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Preferred Solutions</h2>
<h3 id="recommendation-fully-controlled-component"><a href="#recommendation-fully-controlled-component" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Recommendation: Fully controlled component</h3>
<p>One way to avoid the problems mentioned above is to remove state from our component entirely. If the email address only exists as a prop, then we don’t have to worry about conflicts with state. We could even convert <code class="gatsby-code-text">EmailInput</code> to a lighter-weight functional component:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">function</span> <span class="token function">EmailInput</span><span class="token punctuation">(</span>props<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">onChange</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>props<span class="token punctuation">.</span>onChange<span class="token punctuation">}</span></span> <span class="token attr-name">value</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>props<span class="token punctuation">.</span>email<span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>This approach simplifies the implementation of our component, but if we still want to store a draft value, the parent form component will now need to do that manually. (<a href="https://codesandbox.io/s/7154w1l551">Click here to see a demo of this pattern.</a>)</p>
<h3 id="recommendation-fully-uncontrolled-component-with-a-key"><a href="#recommendation-fully-uncontrolled-component-with-a-key" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Recommendation: Fully uncontrolled component with a <code class="gatsby-code-text">key</code></h3>
<p>Another alternative would be for our component to fully own the “draft” email state. In that case, our component could still accept a prop for the <em>initial</em> value, but it would ignore subsequent changes to that prop:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">class</span> <span class="token class-name">EmailInput</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span>
  state <span class="token operator">=</span> <span class="token punctuation">{</span> email<span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>defaultEmail <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token function-variable function">handleChange</span> <span class="token operator">=</span> event <span class="token operator">=></span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span> email<span class="token punctuation">:</span> event<span class="token punctuation">.</span>target<span class="token punctuation">.</span>value <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">onChange</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>handleChange<span class="token punctuation">}</span></span> <span class="token attr-name">value</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>email<span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>In order to reset the value when moving to a different item (as in our password manager scenario), we can use the special React attribute called <code class="gatsby-code-text">key</code>. When a <code class="gatsby-code-text">key</code> changes, React will <a href="/docs/reconciliation.html#keys"><em>create</em> a new component instance rather than <em>update</em> the current one</a>. Keys are usually used for dynamic lists but are also useful here. In our case, we could use the user ID to recreate the email input any time a new user is selected:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>EmailInput</span>
  <span class="token attr-name">defaultEmail</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>user<span class="token punctuation">.</span>email<span class="token punctuation">}</span></span>
  <span class="token attr-name">key</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>user<span class="token punctuation">.</span>id<span class="token punctuation">}</span></span>
<span class="token punctuation">/></span></span></code></pre>
      </div>
<p>Each time the ID changes, the <code class="gatsby-code-text">EmailInput</code> will be recreated and its state will be reset to the latest <code class="gatsby-code-text">defaultEmail</code> value. (<a href="https://codesandbox.io/s/6v1znlxyxn">Click here to see a demo of this pattern.</a>) With this approach, you don’t have to add <code class="gatsby-code-text">key</code> to every input. It might make more sense to put a <code class="gatsby-code-text">key</code> on the whole form instead. Every time the key changes, all components within the form will be recreated with a freshly initialized state.</p>
<p>In most cases, this is the best way to handle state that needs to be reset.</p>
<blockquote>
<p>Note</p>
<p>While this may sound slow, the performance difference is usually insignificant. Using a key can even be faster if the components have heavy logic that runs on updates since diffing gets bypassed for that subtree.</p>
</blockquote>
<h4 id="alternative-1-reset-uncontrolled-component-with-an-id-prop"><a href="#alternative-1-reset-uncontrolled-component-with-an-id-prop" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Alternative 1: Reset uncontrolled component with an ID prop</h4>
<p>If <code class="gatsby-code-text">key</code> doesn’t work for some reason (perhaps the component is very expensive to initialize), a workable but cumbersome solution would be to watch for changes to “userID” in <code class="gatsby-code-text">getDerivedStateFromProps</code>:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">class</span> <span class="token class-name">EmailInput</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span>
  state <span class="token operator">=</span> <span class="token punctuation">{</span>
    email<span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>defaultEmail<span class="token punctuation">,</span>
    prevPropsUserID<span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>userID
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token keyword">static</span> <span class="token function">getDerivedStateFromProps</span><span class="token punctuation">(</span>props<span class="token punctuation">,</span> state<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// Any time the current user changes,</span>
    <span class="token comment">// Reset any parts of state that are tied to that user.</span>
    <span class="token comment">// In this simple example, that's just the email.</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>props<span class="token punctuation">.</span>userID <span class="token operator">!==</span> state<span class="token punctuation">.</span>prevPropsUserID<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token punctuation">{</span>
        prevPropsUserID<span class="token punctuation">:</span> props<span class="token punctuation">.</span>userID<span class="token punctuation">,</span>
        email<span class="token punctuation">:</span> props<span class="token punctuation">.</span>defaultEmail
      <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token comment">// ...</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>This also provides the flexibility to only reset parts of our component’s internal state if we so choose. (<a href="https://codesandbox.io/s/rjyvp7l3rq">Click here to see a demo of this pattern.</a>)</p>
<blockquote>
<p>Note</p>
<p>Even though the example above shows <code class="gatsby-code-text">getDerivedStateFromProps</code>, the same technique can be used with <code class="gatsby-code-text">componentWillReceiveProps</code>.</p>
</blockquote>
<h4 id="alternative-2-reset-uncontrolled-component-with-an-instance-method"><a href="#alternative-2-reset-uncontrolled-component-with-an-instance-method" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Alternative 2: Reset uncontrolled component with an instance method</h4>
<p>More rarely, you may need to reset state even if there’s no appropriate ID to use as <code class="gatsby-code-text">key</code>. One solution is to reset the key to a random value or autoincrementing number each time you want to reset. One other viable alternative is to expose an instance method to imperatively reset the internal state:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">class</span> <span class="token class-name">EmailInput</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span>
  state <span class="token operator">=</span> <span class="token punctuation">{</span>
    email<span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>defaultEmail
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token function">resetEmailForNewUser</span><span class="token punctuation">(</span>newEmail<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span> email<span class="token punctuation">:</span> newEmail <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token comment">// ...</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>The parent form component could then <a href="/docs/glossary.html#refs">use a <code class="gatsby-code-text">ref</code> to call this method</a>. (<a href="https://codesandbox.io/s/l70krvpykl">Click here to see a demo of this pattern.</a>)</p>
<p>Refs can be useful in certain cases like this one, but generally we recommend you use them sparingly. Even in the demo, this imperative method is nonideal because two renders will occur instead of one.</p>
<hr>
<h3 id="recap"><a href="#recap" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Recap</h3>
<p>To recap, when designing a component, it is important to decide whether its data will be controlled or uncontrolled.</p>
<p>Instead of trying to <strong>“mirror” a prop value in state</strong>, make the component <strong>controlled</strong>, and consolidate the two diverging values in the state of some parent component. For example, rather than a child accepting a “committed” <code class="gatsby-code-text">props.value</code> and tracking a “draft” <code class="gatsby-code-text">state.value</code>, have the parent manage both <code class="gatsby-code-text">state.draftValue</code> and <code class="gatsby-code-text">state.committedValue</code> and control the child’s value directly. This makes the data flow more explicit and predictable.</p>
<p>For <strong>uncontrolled</strong> components, if you’re trying to reset state when a particular prop (usually an ID) changes, you have a few options:</p>
<ul>
<li><strong>Recomendation: To reset <em>all internal state</em>, use the <code class="gatsby-code-text">key</code> attribute.</strong></li>
<li>Alternative 1: To reset <em>only certain state fields</em>, watch for changes in a special property (e.g. <code class="gatsby-code-text">props.userID</code>).</li>
<li>Alternative 2: You can also consider fall back to an imperative instance method using refs.</li>
</ul>
<h2 id="what-about-memoization"><a href="#what-about-memoization" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>What about memoization?</h2>
<p>We’ve also seen derived state used to ensure an expensive value used in <code class="gatsby-code-text">render</code> is recomputed only when the inputs change. This technique is known as <a href="https://en.wikipedia.org/wiki/Memoization">memoization</a>.</p>
<p>Using derived state for memoization isn’t necessarily bad, but it’s usually not the best solution. There is inherent complexity in managing derived state, and this complexity increases with each additional property. For example, if we add a second derived field to our component state then our implementation would need to separately track changes to both.</p>
<p>Let’s look at an example of one component that takes one prop—a list of items—and renders the items that match a search query entered by the user. We could use derived state to store the filtered list:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">class</span> <span class="token class-name">Example</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span>
  state <span class="token operator">=</span> <span class="token punctuation">{</span>
    filterText<span class="token punctuation">:</span> <span class="token string">""</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token comment">// *******************************************************</span>
  <span class="token comment">// NOTE: this example is NOT the recommended approach.</span>
  <span class="token comment">// See the examples below for our recommendations instead.</span>
  <span class="token comment">// *******************************************************</span>

  <span class="token keyword">static</span> <span class="token function">getDerivedStateFromProps</span><span class="token punctuation">(</span>props<span class="token punctuation">,</span> state<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// Re-run the filter whenever the list array or filter text change.</span>
    <span class="token comment">// Note we need to store prevPropsList and prevFilterText to detect changes.</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>
      props<span class="token punctuation">.</span>list <span class="token operator">!==</span> state<span class="token punctuation">.</span>prevPropsList <span class="token operator">||</span>
      state<span class="token punctuation">.</span>prevFilterText <span class="token operator">!==</span> state<span class="token punctuation">.</span>filterText
    <span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token punctuation">{</span>
        prevPropsList<span class="token punctuation">:</span> props<span class="token punctuation">.</span>list<span class="token punctuation">,</span>
        prevFilterText<span class="token punctuation">:</span> state<span class="token punctuation">.</span>filterText<span class="token punctuation">,</span>
        filteredList<span class="token punctuation">:</span> props<span class="token punctuation">.</span>list<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>item <span class="token operator">=></span> item<span class="token punctuation">.</span>text<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>state<span class="token punctuation">.</span>filterText<span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token function-variable function">handleChange</span> <span class="token operator">=</span> event <span class="token operator">=></span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span> filterText<span class="token punctuation">:</span> event<span class="token punctuation">.</span>target<span class="token punctuation">.</span>value <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Fragment</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">onChange</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>handleChange<span class="token punctuation">}</span></span> <span class="token attr-name">value</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>filterText<span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span><span class="token punctuation">></span></span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>filteredList<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>item <span class="token operator">=></span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">key</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>item<span class="token punctuation">.</span>id<span class="token punctuation">}</span></span><span class="token punctuation">></span></span><span class="token punctuation">{</span>item<span class="token punctuation">.</span>text<span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>Fragment</span><span class="token punctuation">></span></span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>This implementation avoids recalculating <code class="gatsby-code-text">filteredList</code> more often than necessary. But it is more complicated than it needs to be, because it has to separately track and detect changes in both props and state in order to properly update the filtered list. In this example, we could simplify things by using <code class="gatsby-code-text">PureComponent</code> and moving the filter operation into the render method: </p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token comment">// PureComponents only rerender if at least one state or prop value changes.</span>
<span class="token comment">// Change is determined by doing a shallow comparison of state and prop keys.</span>
<span class="token keyword">class</span> <span class="token class-name">Example</span> <span class="token keyword">extends</span> <span class="token class-name">PureComponent</span> <span class="token punctuation">{</span>
  <span class="token comment">// State only needs to hold the current filter text value:</span>
  state <span class="token operator">=</span> <span class="token punctuation">{</span>
    filterText<span class="token punctuation">:</span> <span class="token string">""</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token function-variable function">handleChange</span> <span class="token operator">=</span> event <span class="token operator">=></span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span> filterText<span class="token punctuation">:</span> event<span class="token punctuation">.</span>target<span class="token punctuation">.</span>value <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// The render method on this PureComponent is called only if</span>
    <span class="token comment">// props.list or state.filterText has changed.</span>
    <span class="token keyword">const</span> filteredList <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>list<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>
      item <span class="token operator">=></span> item<span class="token punctuation">.</span>text<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>filterText<span class="token punctuation">)</span>
    <span class="token punctuation">)</span>

    <span class="token keyword">return</span> <span class="token punctuation">(</span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Fragment</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">onChange</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>handleChange<span class="token punctuation">}</span></span> <span class="token attr-name">value</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>filterText<span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span><span class="token punctuation">></span></span><span class="token punctuation">{</span>filteredList<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>item <span class="token operator">=></span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">key</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>item<span class="token punctuation">.</span>id<span class="token punctuation">}</span></span><span class="token punctuation">></span></span><span class="token punctuation">{</span>item<span class="token punctuation">.</span>text<span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>Fragment</span><span class="token punctuation">></span></span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>The above approach is much cleaner and simpler than the derived state version. Occasionally, this won’t be good enough—filtering may be slow for large lists, and <code class="gatsby-code-text">PureComponent</code> won’t prevent rerenders if another prop were to change. To address both of these concerns, we could add a memoization helper to avoid unnecessarily re-filtering our list:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">import</span> memoize <span class="token keyword">from</span> <span class="token string">"memoize-one"</span><span class="token punctuation">;</span>

<span class="token keyword">class</span> <span class="token class-name">Example</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span>
  <span class="token comment">// State only needs to hold the current filter text value:</span>
  state <span class="token operator">=</span> <span class="token punctuation">{</span> filterText<span class="token punctuation">:</span> <span class="token string">""</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token comment">// Re-run the filter whenever the list array or filter text changes:</span>
  filter <span class="token operator">=</span> <span class="token function">memoize</span><span class="token punctuation">(</span>
    <span class="token punctuation">(</span>list<span class="token punctuation">,</span> filterText<span class="token punctuation">)</span> <span class="token operator">=></span> list<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>item <span class="token operator">=></span> item<span class="token punctuation">.</span>text<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>filterText<span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token function-variable function">handleChange</span> <span class="token operator">=</span> event <span class="token operator">=></span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span> filterText<span class="token punctuation">:</span> event<span class="token punctuation">.</span>target<span class="token punctuation">.</span>value <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// Calculate the latest filtered list. If these arguments haven't changed</span>
    <span class="token comment">// since the last render, `memoize-one` will reuse the last return value.</span>
    <span class="token keyword">const</span> filteredList <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>list<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>filterText<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token punctuation">(</span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Fragment</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">onChange</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>handleChange<span class="token punctuation">}</span></span> <span class="token attr-name">value</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>filterText<span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span><span class="token punctuation">></span></span><span class="token punctuation">{</span>filteredList<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>item <span class="token operator">=></span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">key</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>item<span class="token punctuation">.</span>id<span class="token punctuation">}</span></span><span class="token punctuation">></span></span><span class="token punctuation">{</span>item<span class="token punctuation">.</span>text<span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>Fragment</span><span class="token punctuation">></span></span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>This is much simpler and performs just as well as the derived state version!</p>
<p>When using memoization, remember a couple of constraints:</p>
<ol>
<li>In most cases, you’ll want to <strong>attach the memoized function to a component instance</strong>. This prevents multiple instances of a component from resetting each other’s memoized keys.</li>
<li>Typically you’ll want to use a memoization helper with a <strong>limited cache size</strong> in order to prevent memory leaks over time. (In the example above, we used <code class="gatsby-code-text">memoize-one</code> because it only caches the most recent arguments and result.)</li>
<li>None of the implementations shown in this section will work if <code class="gatsby-code-text">props.list</code> is recreated each time the parent component renders. But in most cases, this setup is appropriate.</li>
</ol>
<h2 id="in-closing"><a href="#in-closing" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>In closing</h2>
<p>In real world applications, components often contain a mix of controlled and uncontrolled behaviors. This is okay! If each value has a clear source of truth, you can avoid the anti-patterns mentioned above.</p>
<p>It is also worth re-iterating that <code class="gatsby-code-text">getDerivedStateFromProps</code> (and derived state in general) is an advanced feature and should be used sparingly because of this complexity. If your use case falls outside of these patterns, please share it with us on <a href="https://github.com/reactjs/reactjs.org/issues/new">GitHub</a> or <a href="https://twitter.com/reactjs">Twitter</a>!</p>]]></description><link>https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html</link><guid isPermaLink="false">https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html</guid><pubDate>Thu, 07 Jun 2018 00:00:00 GMT</pubDate></item><item><title><![CDATA[React v16.4.0: Pointer Events]]></title><description><![CDATA[<p>The latest minor release adds support for an oft-requested feature: pointer events!</p>
<p>It also includes a bugfix for <code class="gatsby-code-text">getDerivedStateFromProps</code>. Check out the full <a href="#changelog">changelog</a> below.</p>
<h2 id="pointer-events"><a href="#pointer-events" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Pointer Events</h2>
<p>The following event types are now available in React DOM:</p>
<ul>
<li><code class="gatsby-code-text">onPointerDown</code></li>
<li><code class="gatsby-code-text">onPointerMove</code></li>
<li><code class="gatsby-code-text">onPointerUp</code></li>
<li><code class="gatsby-code-text">onPointerCancel</code></li>
<li><code class="gatsby-code-text">onGotPointerCapture</code></li>
<li><code class="gatsby-code-text">onLostPointerCapture</code></li>
<li><code class="gatsby-code-text">onPointerEnter</code></li>
<li><code class="gatsby-code-text">onPointerLeave</code></li>
<li><code class="gatsby-code-text">onPointerOver</code></li>
<li><code class="gatsby-code-text">onPointerOut</code></li>
</ul>
<p>Please note that these events will only work in browsers that support the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Pointer_events">Pointer Events</a> specification. (At the time of this writing, this includes the latest versions of Chrome, Firefox, Edge, and Internet Explorer.) If your application depends on pointer events, we recommend using a third-party pointer events polyfill. We have opted not to include such a polyfill in React DOM, to avoid an increase in bundle size.</p>
<p><a href="https://codesandbox.io/api/v1/sandboxes/define?parameters=N4IgZglgNgpgziAXKADgQwMYGs0HMYB0AVnAPYB2SoGFALjObVSACYwoNvkYTzMBOMTE0QgoaenCYAaEIOEBaFqQC2SMRPhMAvrtkRybAB7EEyEDUYMRICCpSl-tAAQAlIRhdh-q5wHJ5Tz8AbgAdcjsHJzcPWgARAHkAWWdvXwDYpVUQ8PDLKWcAYQBJV0KAGQBRAH0AZWKALUrnAF5nAA4AVjDyPPE4OGc4_jwAIVIjZxgjekNB92ECQtUHcmtnYHDnZylNVo2t7ecACzQ4QrQUWgBXQURUtCg4GGlD7Z5-DFhymDBae_aAAZXuQjs4Pl8YAAVUgoAHAw7aHrbCBwYZ4XAGXD7MCPZ7I5woQQANwgpGucB-f32gIJRJgpPJcBhKBpPUOFDipAA7qC2gz1i0AHwHUFHWjHVEEVHo3CY8jYtq0fjXGAE7YCxgEWhofj4WgEZ60AAKpAM9H4FyutxgAApNQaHOaYPxiiwAJTssXbAD0PucAHUYDtaI5gxLgwYILQII9waRHCwDJpBqHnAAjYNodOwZxpjCPDDXcT0PPHYMYU4K-BvZx-5wll3OCgEWsSqXTZXCU1waNk8hxGBQHX24nWT2Ir3bChJUhj_YO1oizbe5wQMDOW0AQnbcGlaJGcqx7tFYO2ghu_HI6uc2lr-RcwFgf2kedh2n2u4InZGnh7fc5IcRwdCdelXL8jVqHV6FtW1gAhb5floV8EOhd8T2FTcVzPcEIE-RC_nuVCqRcABqBskJBHDUJZIi8MhFlnHI0MUCoo5tHdUDtiRXIxQoABVVl-THRgl03L8ZUPeVFQeJ4YC45tyAAcVIWgrUvYNhMFEUIJgWgoM0ODTnOS4NPuZVVQ4gkKHKUgpHUm0FxElxhTbSU90g6C7WAYyHLuWTnis3iNRmX8TTsgCByAtAnO008jgfCjqS0rV0HwAANG9EpY2LUrwGAAE0sooAo2GHGK2mws9n3-JKXAUMspXpRkKRItiwRY8zYWcBqv2askKRZdrbxvPqSQGykkP2GrRvcgh-qZRilVhG8L1uUEyp1AkeLA89OBdW0Tyq94SsfXzTJtFD6II5DcPwtCUA_JU5t2egpwS06MwmKCAE9c0q2ttnTRMXXuPwAEYUEmMgoAgFhnAAYhYABOFGUb8YbthUXV5TB8HASh5xAWcAAmAmjAxwHnBUAwAAkYAgXBjlqgAWQEEVXbZuThiU8fZgBSSnOcJCKYwoMHBBLCAxyFsEdvvT7UN-_74rBbmWF5opSgqGp6iaTGTgZpnapKMoqjqRpKgN4H-DYfhXDQJMKXuU2dYtpo61Jg2HF7MXyDB7MYeuehZeqpC6Putqqc6u6GNha3MCwXAfGuQxligRx7nO61BGcAB-fwc1VPxnDB5OYAYUOOvJSsAEFPH7MHyAoGAq5G4KjjWq9NypgAeJNiRDP6YBaYBgaMZWYG0IUqe2fvpdno4pGH0eldoYe72FsEKFNZ1-C5XlR6_TkeXITecO38hd6sfhZzHI-5pnOcp8X6cr7NG_BIfqUBMe1_FOvhaC43Ahzfz3L_c-F834qXCnvPyI9gDH2UqpeBkCoE2TsrAm-8CwEEAwfZC6gg0Fnh9DPYWvcfQDzIWCBSm9N7hAWJ4RISQCCCEMAdQ4vdZTjEmKQtiygiwqGsAQfUlRYBCMYKMH6bpbQBATLQPw7pwigRANofQ7CTDMxUFAZglhZg2HnoPOGLRQhyHkaYoUFCqGqN0EAA" target="_blank" rel="noreferrer">Check out this example on CodeSandbox.</a></p>
<p>Huge thanks to <a href="https://github.com/philipp-spiess">Philipp Spiess</a> for contributing this change!</p>
<h2 id="bugfix-for-getderivedstatefromprops"><a href="#bugfix-for-getderivedstatefromprops" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Bugfix for <code class="gatsby-code-text">getDerivedStateFromProps</code></h2>
<p><code class="gatsby-code-text">getDerivedStateFromProps</code> is now called every time a component is rendered, regardless of the cause of the update. Previously, it was only called if the component was re-rendered by its parent, and would not fire as the result of a local <code class="gatsby-code-text">setState</code>. This was an oversight in the initial implementation that has now been corrected. The previous behavior was more similar to <code class="gatsby-code-text">componentWillReceiveProps</code>, but the improved behavior ensures compatibility with React’s upcoming asynchronous rendering mode.</p>
<p><strong>This bug fix will not affect most apps</strong>, but it may cause issues with a small fraction of components. The rare cases where it does matter fall into one of two categories:</p>
<h3 id="1-avoid-side-effects-in-getderivedstatefromprops"><a href="#1-avoid-side-effects-in-getderivedstatefromprops" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>1. Avoid Side Effects in <code class="gatsby-code-text">getDerivedStateFromProps</code></h3>
<p>Like the render method, <code class="gatsby-code-text">getDerivedStateFromProps</code> should be a pure function of props and state. Side effects in <code class="gatsby-code-text">getDerivedStateFromProps</code> were never supported, but since it now fires more often than it used to, the recent change may expose previously undiscovered bugs.</p>
<p>Side effectful code should be moved to other methods: for example, Flux dispatches typically belong inside the originating event handler, and manual DOM mutations belong inside componentDidMount or componentDidUpdate. You can read more about this in our recent post about <a href="/blog/2018/03/27/update-on-async-rendering.html">preparing for asynchronous rendering</a>.</p>
<h3 id="2-compare-incoming-props-to-previous-props-when-computing-controlled-values"><a href="#2-compare-incoming-props-to-previous-props-when-computing-controlled-values" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>2. Compare Incoming Props to Previous Props When Computing Controlled Values</h3>
<p>The following code assumes <code class="gatsby-code-text">getDerivedStateFromProps</code> only fires on prop changes:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">static</span> <span class="token function">getDerivedStateFromProps</span><span class="token punctuation">(</span>props<span class="token punctuation">,</span> state<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>props<span class="token punctuation">.</span>value <span class="token operator">!==</span> state<span class="token punctuation">.</span>controlledValue<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">{</span>
      <span class="token comment">// Since this method fires on both props and state changes, local updates</span>
      <span class="token comment">// to the controlled value will be ignored, because the props version</span>
      <span class="token comment">// always overrides it. Oops!</span>
      controlledValue<span class="token punctuation">:</span> props<span class="token punctuation">.</span>value<span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>One possible way to fix this is to compare the incoming value to the previous value by storing the previous props in state:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">static</span> <span class="token function">getDerivedStateFromProps</span><span class="token punctuation">(</span>props<span class="token punctuation">,</span> state<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> prevProps <span class="token operator">=</span> state<span class="token punctuation">.</span>prevProps<span class="token punctuation">;</span>
  <span class="token comment">// Compare the incoming prop to previous prop</span>
  <span class="token keyword">const</span> controlledValue <span class="token operator">=</span>
    prevProps<span class="token punctuation">.</span>value <span class="token operator">!==</span> props<span class="token punctuation">.</span>value
      <span class="token operator">?</span> props<span class="token punctuation">.</span>value
      <span class="token punctuation">:</span> state<span class="token punctuation">.</span>controlledValue<span class="token punctuation">;</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    <span class="token comment">// Store the previous props in state</span>
    prevProps<span class="token punctuation">:</span> props<span class="token punctuation">,</span>
    controlledValue<span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>However, <strong>code that “mirrors” props in state usually contains bugs</strong>, whether you use the newer <code class="gatsby-code-text">getDerivedStateFromProps</code> or the legacy <code class="gatsby-code-text">componentWillReceiveProps</code>. We published a follow-up blog post that explains these problems in more detail, and suggests <a href="/blog/2018/06/07/you-probably-dont-need-derived-state.html">simpler solutions that don’t involve <code class="gatsby-code-text">getDerivedStateFromProps()</code></a>.</p>
<h2 id="installation"><a href="#installation" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Installation</h2>
<p>React v16.4.0 is available on the npm registry.</p>
<p>To install React 16 with Yarn, run:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-bash"><code class="gatsby-code-bash">yarn add react@^16.4.0 react-dom@^16.4.0</code></pre>
      </div>
<p>To install React 16 with npm, run:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-bash"><code class="gatsby-code-bash"><span class="token function">npm</span> <span class="token function">install</span> --save react@^16.4.0 react-dom@^16.4.0</code></pre>
      </div>
<p>We also provide UMD builds of React via a CDN:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-html"><code class="gatsby-code-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">crossorigin</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>https://unpkg.com/react@16/umd/react.production.min.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script language-javascript"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">crossorigin</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>https://unpkg.com/react-dom@16/umd/react-dom.production.min.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script language-javascript"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span></code></pre>
      </div>
<p>Refer to the documentation for <a href="/docs/installation.html">detailed installation instructions</a>.</p>
<h2 id="changelog"><a href="#changelog" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Changelog</h2>
<h3 id="react"><a href="#react" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>React</h3>
<ul>
<li>Add a new <a href="https://github.com/reactjs/rfcs/pull/51">experimental</a> <code class="gatsby-code-text">React.unstable_Profiler</code> component for measuring performance. (<a href="https://github.com/bvaughn">@bvaughn</a> in <a href="https://github.com/facebook/react/pull/12745">#12745</a>)</li>
</ul>
<h3 id="react-dom"><a href="#react-dom" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>React DOM</h3>
<ul>
<li>Add support for the Pointer Events specification. (<a href="https://github.com/philipp-spiess">@philipp-spiess</a> in <a href="https://github.com/facebook/react/pull/12507">#12507</a>)</li>
<li>Properly call <code class="gatsby-code-text">getDerivedStateFromProps()</code> regardless of the reason for re-rendering. (<a href="https://github.com/acdlite">@acdlite</a> in <a href="https://github.com/facebook/react/pull/12600">#12600</a> and <a href="https://github.com/facebook/react/pull/12802">#12802</a>)</li>
<li>Fix a bug that prevented context propagation in some cases. (<a href="https://github.com/gaearon">@gaearon</a> in <a href="https://github.com/facebook/react/pull/12708">#12708</a>)</li>
<li>Fix re-rendering of components using <code class="gatsby-code-text">forwardRef()</code> on a deeper <code class="gatsby-code-text">setState()</code>. (<a href="https://github.com/gaearon">@gaearon</a> in <a href="https://github.com/facebook/react/pull/12690">#12690</a>)</li>
<li>Fix some attributes incorrectly getting removed from custom element nodes. (<a href="https://github.com/airamrguez">@airamrguez</a> in <a href="https://github.com/facebook/react/pull/12702">#12702</a>)</li>
<li>Fix context providers to not bail out on children if there’s a legacy context provider above. (<a href="https://github.com/gaearon">@gaearon</a> in <a href="https://github.com/facebook/react/pull/12586">#12586</a>)</li>
<li>Add the ability to specify <code class="gatsby-code-text">propTypes</code> on a context provider component. (<a href="https://github.com/nicolevy">@nicolevy</a> in <a href="https://github.com/facebook/react/pull/12658">#12658</a>)</li>
<li>Fix a false positive warning when using <code class="gatsby-code-text">react-lifecycles-compat</code> in <code class="gatsby-code-text">&lt;StrictMode&gt;</code>. (<a href="https://github.com/bvaughn">@bvaughn</a> in <a href="https://github.com/facebook/react/pull/12644">#12644</a>)</li>
<li>Warn when the <code class="gatsby-code-text">forwardRef()</code> render function has <code class="gatsby-code-text">propTypes</code> or <code class="gatsby-code-text">defaultProps</code>. (<a href="https://github.com/bvaughn">@bvaughn</a> in <a href="https://github.com/facebook/react/pull/12644">#12644</a>)</li>
<li>Improve how <code class="gatsby-code-text">forwardRef()</code> and context consumers are displayed in the component stack. (<a href="https://github.com/sophiebits">@sophiebits</a> in <a href="https://github.com/facebook/react/pull/12777">#12777</a>)</li>
<li>Change internal event names. This can break third-party packages that rely on React internals in unsupported ways. (<a href="https://github.com/philipp-spiess">@philipp-spiess</a> in <a href="https://github.com/facebook/react/pull/12629">#12629</a>)</li>
</ul>
<h3 id="react-test-renderer"><a href="#react-test-renderer" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>React Test Renderer</h3>
<ul>
<li>Fix the <code class="gatsby-code-text">getDerivedStateFromProps()</code> support to match the new React DOM behavior. (<a href="https://github.com/koba04">@koba04</a> in <a href="https://github.com/facebook/react/pull/12676">#12676</a>)</li>
<li>Fix a <code class="gatsby-code-text">testInstance.parent</code> crash when the parent is a fragment or another special node. (<a href="https://github.com/gaearon">@gaearon</a> in <a href="https://github.com/facebook/react/pull/12813">#12813</a>)</li>
<li><code class="gatsby-code-text">forwardRef()</code> components are now discoverable by the test renderer traversal methods. (<a href="https://github.com/gaearon">@gaearon</a> in <a href="https://github.com/facebook/react/pull/12725">#12725</a>)</li>
<li>Shallow renderer now ignores <code class="gatsby-code-text">setState()</code> updaters that return <code class="gatsby-code-text">null</code> or <code class="gatsby-code-text">undefined</code>. (<a href="https://github.com/koba04">@koba04</a> in <a href="https://github.com/facebook/react/pull/12756">#12756</a>)</li>
</ul>
<h3 id="react-art"><a href="#react-art" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>React ART</h3>
<ul>
<li>Fix reading context provided from the tree managed by React DOM. (<a href="https://github.com/acdlite">@acdlite</a> in <a href="https://github.com/facebook/react/pull/12779">#12779</a>)</li>
</ul>
<h3 id="react-call-return-experimental"><a href="#react-call-return-experimental" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>React Call Return (Experimental)</h3>
<ul>
<li>This experiment was deleted because it was affecting the bundle size and the API wasn’t good enough. It’s likely to come back in the future in some other form. (<a href="https://github.com/gaearon">@gaearon</a> in <a href="https://github.com/facebook/react/pull/12820">#12820</a>)</li>
</ul>
<h3 id="react-reconciler-experimental"><a href="#react-reconciler-experimental" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>React Reconciler (Experimental)</h3>
<ul>
<li>The <a href="https://github.com/facebook/react/blob/c601f7a64640290af85c9f0e33c78480656b46bc/packages/react-noop-renderer/src/createReactNoop.js#L82-L285">new host config shape</a> is flat and doesn’t use nested objects. (<a href="https://github.com/gaearon">@gaearon</a> in <a href="https://github.com/facebook/react/pull/12792">#12792</a>)</li>
</ul>]]></description><link>https://reactjs.org/blog/2018/05/23/react-v-16-4.html</link><guid isPermaLink="false">https://reactjs.org/blog/2018/05/23/react-v-16-4.html</guid><pubDate>Wed, 23 May 2018 00:00:00 GMT</pubDate></item><item><title><![CDATA[React v16.3.0: New lifecycles and context API]]></title><description><![CDATA[<p>A few days ago, we <a href="/blog/2018/03/27/update-on-async-rendering.html">wrote a post about upcoming changes to our legacy lifecycle methods</a>, including gradual migration strategies. In React 16.3.0, we are adding a few new lifecycle methods to assist with that migration. We are also introducing new APIs for long requested features: an official context API, a ref forwarding API, and an ergonomic ref API.</p>
<p>Read on to learn more about the release.</p>
<h2 id="official-context-api"><a href="#official-context-api" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Official Context API</h2>
<p>For many years, React has offered an experimental API for context. Although it was a powerful tool, its use was discouraged because of inherent problems in the API, and because we always intended to replace the experimental API with a better one.</p>
<p>Version 16.3 introduces a new context API that is more efficient and supports both static type checking and deep updates.</p>
<blockquote>
<p><strong>Note</strong></p>
<p>The old context API will keep working for all React 16.x releases, so you will have time to migrate.</p>
</blockquote>
<p>Here is an example illustrating how you might inject a “theme” using the new context API:
<div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="gatsby-highlight-code-line"><span class="token keyword">const</span> ThemeContext <span class="token operator">=</span> React<span class="token punctuation">.</span><span class="token function">createContext</span><span class="token punctuation">(</span><span class="token string">'light'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span>
<span class="token keyword">class</span> <span class="token class-name">ThemeProvider</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
  state <span class="token operator">=</span> <span class="token punctuation">{</span>theme<span class="token punctuation">:</span> <span class="token string">'light'</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span>
<span class="gatsby-highlight-code-line">      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ThemeContext.Provider</span> <span class="token attr-name">value</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>theme<span class="token punctuation">}</span></span><span class="token punctuation">></span></span>
</span><span class="gatsby-highlight-code-line">        <span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>children<span class="token punctuation">}</span>
</span><span class="gatsby-highlight-code-line">      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ThemeContext.Provider</span><span class="token punctuation">></span></span>
</span>    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">ThemedButton</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span>
<span class="gatsby-highlight-code-line">      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ThemeContext.Consumer</span><span class="token punctuation">></span></span>
</span><span class="gatsby-highlight-code-line">        <span class="token punctuation">{</span>theme <span class="token operator">=></span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Button</span> <span class="token attr-name">theme</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>theme<span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span><span class="token punctuation">}</span>
</span><span class="gatsby-highlight-code-line">      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ThemeContext.Consumer</span><span class="token punctuation">></span></span>
</span>    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
        </div></p>
<p><a href="/docs/context.html">Learn more about the new context API here.</a></p>
<h2 id="createref-api"><a href="#createref-api" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a><code class="gatsby-code-text">createRef</code> API</h2>
<p>Previously, React provided two ways of managing refs: the legacy string ref API and the callback API. Although the string ref API was the more convenient of the two, it had <a href="https://github.com/facebook/react/issues/1373">several downsides</a> and so our official recommendation was to use the callback form instead.</p>
<p>Version 16.3 adds a new option for managing refs that offers the convenience of a string ref without any of the downsides:
<div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token keyword">class</span> <span class="token class-name">MyComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
  <span class="token function">constructor</span><span class="token punctuation">(</span>props<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">super</span><span class="token punctuation">(</span>props<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="gatsby-highlight-code-line">    <span class="token keyword">this</span><span class="token punctuation">.</span>inputRef <span class="token operator">=</span> React<span class="token punctuation">.</span><span class="token function">createRef</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span>  <span class="token punctuation">}</span>

  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="gatsby-highlight-code-line">    <span class="token keyword">return</span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>text<span class="token punctuation">"</span></span> <span class="token attr-name">ref</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>inputRef<span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span><span class="token punctuation">;</span>
</span>  <span class="token punctuation">}</span>

  <span class="token function">componentDidMount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="gatsby-highlight-code-line">    <span class="token keyword">this</span><span class="token punctuation">.</span>inputRef<span class="token punctuation">.</span>current<span class="token punctuation">.</span><span class="token function">focus</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span>  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
        </div></p>
<blockquote>
<p><strong>Note:</strong></p>
<p>Callback refs will continue to be supported in addition to the new <code class="gatsby-code-text">createRef</code> API.</p>
<p>You don’t need to replace callback refs in your components. They are slightly more flexible, so they will remain as an advanced feature.</p>
</blockquote>
<p><a href="/docs/refs-and-the-dom.html">Learn more about the new <code class="gatsby-code-text">createRef</code> API here.</a></p>
<h2 id="forwardref-api"><a href="#forwardref-api" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a><code class="gatsby-code-text">forwardRef</code> API</h2>
<p>Generally, React components are declarative, but sometimes imperative access to the component instances and the underlying DOM nodes is necessary. This is common for use cases like managing focus, selection, or animations. React provides <a href="/docs/refs-and-the-dom.html">refs</a> as a way to solve this problem. However, component encapsulation poses some challenges with refs.</p>
<p>For example, if you replace a <code class="gatsby-code-text">&lt;button&gt;</code> with a custom <code class="gatsby-code-text">&lt;FancyButton&gt;</code> component, the <code class="gatsby-code-text">ref</code> attribute on it will start pointing at the wrapper component instead of the DOM node (and would be <code class="gatsby-code-text">null</code> for functional components). While this is desirable for “application-level” components like <code class="gatsby-code-text">FeedStory</code> or <code class="gatsby-code-text">Comment</code> that need to be encapsulated, it can be annoying for “leaf” components such as <code class="gatsby-code-text">FancyButton</code> or <code class="gatsby-code-text">MyTextInput</code> that are typically used like their DOM counterparts, and might need to expose their DOM nodes.</p>
<p>Ref forwarding is a new opt-in feature that lets some components take a <code class="gatsby-code-text">ref</code> they receive, and pass it further down (in other words, “forward” it) to a child. In the example below, <code class="gatsby-code-text">FancyButton</code> forwards its ref to a DOM <code class="gatsby-code-text">button</code> that it renders:</p>
<p><div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="gatsby-highlight-code-line"><span class="token keyword">const</span> FancyButton <span class="token operator">=</span> React<span class="token punctuation">.</span><span class="token function">forwardRef</span><span class="token punctuation">(</span><span class="token punctuation">(</span>props<span class="token punctuation">,</span> ref<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>
</span><span class="gatsby-highlight-code-line">  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">ref</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>ref<span class="token punctuation">}</span></span> <span class="token attr-name">className</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>FancyButton<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
</span>    <span class="token punctuation">{</span>props<span class="token punctuation">.</span>children<span class="token punctuation">}</span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// You can now get a ref directly to the DOM button:</span>
<span class="token keyword">const</span> ref <span class="token operator">=</span> React<span class="token punctuation">.</span><span class="token function">createRef</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>FancyButton</span> <span class="token attr-name">ref</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>ref<span class="token punctuation">}</span></span><span class="token punctuation">></span></span>Click me<span class="token operator">!</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>FancyButton</span><span class="token punctuation">></span></span><span class="token punctuation">;</span></code></pre>
        </div></p>
<p>This way, components using <code class="gatsby-code-text">FancyButton</code> can get a ref to the underlying <code class="gatsby-code-text">button</code> DOM node and access it if necessary—just like if they used a DOM <code class="gatsby-code-text">button</code> directly.</p>
<p>Ref forwarding is not limited to “leaf” components that render DOM nodes. If you write <a href="/docs/higher-order-components.html">higher order components</a>, we recommend using ref forwarding to automatically pass the ref down to the wrapped class component instances.</p>
<p><a href="/docs/forwarding-refs.html">Learn more about the forwardRef API here.</a></p>
<h2 id="component-lifecycle-changes"><a href="#component-lifecycle-changes" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Component Lifecycle Changes</h2>
<p>React’s class component API has been around for years with little change. However, as we add support for more advanced features (such as <a href="/docs/react-component.html#componentdidcatch">error boundaries</a> and the upcoming <a href="/blog/2018/03/01/sneak-peek-beyond-react-16.html">async rendering mode</a>) we stretch this model in ways that it was not originally intended.</p>
<p>For example, with the current API, it is too easy to block the initial render with non-essential logic. In part this is because there are too many ways to accomplish a given task, and it can be unclear which is best. We’ve observed that the interrupting behavior of error handling is often not taken into consideration and can result in memory leaks (something that will also impact the upcoming async rendering mode). The current class component API also complicates other efforts, like our work on <a href="https://twitter.com/trueadm/status/944908776896978946">prototyping a React compiler</a>.</p>
<p>Many of these issues are exacerbated by a subset of the component lifecycles (<code class="gatsby-code-text">componentWillMount</code>, <code class="gatsby-code-text">componentWillReceiveProps</code>, and <code class="gatsby-code-text">componentWillUpdate</code>). These also happen to be the lifecycles that cause the most confusion within the React community. For these reasons, we are going to deprecate those methods in favor of better alternatives.</p>
<p>We recognize that this change will impact many existing components. Because of this, the migration path will be as gradual as possible, and will provide escape hatches. (At Facebook, we maintain more than 50,000 React components. We depend on a gradual release cycle too!)</p>
<blockquote>
<p><strong>Note:</strong></p>
<p>Deprecation warnings will be enabled with a future 16.x release, <strong>but the legacy lifecycles will continue to work until version 17</strong>.</p>
<p>Even in version 17, it will still be possible to use them, but they will be aliased with an “UNSAFE_” prefix to indicate that they might cause issues. We have also prepared an <a href="https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles">automated script to rename them</a> in existing code.</p>
</blockquote>
<p>In addition to deprecating unsafe lifecycles, we are also adding a couple of new lifecyles:</p>
<ul>
<li><a href="/docs/react-component.html#static-getderivedstatefromprops"><code class="gatsby-code-text">getDerivedStateFromProps</code></a> is being added as a safer alternative to the legacy <code class="gatsby-code-text">componentWillReceiveProps</code>. (Note that <a href="/blog/2018/06/07/you-probably-dont-need-derived-state.html">in most cases you don’t need either of them</a>.)</li>
<li><a href="/docs/react-component.html#getsnapshotbeforeupdate"><code class="gatsby-code-text">getSnapshotBeforeUpdate</code></a> is being added to support safely reading properties from e.g. the DOM before updates are made.</li>
</ul>
<p><a href="/blog/2018/03/27/update-on-async-rendering.html">Learn more about these lifecycle changes here.</a></p>
<h2 id="strictmode-component"><a href="#strictmode-component" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a><code class="gatsby-code-text">StrictMode</code> Component</h2>
<p><code class="gatsby-code-text">StrictMode</code> is a tool for highlighting potential problems in an application. Like <code class="gatsby-code-text">Fragment</code>, <code class="gatsby-code-text">StrictMode</code> does not render any visible UI. It activates additional checks and warnings for its descendants.</p>
<blockquote>
<p><strong>Note:</strong></p>
<p><code class="gatsby-code-text">StrictMode</code> checks are run in development mode only; <em>they do not impact the production build</em>.</p>
</blockquote>
<p>Although it is not possible for strict mode to catch all problems (e.g. certain types of mutation), it can help with many. If you see warnings in strict mode, those things will likely cause bugs for async rendering.</p>
<p>In version 16.3, <code class="gatsby-code-text">StrictMode</code> helps with:</p>
<ul>
<li>Identifying components with unsafe lifecycles</li>
<li>Warning about legacy string ref API usage</li>
<li>Detecting unexpected side effects</li>
</ul>
<p>Additional functionality will be added with future releases of React.</p>
<p><a href="/docs/strict-mode.html">Learn more about the <code class="gatsby-code-text">StrictMode</code> component here.</a></p>]]></description><link>https://reactjs.org/blog/2018/03/29/react-v-16-3.html</link><guid isPermaLink="false">https://reactjs.org/blog/2018/03/29/react-v-16-3.html</guid><pubDate>Thu, 29 Mar 2018 00:00:00 GMT</pubDate></item><item><title><![CDATA[Update on Async Rendering]]></title><description><![CDATA[<p>For over a year, the React team has been working to implement asynchronous rendering. Last month during his talk at JSConf Iceland, <a href="/blog/2018/03/01/sneak-peek-beyond-react-16.html">Dan unveiled some of the exciting new possibilities async rendering unlocks</a>. Now we’d like to share with you some of the lessons we’ve learned while working on these features, and some recipes to help prepare your components for async rendering when it launches.</p>
<p>One of the biggest lessons we’ve learned is that some of our legacy component lifecycles tend to encourage unsafe coding practices. They are:</p>
<ul>
<li><code class="gatsby-code-text">componentWillMount</code></li>
<li><code class="gatsby-code-text">componentWillReceiveProps</code></li>
<li><code class="gatsby-code-text">componentWillUpdate</code></li>
</ul>
<p>These lifecycle methods have often been misunderstood and subtly misused; furthermore, we anticipate that their potential misuse may be more problematic with async rendering. Because of this, we will be adding an “UNSAFE_” prefix to these lifecycles in an upcoming release. (Here, “unsafe” refers not to security but instead conveys that code using these lifecycles will be more likely to have bugs in future versions of React, especially once async rendering is enabled.)</p>
<h2 id="gradual-migration-path"><a href="#gradual-migration-path" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Gradual Migration Path</h2>
<p><a href="/blog/2016/02/19/new-versioning-scheme.html">React follows semantic versioning</a>, so this change will be gradual. Our current plan is:</p>
<ul>
<li><strong>16.3</strong>: Introduce aliases for the unsafe lifecycles, <code class="gatsby-code-text">UNSAFE_componentWillMount</code>, <code class="gatsby-code-text">UNSAFE_componentWillReceiveProps</code>, and <code class="gatsby-code-text">UNSAFE_componentWillUpdate</code>. (Both the old lifecycle names and the new aliases will work in this release.)</li>
<li><strong>A future 16.x release</strong>: Enable deprecation warning for <code class="gatsby-code-text">componentWillMount</code>, <code class="gatsby-code-text">componentWillReceiveProps</code>, and <code class="gatsby-code-text">componentWillUpdate</code>. (Both the old lifecycle names and the new aliases will work in this release, but the old names will log a DEV-mode warning.)</li>
<li><strong>17.0</strong>: Remove <code class="gatsby-code-text">componentWillMount</code>, <code class="gatsby-code-text">componentWillReceiveProps</code>, and <code class="gatsby-code-text">componentWillUpdate</code> . (Only the new “UNSAFE_” lifecycle names will work from this point forward.)</li>
</ul>
<p><strong>Note that if you’re a React application developer, you don’t have to do anything about the legacy methods yet. The primary purpose of the upcoming version 16.3 release is to enable open source project maintainers to update their libraries in advance of any deprecation warnings. Those warnings will not be enabled until a future 16.x release.</strong></p>
<p>We maintain over 50,000 React components at Facebook, and we don’t plan to rewrite them all immediately. We understand that migrations take time. We will take the gradual migration path along with everyone in the React community.</p>
<hr>
<h2 id="migrating-from-legacy-lifecycles"><a href="#migrating-from-legacy-lifecycles" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Migrating from Legacy Lifecycles</h2>
<p>If you’d like to start using the new component APIs introduced in React 16.3 (or if you’re a maintainer looking to update your library in advance) here are a few examples that we hope will help you to start thinking about components a bit differently. Over time, we plan to add additional “recipes” to our documentation that show how to perform common tasks in a way that avoids the problematic lifecycles.</p>
<p>Before we begin, here’s a quick overview of the lifecycle changes planned for version 16.3:</p>
<ul>
<li>We are <strong>adding the following lifecycle aliases</strong>: <code class="gatsby-code-text">UNSAFE_componentWillMount</code>, <code class="gatsby-code-text">UNSAFE_componentWillReceiveProps</code>, and <code class="gatsby-code-text">UNSAFE_componentWillUpdate</code>. (Both the old lifecycle names and the new aliases will be supported.)</li>
<li>We are <strong>introducing two new lifecycles</strong>, static <code class="gatsby-code-text">getDerivedStateFromProps</code> and <code class="gatsby-code-text">getSnapshotBeforeUpdate</code>.</li>
</ul>
<h3 id="new-lifecycle-getderivedstatefromprops"><a href="#new-lifecycle-getderivedstatefromprops" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>New lifecycle: <code class="gatsby-code-text">getDerivedStateFromProps</code></h3>
<p><div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token keyword">class</span> <span class="token class-name">Example</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
  <span class="token keyword">static</span> <span class="token function">getDerivedStateFromProps</span><span class="token punctuation">(</span>props<span class="token punctuation">,</span> state<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// ...</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
        </div></p>
<p>The new static <code class="gatsby-code-text">getDerivedStateFromProps</code> lifecycle is invoked after a component is instantiated as well as before it is re-rendered. It can return an object to update <code class="gatsby-code-text">state</code>, or <code class="gatsby-code-text">null</code> to indicate that the new <code class="gatsby-code-text">props</code> do not require any <code class="gatsby-code-text">state</code> updates.</p>
<p>Together with <code class="gatsby-code-text">componentDidUpdate</code>, this new lifecycle should cover all use cases for the legacy <code class="gatsby-code-text">componentWillReceiveProps</code>.</p>
<blockquote>
<p>Note:</p>
<p>Both the older <code class="gatsby-code-text">componentWillReceiveProps</code> and the new <code class="gatsby-code-text">getDerivedStateFromProps</code> methods add significant complexity to components. This often leads to <a href="/blog/2018/06/07/you-probably-dont-need-derived-state.html#common-bugs-when-using-derived-state">bugs</a>. Consider <strong><a href="/blog/2018/06/07/you-probably-dont-need-derived-state.html">simpler alternatives to derived state</a></strong> to make components predictable and maintainable.</p>
</blockquote>
<h3 id="new-lifecycle-getsnapshotbeforeupdate"><a href="#new-lifecycle-getsnapshotbeforeupdate" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>New lifecycle: <code class="gatsby-code-text">getSnapshotBeforeUpdate</code></h3>
<p><div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token keyword">class</span> <span class="token class-name">Example</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
  <span class="token function">getSnapshotBeforeUpdate</span><span class="token punctuation">(</span>prevProps<span class="token punctuation">,</span> prevState<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// ...</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
        </div></p>
<p>The new <code class="gatsby-code-text">getSnapshotBeforeUpdate</code> lifecycle is called right before mutations are made (e.g. before the DOM is updated). The return value for this lifecycle will be passed as the third parameter to <code class="gatsby-code-text">componentDidUpdate</code>. (This lifecycle isn’t often needed, but can be useful in cases like manually preserving scroll position during rerenders.)</p>
<p>Together with <code class="gatsby-code-text">componentDidUpdate</code>, this new lifecycle should cover all use cases for the legacy <code class="gatsby-code-text">componentWillUpdate</code>.</p>
<p>You can find their type signatures <a href="https://gist.github.com/gaearon/88634d27abbc4feeb40a698f760f3264">in this gist</a>.</p>
<p>We’ll look at examples of how both of these lifecycles can be used below.</p>
<h2 id="examples"><a href="#examples" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Examples</h2>
<ul>
<li><a href="#initializing-state">Initializing state</a></li>
<li><a href="#fetching-external-data">Fetching external data</a></li>
<li><a href="#adding-event-listeners-or-subscriptions">Adding event listeners (or subscriptions)</a></li>
<li><a href="#updating-state-based-on-props">Updating <code class="gatsby-code-text">state</code> based on props</a></li>
<li><a href="#invoking-external-callbacks">Invoking external callbacks</a></li>
<li><a href="#side-effects-on-props-change">Side effects on props change</a></li>
<li><a href="#fetching-external-data-when-props-change">Fetching external data when props change</a></li>
<li><a href="#reading-dom-properties-before-an-update">Reading DOM properties before an update</a></li>
</ul>
<blockquote>
<p>Note</p>
<p>For brevity, the examples below are written using the experimental class properties transform, but the same migration strategies apply without it.</p>
</blockquote>
<h3 id="initializing-state"><a href="#initializing-state" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Initializing state</h3>
<p>This example shows a component with <code class="gatsby-code-text">setState</code> calls inside of <code class="gatsby-code-text">componentWillMount</code>:
<div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token comment">// Before</span>
<span class="token keyword">class</span> <span class="token class-name">ExampleComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
  state <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="gatsby-highlight-code-line">  <span class="token function">componentWillMount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">      currentColor<span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>defaultColor<span class="token punctuation">,</span>
</span><span class="gatsby-highlight-code-line">      palette<span class="token punctuation">:</span> <span class="token string">'rgb'</span><span class="token punctuation">,</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span>
</span><span class="token punctuation">}</span></code></pre>
        </div></p>
<p>The simplest refactor for this type of component is to move state initialization to the constructor or to a property initializer, like so:
<div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token comment">// After</span>
<span class="token keyword">class</span> <span class="token class-name">ExampleComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
<span class="gatsby-highlight-code-line">  state <span class="token operator">=</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    currentColor<span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>defaultColor<span class="token punctuation">,</span>
</span><span class="gatsby-highlight-code-line">    palette<span class="token punctuation">:</span> <span class="token string">'rgb'</span><span class="token punctuation">,</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span><span class="token punctuation">;</span>
</span><span class="token punctuation">}</span></code></pre>
        </div></p>
<h3 id="fetching-external-data"><a href="#fetching-external-data" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Fetching external data</h3>
<p>Here is an example of a component that uses <code class="gatsby-code-text">componentWillMount</code> to fetch external data:
<div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token comment">// Before</span>
<span class="token keyword">class</span> <span class="token class-name">ExampleComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
  state <span class="token operator">=</span> <span class="token punctuation">{</span>
    externalData<span class="token punctuation">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="gatsby-highlight-code-line">  <span class="token function">componentWillMount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">this</span><span class="token punctuation">.</span>_asyncRequest <span class="token operator">=</span> <span class="token function">asyncLoadData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>
</span><span class="gatsby-highlight-code-line">      externalData <span class="token operator">=></span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">        <span class="token keyword">this</span><span class="token punctuation">.</span>_asyncRequest <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>externalData<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">      <span class="token punctuation">}</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span>
</span>
  <span class="token function">componentWillUnmount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>_asyncRequest<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>_asyncRequest<span class="token punctuation">.</span><span class="token function">cancel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>externalData <span class="token operator">===</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// Render loading state ...</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token comment">// Render real UI ...</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
        </div></p>
<p>The above code is problematic for both server rendering (where the external data won’t be used) and the upcoming async rendering mode (where the request might be initiated multiple times).</p>
<p>The recommended upgrade path for most use cases is to move data-fetching into <code class="gatsby-code-text">componentDidMount</code>:
<div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token comment">// After</span>
<span class="token keyword">class</span> <span class="token class-name">ExampleComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
  state <span class="token operator">=</span> <span class="token punctuation">{</span>
    externalData<span class="token punctuation">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="gatsby-highlight-code-line">  <span class="token function">componentDidMount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">this</span><span class="token punctuation">.</span>_asyncRequest <span class="token operator">=</span> <span class="token function">asyncLoadData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>
</span><span class="gatsby-highlight-code-line">      externalData <span class="token operator">=></span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">        <span class="token keyword">this</span><span class="token punctuation">.</span>_asyncRequest <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>externalData<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">      <span class="token punctuation">}</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span>
</span>
  <span class="token function">componentWillUnmount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>_asyncRequest<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>_asyncRequest<span class="token punctuation">.</span><span class="token function">cancel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>externalData <span class="token operator">===</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// Render loading state ...</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token comment">// Render real UI ...</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
        </div></p>
<p>There is a common misconception that fetching in <code class="gatsby-code-text">componentWillMount</code> lets you avoid the first empty rendering state. In practice this was never true because React has always executed <code class="gatsby-code-text">render</code> immediately after <code class="gatsby-code-text">componentWillMount</code>. If the data is not available by the time <code class="gatsby-code-text">componentWillMount</code> fires, the first <code class="gatsby-code-text">render</code> will still show a loading state regardless of where you initiate the fetch. This is why moving the fetch to <code class="gatsby-code-text">componentDidMount</code> has no perceptible effect in the vast majority of cases.</p>
<blockquote>
<p>Note</p>
<p>Some advanced use-cases (e.g. libraries like Relay) may want to experiment with eagerly prefetching async data. An example of how this can be done is available <a href="https://gist.github.com/bvaughn/89700e525ff423a75ffb63b1b1e30a8f">here</a>.</p>
<p>In the longer term, the canonical way to fetch data in React components will likely be based on the “suspense” API <a href="/blog/2018/03/01/sneak-peek-beyond-react-16.html">introduced at JSConf Iceland</a>. Both simple data fetching solutions and libraries like Apollo and Relay will be able to use it under the hood. It is significantly less verbose than either of the above solutions, but will not be finalized in time for the 16.3 release.</p>
<p>When supporting server rendering, it’s currently necessary to provide the data synchronously – <code class="gatsby-code-text">componentWillMount</code> was often used for this purpose but the constructor can be used as a replacement. The upcoming suspense APIs will make async data fetching cleanly possible for both client and server rendering.</p>
</blockquote>
<h3 id="adding-event-listeners-or-subscriptions"><a href="#adding-event-listeners-or-subscriptions" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Adding event listeners (or subscriptions)</h3>
<p>Here is an example of a component that subscribes to an external event dispatcher when mounting:
<div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token comment">// Before</span>
<span class="token keyword">class</span> <span class="token class-name">ExampleComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
<span class="gatsby-highlight-code-line">  <span class="token function">componentWillMount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">      subscribedValue<span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>dataSource<span class="token punctuation">.</span>value<span class="token punctuation">,</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// This is not safe; it can leak!</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>dataSource<span class="token punctuation">.</span><span class="token function">subscribe</span><span class="token punctuation">(</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">this</span><span class="token punctuation">.</span>handleSubscriptionChange
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span>
</span>
  <span class="token function">componentWillUnmount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>dataSource<span class="token punctuation">.</span><span class="token function">unsubscribe</span><span class="token punctuation">(</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>handleSubscriptionChange
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token function-variable function">handleSubscriptionChange</span> <span class="token operator">=</span> dataSource <span class="token operator">=></span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
      subscribedValue<span class="token punctuation">:</span> dataSource<span class="token punctuation">.</span>value<span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
        </div></p>
<p>Unfortunately, this can cause memory leaks for server rendering (where <code class="gatsby-code-text">componentWillUnmount</code> will never be called) and async rendering (where rendering might be interrupted before it completes, causing <code class="gatsby-code-text">componentWillUnmount</code> not to be called).</p>
<p>People often assume that <code class="gatsby-code-text">componentWillMount</code> and <code class="gatsby-code-text">componentWillUnmount</code> are always paired, but that is not guaranteed. Only once <code class="gatsby-code-text">componentDidMount</code> has been called does React guarantee that <code class="gatsby-code-text">componentWillUnmount</code> will later be called for clean up.</p>
<p>For this reason, the recommended way to add listeners/subscriptions is to use the <code class="gatsby-code-text">componentDidMount</code> lifecycle:
<div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token comment">// After</span>
<span class="token keyword">class</span> <span class="token class-name">ExampleComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
<span class="gatsby-highlight-code-line">  state <span class="token operator">=</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    subscribedValue<span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>dataSource<span class="token punctuation">.</span>value<span class="token punctuation">,</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">
</span><span class="gatsby-highlight-code-line">  <span class="token function">componentDidMount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// Event listeners are only safe to add after mount,</span>
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// So they won't leak if mount is interrupted or errors.</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>dataSource<span class="token punctuation">.</span><span class="token function">subscribe</span><span class="token punctuation">(</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">this</span><span class="token punctuation">.</span>handleSubscriptionChange
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// External values could change between render and mount,</span>
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// In some cases it may be important to handle this case.</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">if</span> <span class="token punctuation">(</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>subscribedValue <span class="token operator">!==</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>dataSource<span class="token punctuation">.</span>value
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">        subscribedValue<span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>dataSource<span class="token punctuation">.</span>value<span class="token punctuation">,</span>
</span><span class="gatsby-highlight-code-line">      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">}</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span>
</span>
  <span class="token function">componentWillUnmount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>dataSource<span class="token punctuation">.</span><span class="token function">unsubscribe</span><span class="token punctuation">(</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>handleSubscriptionChange
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token function-variable function">handleSubscriptionChange</span> <span class="token operator">=</span> dataSource <span class="token operator">=></span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
      subscribedValue<span class="token punctuation">:</span> dataSource<span class="token punctuation">.</span>value<span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
        </div></p>
<p>Sometimes it is important to update subscriptions in response to property changes. If you’re using a library like Redux or MobX, the library’s container component should handle this for you. For application authors, we’ve created a small library, <a href="https://github.com/facebook/react/tree/master/packages/create-subscription"><code class="gatsby-code-text">create-subscription</code></a>, to help with this. We’ll publish it along with React 16.3.</p>
<p>Rather than passing a subscribable <code class="gatsby-code-text">dataSource</code> prop as we did in the example above, we could use <code class="gatsby-code-text">create-subscription</code> to pass in the subscribed value:</p>
<p><div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token keyword">import</span> <span class="token punctuation">{</span>createSubscription<span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'create-subscription'</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> Subscription <span class="token operator">=</span> <span class="token function">createSubscription</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token function">getCurrentValue</span><span class="token punctuation">(</span>sourceProp<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// Return the current value of the subscription (sourceProp).</span>
    <span class="token keyword">return</span> sourceProp<span class="token punctuation">.</span>value<span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>

  <span class="token function">subscribe</span><span class="token punctuation">(</span>sourceProp<span class="token punctuation">,</span> callback<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">function</span> <span class="token function">handleSubscriptionChange</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">callback</span><span class="token punctuation">(</span>sourceProp<span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// Subscribe (e.g. add an event listener) to the subscription (sourceProp).</span>
    <span class="token comment">// Call callback(newValue) whenever a subscription changes.</span>
    sourceProp<span class="token punctuation">.</span><span class="token function">subscribe</span><span class="token punctuation">(</span>handleSubscriptionChange<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// Return an unsubscribe method.</span>
    <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token function">unsubscribe</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      sourceProp<span class="token punctuation">.</span><span class="token function">unsubscribe</span><span class="token punctuation">(</span>handleSubscriptionChange<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// Rather than passing the subscribable source to our ExampleComponent,</span>
<span class="token comment">// We could just pass the subscribed value directly:</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Subscription</span> <span class="token attr-name">source</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>dataSource<span class="token punctuation">}</span></span><span class="token punctuation">></span></span>
  <span class="token punctuation">{</span>value <span class="token operator">=></span> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ExampleComponent</span> <span class="token attr-name">subscribedValue</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>value<span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span><span class="token punctuation">}</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>Subscription</span><span class="token punctuation">></span></span><span class="token punctuation">;</span></code></pre>
        </div></p>
<blockquote>
<p>Note</p>
<p>Libraries like Relay/Apollo should manage subscriptions manually with the same techniques as <code class="gatsby-code-text">create-subscription</code> uses under the hood (as referenced <a href="https://gist.github.com/bvaughn/d569177d70b50b58bff69c3c4a5353f3">here</a>) in a way that is most optimized for their library usage.</p>
</blockquote>
<h3 id="updating-state-based-on-props"><a href="#updating-state-based-on-props" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Updating <code class="gatsby-code-text">state</code> based on <code class="gatsby-code-text">props</code></h3>
<blockquote>
<p>Note:</p>
<p>Both the older <code class="gatsby-code-text">componentWillReceiveProps</code> and the new <code class="gatsby-code-text">getDerivedStateFromProps</code> methods add significant complexity to components. This often leads to <a href="/blog/2018/06/07/you-probably-dont-need-derived-state.html#common-bugs-when-using-derived-state">bugs</a>. Consider <strong><a href="/blog/2018/06/07/you-probably-dont-need-derived-state.html">simpler alternatives to derived state</a></strong> to make components predictable and maintainable.</p>
</blockquote>
<p>Here is an example of a component that uses the legacy <code class="gatsby-code-text">componentWillReceiveProps</code> lifecycle to update <code class="gatsby-code-text">state</code> based on new <code class="gatsby-code-text">props</code> values:
<div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token comment">// Before</span>
<span class="token keyword">class</span> <span class="token class-name">ExampleComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
  state <span class="token operator">=</span> <span class="token punctuation">{</span>
    isScrollingDown<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="gatsby-highlight-code-line">  <span class="token function">componentWillReceiveProps</span><span class="token punctuation">(</span>nextProps<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>currentRow <span class="token operator">!==</span> nextProps<span class="token punctuation">.</span>currentRow<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">        isScrollingDown<span class="token punctuation">:</span>
</span><span class="gatsby-highlight-code-line">          nextProps<span class="token punctuation">.</span>currentRow <span class="token operator">></span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>currentRow<span class="token punctuation">,</span>
</span><span class="gatsby-highlight-code-line">      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">}</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span>
</span><span class="token punctuation">}</span></code></pre>
        </div></p>
<p>Although the above code is not problematic in itself, the <code class="gatsby-code-text">componentWillReceiveProps</code> lifecycle is often mis-used in ways that <em>do</em> present problems. Because of this, the method will be deprecated.</p>
<p>As of version 16.3, the recommended way to update <code class="gatsby-code-text">state</code> in response to <code class="gatsby-code-text">props</code> changes is with the new <code class="gatsby-code-text">static getDerivedStateFromProps</code> lifecycle. (That lifecycle is called when a component is created and each time it receives new props):
<div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token comment">// After</span>
<span class="token keyword">class</span> <span class="token class-name">ExampleComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
  <span class="token comment">// Initialize state in constructor,</span>
  <span class="token comment">// Or with a property initializer.</span>
<span class="gatsby-highlight-code-line">  state <span class="token operator">=</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    isScrollingDown<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
</span><span class="gatsby-highlight-code-line">    lastRow<span class="token punctuation">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span><span class="token punctuation">;</span>
</span>
<span class="gatsby-highlight-code-line">  <span class="token keyword">static</span> <span class="token function">getDerivedStateFromProps</span><span class="token punctuation">(</span>props<span class="token punctuation">,</span> state<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">if</span> <span class="token punctuation">(</span>props<span class="token punctuation">.</span>currentRow <span class="token operator">!==</span> state<span class="token punctuation">.</span>lastRow<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">return</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">        isScrollingDown<span class="token punctuation">:</span> props<span class="token punctuation">.</span>currentRow <span class="token operator">></span> state<span class="token punctuation">.</span>lastRow<span class="token punctuation">,</span>
</span><span class="gatsby-highlight-code-line">        lastRow<span class="token punctuation">:</span> props<span class="token punctuation">.</span>currentRow<span class="token punctuation">,</span>
</span><span class="gatsby-highlight-code-line">      <span class="token punctuation">}</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">}</span>
</span>
    <span class="token comment">// Return null to indicate no change to state.</span>
    <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
        </div></p>
<p>You may notice in the example above that <code class="gatsby-code-text">props.currentRow</code> is mirrored in state (as <code class="gatsby-code-text">state.lastRow</code>). This enables <code class="gatsby-code-text">getDerivedStateFromProps</code> to access the previous props value in the same way as is done in <code class="gatsby-code-text">componentWillReceiveProps</code>.</p>
<p>You may wonder why we don’t just pass previous props as a parameter to <code class="gatsby-code-text">getDerivedStateFromProps</code>. We considered this option when designing the API, but ultimately decided against it for two reasons:</p>
<ul>
<li>A <code class="gatsby-code-text">prevProps</code> parameter would be null the first time <code class="gatsby-code-text">getDerivedStateFromProps</code> was called (after instantiation), requiring an if-not-null check to be added any time <code class="gatsby-code-text">prevProps</code> was accessed.</li>
<li>Not passing the previous props to this function is a step toward freeing up memory in future versions of React. (If React does not need to pass previous props to lifecycles, then it does not need to keep the previous <code class="gatsby-code-text">props</code> object in memory.)</li>
</ul>
<blockquote>
<p>Note</p>
<p>If you’re writing a shared component, the <a href="https://github.com/reactjs/react-lifecycles-compat"><code class="gatsby-code-text">react-lifecycles-compat</code></a> polyfill enables the new <code class="gatsby-code-text">getDerivedStateFromProps</code> lifecycle to be used with older versions of React as well. <a href="#open-source-project-maintainers">Learn more about how to use it below.</a></p>
</blockquote>
<h3 id="invoking-external-callbacks"><a href="#invoking-external-callbacks" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Invoking external callbacks</h3>
<p>Here is an example of a component that calls an external function when its internal state changes:
<div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token comment">// Before</span>
<span class="token keyword">class</span> <span class="token class-name">ExampleComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
<span class="gatsby-highlight-code-line">  <span class="token function">componentWillUpdate</span><span class="token punctuation">(</span>nextProps<span class="token punctuation">,</span> nextState<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">if</span> <span class="token punctuation">(</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>someStatefulValue <span class="token operator">!==</span>
</span><span class="gatsby-highlight-code-line">      nextState<span class="token punctuation">.</span>someStatefulValue
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">      nextProps<span class="token punctuation">.</span><span class="token function">onChange</span><span class="token punctuation">(</span>nextState<span class="token punctuation">.</span>someStatefulValue<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">}</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span>
</span><span class="token punctuation">}</span></code></pre>
        </div></p>
<p>Sometimes people use <code class="gatsby-code-text">componentWillUpdate</code> out of a misplaced fear that by the time <code class="gatsby-code-text">componentDidUpdate</code> fires, it is “too late” to update the state of other components. This is not the case. React ensures that any <code class="gatsby-code-text">setState</code> calls that happen during <code class="gatsby-code-text">componentDidMount</code> and <code class="gatsby-code-text">componentDidUpdate</code> are flushed before the user sees the updated UI. In general, it is better to avoid cascading updates like this, but in some cases they are necessary (for example, if you need to position a tooltip after measuring the rendered DOM element).</p>
<p>Either way, it is unsafe to use <code class="gatsby-code-text">componentWillUpdate</code> for this purpose in async mode, because the external callback might get called multiple times for a single update. Instead, the <code class="gatsby-code-text">componentDidUpdate</code> lifecycle should be used since it is guaranteed to be invoked only once per update:
<div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token comment">// After</span>
<span class="token keyword">class</span> <span class="token class-name">ExampleComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
<span class="gatsby-highlight-code-line">  <span class="token function">componentDidUpdate</span><span class="token punctuation">(</span>prevProps<span class="token punctuation">,</span> prevState<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">if</span> <span class="token punctuation">(</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>someStatefulValue <span class="token operator">!==</span>
</span><span class="gatsby-highlight-code-line">      prevState<span class="token punctuation">.</span>someStatefulValue
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span><span class="token function">onChange</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>someStatefulValue<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">}</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span>
</span><span class="token punctuation">}</span></code></pre>
        </div></p>
<h3 id="side-effects-on-props-change"><a href="#side-effects-on-props-change" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Side effects on props change</h3>
<p>Similar to the <a href="#invoking-external-callbacks">example above</a>, sometimes components have side effects when <code class="gatsby-code-text">props</code> change.</p>
<p><div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token comment">// Before</span>
<span class="token keyword">class</span> <span class="token class-name">ExampleComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
<span class="gatsby-highlight-code-line">  <span class="token function">componentWillReceiveProps</span><span class="token punctuation">(</span>nextProps<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>isVisible <span class="token operator">!==</span> nextProps<span class="token punctuation">.</span>isVisible<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">      <span class="token function">logVisibleChange</span><span class="token punctuation">(</span>nextProps<span class="token punctuation">.</span>isVisible<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">}</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span>
</span><span class="token punctuation">}</span></code></pre>
        </div></p>
<p>Like <code class="gatsby-code-text">componentWillUpdate</code>, <code class="gatsby-code-text">componentWillReceiveProps</code> might get called multiple times for a single update. For this reason it is important to avoid putting side effects in this method. Instead, <code class="gatsby-code-text">componentDidUpdate</code> should be used since it is guaranteed to be invoked only once per update:</p>
<p><div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token comment">// After</span>
<span class="token keyword">class</span> <span class="token class-name">ExampleComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
<span class="gatsby-highlight-code-line">  <span class="token function">componentDidUpdate</span><span class="token punctuation">(</span>prevProps<span class="token punctuation">,</span> prevState<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>isVisible <span class="token operator">!==</span> prevProps<span class="token punctuation">.</span>isVisible<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">      <span class="token function">logVisibleChange</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>isVisible<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">}</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span>
</span><span class="token punctuation">}</span></code></pre>
        </div></p>
<h3 id="fetching-external-data-when-props-change"><a href="#fetching-external-data-when-props-change" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Fetching external data when <code class="gatsby-code-text">props</code> change</h3>
<p>Here is an example of a component that fetches external data based on <code class="gatsby-code-text">props</code> values:
<div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token comment">// Before</span>
<span class="token keyword">class</span> <span class="token class-name">ExampleComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
  state <span class="token operator">=</span> <span class="token punctuation">{</span>
    externalData<span class="token punctuation">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token function">componentDidMount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">_loadAsyncData</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

<span class="gatsby-highlight-code-line">  <span class="token function">componentWillReceiveProps</span><span class="token punctuation">(</span>nextProps<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">if</span> <span class="token punctuation">(</span>nextProps<span class="token punctuation">.</span>id <span class="token operator">!==</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>externalData<span class="token punctuation">:</span> <span class="token keyword">null</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">_loadAsyncData</span><span class="token punctuation">(</span>nextProps<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">}</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span>
</span>
  <span class="token function">componentWillUnmount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>_asyncRequest<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>_asyncRequest<span class="token punctuation">.</span><span class="token function">cancel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>externalData <span class="token operator">===</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// Render loading state ...</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token comment">// Render real UI ...</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  <span class="token function">_loadAsyncData</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>_asyncRequest <span class="token operator">=</span> <span class="token function">asyncLoadData</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>
      externalData <span class="token operator">=></span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>_asyncRequest <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>externalData<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
        </div></p>
<p>The recommended upgrade path for this component is to move data updates into <code class="gatsby-code-text">componentDidUpdate</code>. You can also use the new <code class="gatsby-code-text">getDerivedStateFromProps</code> lifecycle to clear stale data before rendering the new props:
<div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token comment">// After</span>
<span class="token keyword">class</span> <span class="token class-name">ExampleComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
  state <span class="token operator">=</span> <span class="token punctuation">{</span>
    externalData<span class="token punctuation">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="gatsby-highlight-code-line">  <span class="token keyword">static</span> <span class="token function">getDerivedStateFromProps</span><span class="token punctuation">(</span>props<span class="token punctuation">,</span> state<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// Store prevId in state so we can compare when props change.</span>
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// Clear out previously-loaded data (so we don't render stale stuff).</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">if</span> <span class="token punctuation">(</span>props<span class="token punctuation">.</span>id <span class="token operator">!==</span> state<span class="token punctuation">.</span>prevId<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">return</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">        externalData<span class="token punctuation">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span>
</span><span class="gatsby-highlight-code-line">        prevId<span class="token punctuation">:</span> props<span class="token punctuation">.</span>id<span class="token punctuation">,</span>
</span><span class="gatsby-highlight-code-line">      <span class="token punctuation">}</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">}</span>
</span><span class="gatsby-highlight-code-line">
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// No state update necessary</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span>
</span>
  <span class="token function">componentDidMount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">_loadAsyncData</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

<span class="gatsby-highlight-code-line">  <span class="token function">componentDidUpdate</span><span class="token punctuation">(</span>prevProps<span class="token punctuation">,</span> prevState<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>externalData <span class="token operator">===</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">_loadAsyncData</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">}</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span>
</span>
  <span class="token function">componentWillUnmount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>_asyncRequest<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>_asyncRequest<span class="token punctuation">.</span><span class="token function">cancel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>externalData <span class="token operator">===</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// Render loading state ...</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token comment">// Render real UI ...</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  <span class="token function">_loadAsyncData</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>_asyncRequest <span class="token operator">=</span> <span class="token function">asyncLoadData</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>
      externalData <span class="token operator">=></span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>_asyncRequest <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>externalData<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
        </div></p>
<blockquote>
<p>Note</p>
<p>If you’re using an HTTP library that supports cancellation, like <a href="https://www.npmjs.com/package/axios">axios</a>, then it’s simple to cancel an in-progress request when unmounting. For native Promises, you can use an approach like <a href="https://gist.github.com/bvaughn/982ab689a41097237f6e9860db7ca8d6">the one shown here</a>.</p>
</blockquote>
<h3 id="reading-dom-properties-before-an-update"><a href="#reading-dom-properties-before-an-update" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Reading DOM properties before an update</h3>
<p>Here is an example of a component that reads a property from the DOM before an update in order to maintain scroll position within a list:
<div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token keyword">class</span> <span class="token class-name">ScrollingList</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
  listRef <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
  previousScrollOffset <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>

<span class="gatsby-highlight-code-line">  <span class="token function">componentWillUpdate</span><span class="token punctuation">(</span>nextProps<span class="token punctuation">,</span> nextState<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// Are we adding new items to the list?</span>
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// Capture the scroll position so we can adjust scroll later.</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>list<span class="token punctuation">.</span>length <span class="token operator">&lt;</span> nextProps<span class="token punctuation">.</span>list<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">this</span><span class="token punctuation">.</span>previousScrollOffset <span class="token operator">=</span>
</span><span class="gatsby-highlight-code-line">        <span class="token keyword">this</span><span class="token punctuation">.</span>listRef<span class="token punctuation">.</span>scrollHeight <span class="token operator">-</span> <span class="token keyword">this</span><span class="token punctuation">.</span>listRef<span class="token punctuation">.</span>scrollTop<span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">}</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span>
</span>
<span class="gatsby-highlight-code-line">  <span class="token function">componentDidUpdate</span><span class="token punctuation">(</span>prevProps<span class="token punctuation">,</span> prevState<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// If previousScrollOffset is set, we've just added new items.</span>
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// Adjust scroll so these new items don't push the old ones out of view.</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>previousScrollOffset <span class="token operator">!==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">this</span><span class="token punctuation">.</span>listRef<span class="token punctuation">.</span>scrollTop <span class="token operator">=</span>
</span><span class="gatsby-highlight-code-line">        <span class="token keyword">this</span><span class="token punctuation">.</span>listRef<span class="token punctuation">.</span>scrollHeight <span class="token operator">-</span>
</span><span class="gatsby-highlight-code-line">        <span class="token keyword">this</span><span class="token punctuation">.</span>previousScrollOffset<span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">this</span><span class="token punctuation">.</span>previousScrollOffset <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">}</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span>
</span>
  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">ref</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>setListRef<span class="token punctuation">}</span></span><span class="token punctuation">></span></span>
        <span class="token punctuation">{</span><span class="token comment">/* ...contents... */</span><span class="token punctuation">}</span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token function-variable function">setListRef</span> <span class="token operator">=</span> ref <span class="token operator">=></span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>listRef <span class="token operator">=</span> ref<span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
        </div></p>
<p>In the above example, <code class="gatsby-code-text">componentWillUpdate</code> is used to read the DOM property. However with async rendering, there may be delays between “render” phase lifecycles (like <code class="gatsby-code-text">componentWillUpdate</code> and <code class="gatsby-code-text">render</code>) and “commit” phase lifecycles (like <code class="gatsby-code-text">componentDidUpdate</code>). If the user does something like resize the window during this time, the <code class="gatsby-code-text">scrollHeight</code> value read from <code class="gatsby-code-text">componentWillUpdate</code> will be stale.</p>
<p>The solution to this problem is to use the new “commit” phase lifecycle, <code class="gatsby-code-text">getSnapshotBeforeUpdate</code>. This method gets called <em>immediately before</em> mutations are made (e.g. before the DOM is updated). It can return a value for React to pass as a parameter to <code class="gatsby-code-text">componentDidUpdate</code>, which gets called <em>immediately after</em> mutations.</p>
<p>The two lifecycles can be used together like this:</p>
<p><div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token keyword">class</span> <span class="token class-name">ScrollingList</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
  listRef <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>

<span class="gatsby-highlight-code-line">  <span class="token function">getSnapshotBeforeUpdate</span><span class="token punctuation">(</span>prevProps<span class="token punctuation">,</span> prevState<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// Are we adding new items to the list?</span>
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// Capture the scroll position so we can adjust scroll later.</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">if</span> <span class="token punctuation">(</span>prevProps<span class="token punctuation">.</span>list<span class="token punctuation">.</span>length <span class="token operator">&lt;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>list<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">return</span> <span class="token punctuation">(</span>
</span><span class="gatsby-highlight-code-line">        <span class="token keyword">this</span><span class="token punctuation">.</span>listRef<span class="token punctuation">.</span>scrollHeight <span class="token operator">-</span> <span class="token keyword">this</span><span class="token punctuation">.</span>listRef<span class="token punctuation">.</span>scrollTop
</span><span class="gatsby-highlight-code-line">      <span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">}</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">  <span class="token punctuation">}</span>
</span>
<span class="gatsby-highlight-code-line">  <span class="token function">componentDidUpdate</span><span class="token punctuation">(</span>prevProps<span class="token punctuation">,</span> prevState<span class="token punctuation">,</span> snapshot<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// If we have a snapshot value, we've just added new items.</span>
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// Adjust scroll so these new items don't push the old ones out of view.</span>
</span><span class="gatsby-highlight-code-line">    <span class="token comment">// (snapshot here is the value returned from getSnapshotBeforeUpdate)</span>
</span><span class="gatsby-highlight-code-line">    <span class="token keyword">if</span> <span class="token punctuation">(</span>snapshot <span class="token operator">!==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="gatsby-highlight-code-line">      <span class="token keyword">this</span><span class="token punctuation">.</span>listRef<span class="token punctuation">.</span>scrollTop <span class="token operator">=</span>
</span><span class="gatsby-highlight-code-line">        <span class="token keyword">this</span><span class="token punctuation">.</span>listRef<span class="token punctuation">.</span>scrollHeight <span class="token operator">-</span> snapshot<span class="token punctuation">;</span>
</span><span class="gatsby-highlight-code-line">    <span class="token punctuation">}</span>
</span>  <span class="token punctuation">}</span>

  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">ref</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>setListRef<span class="token punctuation">}</span></span><span class="token punctuation">></span></span>
        <span class="token punctuation">{</span><span class="token comment">/* ...contents... */</span><span class="token punctuation">}</span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token function-variable function">setListRef</span> <span class="token operator">=</span> ref <span class="token operator">=></span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>listRef <span class="token operator">=</span> ref<span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
        </div></p>
<blockquote>
<p>Note</p>
<p>If you’re writing a shared component, the <a href="https://github.com/reactjs/react-lifecycles-compat"><code class="gatsby-code-text">react-lifecycles-compat</code></a> polyfill enables the new <code class="gatsby-code-text">getSnapshotBeforeUpdate</code> lifecycle to be used with older versions of React as well. <a href="#open-source-project-maintainers">Learn more about how to use it below.</a></p>
</blockquote>
<h2 id="other-scenarios"><a href="#other-scenarios" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Other scenarios</h2>
<p>While we tried to cover the most common use cases in this post, we recognize that we might have missed some of them. If you are using <code class="gatsby-code-text">componentWillMount</code>, <code class="gatsby-code-text">componentWillUpdate</code>, or <code class="gatsby-code-text">componentWillReceiveProps</code> in ways that aren’t covered by this blog post, and aren’t sure how to migrate off these legacy lifecycles, please <a href="https://github.com/reactjs/reactjs.org/issues/new">file a new issue against our documentation</a> with your code examples and as much background information as you can provide. We will update this document with new alternative patterns as they come up.</p>
<h2 id="open-source-project-maintainers"><a href="#open-source-project-maintainers" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Open source project maintainers</h2>
<p>Open source maintainers might be wondering what these changes mean for shared components. If you implement the above suggestions, what happens with components that depend on the new static <code class="gatsby-code-text">getDerivedStateFromProps</code> lifecycle? Do you also have to release a new major version and drop compatibility for React 16.2 and older?</p>
<p>Fortunately, you do not!</p>
<p>When React 16.3 is published, we’ll also publish a new npm package, <a href="https://github.com/reactjs/react-lifecycles-compat"><code class="gatsby-code-text">react-lifecycles-compat</code></a>. This package polyfills components so that the new <code class="gatsby-code-text">getDerivedStateFromProps</code> and <code class="gatsby-code-text">getSnapshotBeforeUpdate</code> lifecycles will also work with older versions of React (0.14.9+).</p>
<p>To use this polyfill, first add it as a dependency to your library:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-bash"><code class="gatsby-code-bash"><span class="token comment"># Yarn</span>
yarn add react-lifecycles-compat

<span class="token comment"># NPM</span>
<span class="token function">npm</span> <span class="token function">install</span> react-lifecycles-compat --save</code></pre>
      </div>
<p>Next, update your components to use the new lifecycles (as described above).</p>
<p>Lastly, use the polyfill to make your component backwards compatible with older versions of React:
<div class="gatsby-highlight">
        <pre class="gatsby-code-jsx"><code><span class="token keyword">import</span> React <span class="token keyword">from</span> <span class="token string">'react'</span><span class="token punctuation">;</span>
<span class="gatsby-highlight-code-line"><span class="token keyword">import</span> <span class="token punctuation">{</span>polyfill<span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'react-lifecycles-compat'</span><span class="token punctuation">;</span>
</span>
<span class="token keyword">class</span> <span class="token class-name">ExampleComponent</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
<span class="gatsby-highlight-code-line">  <span class="token keyword">static</span> <span class="token function">getDerivedStateFromProps</span><span class="token punctuation">(</span>props<span class="token punctuation">,</span> state<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span>    <span class="token comment">// Your state update logic here ...</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// Polyfill your component to work with older versions of React:</span>
<span class="gatsby-highlight-code-line"><span class="token function">polyfill</span><span class="token punctuation">(</span>ExampleComponent<span class="token punctuation">)</span><span class="token punctuation">;</span>
</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> ExampleComponent<span class="token punctuation">;</span></code></pre>
        </div></p>]]></description><link>https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html</link><guid isPermaLink="false">https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html</guid><pubDate>Tue, 27 Mar 2018 00:00:00 GMT</pubDate></item><item><title><![CDATA[Sneak Peek: Beyond React 16]]></title><description><![CDATA[<p><a href="https://twitter.com/dan_abramov">Dan Abramov</a> from our team just spoke at <a href="https://2018.jsconf.is/">JSConf Iceland 2018</a> with a preview of some new features we’ve been working on in React. The talk opens with a question: “With vast differences in computing power and network speed, how do we deliver the best user experience for everyone?”</p>
<p>Here’s the video courtesy of JSConf Iceland:</p>
<br>
<div>
          <div
            class="gatsby-resp-iframe-wrapper"
            style="padding-bottom: 56.25%; position: relative; height: 0; overflow: hidden;"
          >
            <iframe src="https://www.youtube-nocookie.com/embed/nLF0n9SACd4?rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen style="
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
          "></iframe>
          </div>
          </div>
<p>I think you’ll enjoy the talk more if you stop reading here and just watch the video. If you don’t have time to watch, a (very) brief summary follows.</p>
<h2 id="about-the-two-demos"><a href="#about-the-two-demos" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>About the Two Demos</h2>
<p>On the first demo, Dan says: “We’ve built a generic way to ensure that high-priority updates don’t get blocked by a low-priority update, called <strong>time slicing</strong>. If my device is fast enough, it feels almost like it’s synchronous; if my device is slow, the app still feels responsive. It adapts to the device thanks to the <a href="https://developers.google.com/web/updates/2015/08/using-requestidlecallback">requestIdleCallback</a> API. Notice that only the final state was displayed; the rendered screen is always consistent and we don’t see visual artifacts of slow rendering causing a janky user experience.”</p>
<p>On the second demo, Dan explains: “We’ve built a generic way for components to suspend rendering while they load async data, which we call <strong>suspense</strong>. You can pause any state update until the data is ready, and you can add async loading to any component deep in the tree without plumbing all the props and state through your app and hoisting the logic. On a fast network, updates appear very fluid and instantaneous without a jarring cascade of spinners that appear and disappear. On a slow network, you can intentionally design which loading states the user should see and how granular or coarse they should be, instead of showing spinners based on how the code is written. The app stays responsive throughout.”</p>
<p>“Importantly, this is still the React you know. This is still the declarative component paradigm that you probably like about React.”</p>
<p>We can’t wait to release these new async rendering features later this year. Follow this blog and <a href="https://twitter.com/reactjs">@reactjs on Twitter</a> for updates.</p>]]></description><link>https://reactjs.org/blog/2018/03/01/sneak-peek-beyond-react-16.html</link><guid isPermaLink="false">https://reactjs.org/blog/2018/03/01/sneak-peek-beyond-react-16.html</guid><pubDate>Thu, 01 Mar 2018 00:00:00 GMT</pubDate></item><item><title><![CDATA[Behind the Scenes: Improving the Repository Infrastructure]]></title><description><![CDATA[<p>As we worked on <a href="/blog/2017/09/26/react-v16.0.html">React 16</a>, we revamped the folder structure and much of the build tooling in the React repository. Among other things, we introduced projects such as <a href="https://rollupjs.org/">Rollup</a>, <a href="https://prettier.io/">Prettier</a>, and <a href="https://developers.google.com/closure/compiler/">Google Closure Compiler</a> into our workflow. People often ask us questions about how we use those tools. In this post, we would like to share some of the changes that we’ve made to our build and test infrastructure in 2017, and what motivated them.</p>
<p>While these changes helped us make React better, they don’t affect most React users directly. However, we hope that blogging about them might help other library authors solve similar problems. Our contributors might also find these notes helpful!</p>
<h2 id="formatting-code-with-prettier"><a href="#formatting-code-with-prettier" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Formatting Code with Prettier</h2>
<p>React was one of the first large repositories to <a href="https://github.com/facebook/react/pull/9101">fully embrace</a> opinionated automatic code formatting with <a href="https://prettier.io/">Prettier</a>. Our current Prettier setup consists of:</p>
<ul>
<li>A local <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/package.json#L115"><code class="gatsby-code-text">yarn prettier</code></a> script that <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/prettier/index.js#L71-L77">uses the Prettier Node API</a> to format files in place. We typically run it before committing changes. It is fast because it only checks the <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/shared/listChangedFiles.js#L29-L33">files changed since diverging from remote master</a>.</li>
<li>A script that <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/prettier/index.js#L79-L90">runs Prettier</a> as part of our <a href="https://github.com/facebook/react/blob/d906de7f602df810c38aa622c83023228b047db6/scripts/circleci/test_entry_point.sh#L10">continuous integration checks</a>. It won’t attempt to overwrite the files, but instead will fail the build if any file differs from the Prettier output for that file. This ensures that we can’t merge a pull request unless it has been fully formatted.</li>
</ul>
<p>Some team members have also set up the <a href="https://prettier.io/docs/en/editors.html">editor integrations</a>. Our experience with Prettier has been fantastic, and we recommend it to any team that writes JavaScript.</p>
<h2 id="restructuring-the-monorepo"><a href="#restructuring-the-monorepo" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Restructuring the Monorepo</h2>
<p>Ever since React was split into packages, it has been a <a href="https://danluu.com/monorepo/">monorepo</a>: a set of packages under the umbrella of a single repository. This made it easier to coordinate changes and share the tooling, but our folder structure was deeply nested and difficult to understand. It was not clear which files belonged to which package. After releasing React 16, we’ve decided to completely reorganize the repository structure. Here is how we did it.</p>
<h3 id="migrating-to-yarn-workspaces"><a href="#migrating-to-yarn-workspaces" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Migrating to Yarn Workspaces</h3>
<p>The Yarn package manager <a href="https://yarnpkg.com/blog/2017/08/02/introducing-workspaces/">introduced a feature called Workspaces</a> a few months ago. This feature lets you tell Yarn where your monorepo’s packages are located in the source tree. Every time you run <code class="gatsby-code-text">yarn</code>, in addition to installing your dependencies it also sets up the symlinks that point from your project’s <code class="gatsby-code-text">node_modules</code> to the source folders of your packages.</p>
<p>Thanks to Workspaces, absolute imports between our own packages (such as importing <code class="gatsby-code-text">react</code> from <code class="gatsby-code-text">react-dom</code>) “just work” with any tools that support the Node resolution mechanism. The only problem we encountered was Jest not running the transforms inside the linked packages, but we <a href="https://github.com/facebook/jest/pull/4761">found a fix</a>, and it was merged into Jest.</p>
<p>To enable Yarn Workspaces, we added <code class="gatsby-code-text">&quot;workspaces&quot;: [&quot;packages/*&quot;]</code> to our <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/package.json#L4-L6"><code class="gatsby-code-text">package.json</code></a>, and moved all the code into <a href="https://github.com/facebook/react/tree/cc52e06b490e0dc2482b345aa5d0d65fae931095/packages">top-level <code class="gatsby-code-text">packages/*</code> folders</a>, each with its own <code class="gatsby-code-text">package.json</code> file.</p>
<p>Each package is structured in a similar way. For every public API entry point such as <code class="gatsby-code-text">react-dom</code> or <code class="gatsby-code-text">react-dom/server</code>, there is a <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/packages/react-dom/index.js">file</a> in the package root folder that re-exports the implementation from the <a href="https://github.com/facebook/react/tree/cc52e06b490e0dc2482b345aa5d0d65fae931095/packages/react-dom/src"><code class="gatsby-code-text">/src/</code></a> subfolder. The decision to point entry points to the source rather than to the built versions was intentional. Typically, we re-run a subset of tests after every change during development. Having to build the project to run a test would have been prohibitively slow. When we publish packages to npm, we replace these entry points with files in the <a href="https://github.com/facebook/react/tree/cc52e06b490e0dc2482b345aa5d0d65fae931095/packages/react-dom/npm"><code class="gatsby-code-text">/npm/</code></a> folder that point to the build artifacts.</p>
<p>Not all packages have to be published on npm. For example, we keep some utilities that are tiny enough and can be safely duplicated in a <a href="https://github.com/facebook/react/tree/cc52e06b490e0dc2482b345aa5d0d65fae931095/packages/shared">pseudo-package called <code class="gatsby-code-text">shared</code></a>. Our bundler is configured to <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/rollup/build.js#L326-L329">only treat <code class="gatsby-code-text">dependencies</code> declared from <code class="gatsby-code-text">package.json</code> as externals</a> so it happily bundles the <code class="gatsby-code-text">shared</code> code into <code class="gatsby-code-text">react</code> and <code class="gatsby-code-text">react-dom</code> without leaving any references to <code class="gatsby-code-text">shared/</code> in the build artifacts. So you can use Yarn Workspaces even if you don’t plan to publish actual npm packages!</p>
<h3 id="removing-the-custom-module-system"><a href="#removing-the-custom-module-system" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Removing the Custom Module System</h3>
<p>In the past, we used a non-standard module system called “Haste” that lets you import any file from any other file by its unique <code class="gatsby-code-text">@providesModule</code> directive no matter where it is in the tree. It neatly avoids the problem of deep relative imports with paths like <code class="gatsby-code-text">../../../../</code> and is great for the product code. However, this makes it hard to understand the dependencies between packages. We also had to resort to hacks to make it work with different tools.</p>
<p>We decided to <a href="https://github.com/facebook/react/pull/11303">remove Haste</a> and use the Node resolution with relative imports instead. To avoid the problem of deep relative paths, we have <a href="https://github.com/facebook/react/pull/11304">flattened our repository structure</a> so that it goes at most one level deep inside each package:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-text"><code class="gatsby-code-text">|-react
|  |-npm
|  |-src
|-react-dom
|  |-npm
|  |-src
|  |  |-client
|  |  |-events
|  |  |-server
|  |  |-shared</code></pre>
      </div>
<p>This way, the relative paths can only contain one <code class="gatsby-code-text">./</code> or <code class="gatsby-code-text">../</code> followed by the filename. If one package needs to import something from another package, it can do so with an absolute import from a top-level entry point.</p>
<p>In practice, we still have <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/packages/react-dom/src/client/ReactDOMFiberComponent.js#L10-L11">some cross-package “internal” imports</a> that violate this principle, but they’re explicit, and we plan to gradually get rid of them.</p>
<h2 id="compiling-flat-bundles"><a href="#compiling-flat-bundles" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Compiling Flat Bundles</h2>
<p>Historically, React was distributed in two different formats: as a single-file build that you can add as a <code class="gatsby-code-text">&lt;script&gt;</code> tag in the browser, and as a collection of CommonJS modules that you can bundle with a tool like webpack or Browserify. </p>
<p>Before React 16, each React source file had a corresponding CommonJS module that was published as part of the npm packages. Importing <code class="gatsby-code-text">react</code> or <code class="gatsby-code-text">react-dom</code> led bundlers to the package <a href="https://unpkg.com/react@15/index.js">entry point</a> from which they would build a dependency tree with the CommonJS modules in the <a href="https://unpkg.com/react@15/lib/">internal <code class="gatsby-code-text">lib</code> folder</a>.</p>
<p>However, this approach had multiple disadvantages:</p>
<ul>
<li><strong>It was inconsistent.</strong> Different tools produce bundles of different sizes for identical code importing React, with the difference going as far as 30 kB (before gzip).</li>
<li><strong>It was inefficient for bundler users.</strong> The code produced by most bundlers today contains a lot of “glue code” at the module boundaries. It keeps the modules isolated from each other, but increases the parse time, the bundle size, and the build time.</li>
<li><strong>It was inefficient for Node users.</strong> When running in Node, performing <code class="gatsby-code-text">process.env.NODE_ENV</code> checks before development-only code incurs the overhead of actually looking up environment variables. This slowed down React server rendering. We couldn’t cache it in a variable either because it prevented dead code elimination with Uglify.</li>
<li><strong>It broke encapsulation.</strong> React internals were exposed both in the open source (as <code class="gatsby-code-text">react-dom/lib/*</code> imports) and internally at Facebook. It was convenient at first as a way to share utilities between projects, but with time it became a maintenance burden because renaming or changing argument types of internal functions would break unrelated projects.</li>
<li><strong>It prevented experimentation.</strong> There was no way for the React team to experiment with any advanced compilation techniques. For example, in theory, we might want to apply <a href="https://developers.google.com/closure/compiler/docs/api-tutorial3">Google Closure Compiler Advanced</a> optimizations or <a href="https://prepack.io/">Prepack</a> to some of our code, but they are designed to work on complete bundles rather than small individual modules that we used to ship to npm.</li>
</ul>
<p>Due to these and other issues, we’ve changed the strategy in React 16. We still ship CommonJS modules for Node.js and bundlers, but instead of publishing many individual files in the npm package, we publish just two CommonJS bundles per entry point.</p>
<p>For example, when you import <code class="gatsby-code-text">react</code> with React 16, the bundler <a href="https://unpkg.com/react@16/index.js">finds the entry point</a> that just re-exports one of the two files:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token string">'use strict'</span><span class="token punctuation">;</span>

<span class="token keyword">if</span> <span class="token punctuation">(</span>process<span class="token punctuation">.</span>env<span class="token punctuation">.</span><span class="token constant">NODE_ENV</span> <span class="token operator">===</span> <span class="token string">'production'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./cjs/react.production.min.js'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
  module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./cjs/react.development.js'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>In every package provided by React, the <a href="https://unpkg.com/react@16/cjs/"><code class="gatsby-code-text">cjs</code> folder</a> (short for “CommonJS”) contains a development and a production pre-built bundle for each entry point. </p>
<p>For example, <a href="https://unpkg.com/react@16/cjs/react.development.js"><code class="gatsby-code-text">react.development.js</code></a> is the version intended for development. It is readable and includes comments. On the other hand, <a href="https://unpkg.com/react@16/cjs/react.production.min.js"><code class="gatsby-code-text">react.production.min.js</code></a> was minified and optimized before it was published to npm.</p>
<p>Note how this is essentially the same strategy that we’ve been using for the single-file browser builds (which now reside in the <a href="https://unpkg.com/react@16/umd/"><code class="gatsby-code-text">umd</code> directory</a>, short for <a href="https://www.davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/">Universal Module Definition</a>). Now we just apply the same strategy to the CommonJS builds as well.</p>
<h3 id="migrating-to-rollup"><a href="#migrating-to-rollup" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Migrating to Rollup</h3>
<p>Just compiling CommonJS modules into single-file bundles doesn’t solve all of the above problems. The really significant wins came from <a href="https://github.com/facebook/react/pull/9327">migrating our build system</a> from Browserify to <a href="https://rollupjs.org/">Rollup</a>.</p>
<p><a href="https://medium.com/webpack/webpack-and-rollup-the-same-but-different-a41ad427058c">Rollup was designed with libraries rather than apps in mind</a>, and it is a perfect fit for React’s use case. It solves one problem well: how to combine multiple modules into a flat file with minimal junk code in between. To achieve this, instead of turning modules into functions like many other bundlers, it puts all the code in the same scope, and renames variables so that they don’t conflict. This produces code that is easier for the JavaScript engine to parse, for a human to read, and for a minifier to optimize.</p>
<p>Rollup currently doesn’t support some features that are important to application builders, such as code splitting. However, it does not aim to replace tools like webpack that do a great job at this. Rollup is a perfect fit for <em>libraries</em> like React that can be pre-built and then integrated into apps.</p>
<p>You can find our Rollup build configuration <a href="https://github.com/facebook/react/blob/8ec146c38ee4f4c84b6ecf59f52de3371224e8bd/scripts/rollup/build.js#L336-L362">here</a>, with a <a href="https://github.com/facebook/react/blob/8ec146c38ee4f4c84b6ecf59f52de3371224e8bd/scripts/rollup/build.js#L196-L273">list of plugins we currently use</a>.</p>
<h3 id="migrating-to-google-closure-compiler"><a href="#migrating-to-google-closure-compiler" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Migrating to Google Closure Compiler</h3>
<p>After migrating to flat bundles, we <a href="https://github.com/facebook/react/pull/10236">started</a> using <a href="https://github.com/google/closure-compiler-js">the JavaScript version of the Google Closure Compiler</a> in its “simple” mode. In our experience, even with the advanced optimizations disabled, it still provided a significant advantage over Uglify, as it was able to better eliminate dead code and automatically inline small functions when appropriate.</p>
<p>At first, we could only use Google Closure Compiler for the React bundles we shipped in the open source. At Facebook, we still needed the checked-in bundles to be unminified so we could symbolicate React production crashes with our error reporting tools. We ended up contributing <a href="https://github.com/google/closure-compiler/pull/2707">a flag</a> that completely disables the renaming compiler pass. This lets us apply other optimizations like function inlining, but keep the code fully readable for the Facebook-specific builds of React. To improve the output readability, we <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/rollup/build.js#L249-L250">also format that custom build using Prettier</a>. Interestingly, running Prettier on production bundles while debugging the build process is a great way to find unnecessary code in the bundles!</p>
<p>Currently, all production React bundles <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/rollup/build.js#L235-L248">run through Google Closure Compiler in simple mode</a>, and we may look into enabling advanced optimizations in the future.</p>
<h3 id="protecting-against-weak-dead-code-elimination"><a href="#protecting-against-weak-dead-code-elimination" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Protecting Against Weak Dead Code Elimination</h3>
<p>While we use an efficient <a href="https://en.wikipedia.org/wiki/Dead_code_elimination">dead code elimination</a> solution in React itself, we can’t make a lot of assumptions about the tools used by the React consumers.</p>
<p>Typically, when you <a href="/docs/optimizing-performance.html#use-the-production-build">configure a bundler for production</a>, you need to tell it to substitute <code class="gatsby-code-text">process.env.NODE_ENV</code> with the <code class="gatsby-code-text">&quot;production&quot;</code> string literal. This process is sometimes called “envification”. Consider this code:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">if</span> <span class="token punctuation">(</span>process<span class="token punctuation">.</span>env<span class="token punctuation">.</span><span class="token constant">NODE_ENV</span> <span class="token operator">!==</span> <span class="token string">"production"</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// development-only code</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>After envification, this condition will always be <code class="gatsby-code-text">false</code>, and can be completely eliminated by most minifiers:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">"production"</span> <span class="token operator">!==</span> <span class="token string">"production"</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// development-only code</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>However, if the bundler is misconfigured, you can accidentally ship development code into production. We can’t completely prevent this, but we took a few steps to mitigate the common cases when it happens.</p>
<h4 id="protecting-against-late-envification"><a href="#protecting-against-late-envification" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Protecting Against Late Envification</h4>
<p>As mentioned above, our entry points now look like this:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token string">'use strict'</span><span class="token punctuation">;</span>

<span class="token keyword">if</span> <span class="token punctuation">(</span>process<span class="token punctuation">.</span>env<span class="token punctuation">.</span><span class="token constant">NODE_ENV</span> <span class="token operator">===</span> <span class="token string">'production'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./cjs/react.production.min.js'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
  module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./cjs/react.development.js'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>However, some bundlers process <code class="gatsby-code-text">require</code>s before envification. In this case, even if the <code class="gatsby-code-text">else</code> block never executes, the <code class="gatsby-code-text">cjs/react.development.js</code> file still gets bundled.</p>
<p>To prevent this, we also <a href="https://github.com/facebook/react/blob/d906de7f602df810c38aa622c83023228b047db6/scripts/rollup/wrappers.js#L65-L69">wrap the whole content</a> of the development bundle into another <code class="gatsby-code-text">process.env.NODE_ENV</code> check inside the <code class="gatsby-code-text">cjs/react.development.js</code> bundle itself:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token string">'use strict'</span><span class="token punctuation">;</span>

<span class="token keyword">if</span> <span class="token punctuation">(</span>process<span class="token punctuation">.</span>env<span class="token punctuation">.</span><span class="token constant">NODE_ENV</span> <span class="token operator">!==</span> <span class="token string">"production"</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// bundle code</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>This way, even if the application bundle includes both the development and the production versions of the file, the development version will be empty after envification.</p>
<p>The additional <a href="https://en.wikipedia.org/wiki/Immediately-invoked_function_expression">IIFE</a> wrapper is necessary because some declarations (e.g. functions) can’t be placed inside an <code class="gatsby-code-text">if</code> statement in JavaScript.</p>
<h4 id="detecting-misconfigured-dead-code-elimination"><a href="#detecting-misconfigured-dead-code-elimination" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Detecting Misconfigured Dead Code Elimination</h4>
<p>Even though <a href="https://twitter.com/iamakulov/status/941336777188696066">the situation is changing</a>, many popular bundlers don’t yet force the users to specify the development or production mode. In this case <code class="gatsby-code-text">process.env.NODE_ENV</code> is typically provided by a runtime polyfill, but the dead code elimination doesn’t work.</p>
<p>We can’t completely prevent React users from misconfiguring their bundlers, but we introduced a few additional checks for this in <a href="https://github.com/facebook/react-devtools">React DevTools</a>.</p>
<p>If the development bundle executes, <a href="https://github.com/facebook/react/blob/d906de7f602df810c38aa622c83023228b047db6/packages/react-dom/src/client/ReactDOM.js#L1333-L1335">React DOM reports this to React DevTools</a>:</p>
<br>

  <a class="gatsby-resp-image-link" href="/static/devtools-dev-e434ce2f7e64f63e597edf03f4465694-1e9b4.png" style="display: block" target="_blank" rel="noopener">
  
  <span class="gatsby-resp-image-wrapper" style="position: relative; display: block; ; max-width: 600px; margin-left: auto; margin-right: auto;">
    <span class="gatsby-resp-image-background-image" style="padding-bottom: 33.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAIAAACHqfpvAAAACXBIWXMAABYlAAAWJQFJUiTwAAABHElEQVQY022PX2+CMBTF+f5fai9zUgQs4tS5zGSRFGytSP/CQNhtSRYfdh7uPTlNc343GP7T4888aY7btmX89v36cj7sA62ctNbWWumMkl4u9Lk2xg/bteox/EzT1Pe9Zpc750ESJwghjLM834ZhmOebLMvieIVCtE7TJAXBTlG0+vxIOSus7YQQHmQICCkZpSUpi6IghFDKKLtS0IWWVVWW7vV65UVxJqSqLgyy09cJMKE/MMY4Qs+mAFkJ2I5WK2tdLEQzHwVJ0zRgZvKu64LF4g2vMcBus0O4wDHa4GSXxLstPub4GKEoXC4dfZJGUbR730shJy/XDAdAM/RKYZraiNrUXNdcNXdzv0EofbmATiUVfBjHESYcDM2/zweCOOU28RIAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;">
      <img class="gatsby-resp-image-image" style="width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;" alt="React DevTools on a website with development version of React" title="" src="/static/devtools-dev-e434ce2f7e64f63e597edf03f4465694-1e9b4.png" srcset="/static/devtools-dev-e434ce2f7e64f63e597edf03f4465694-1cd08.png 210w,
/static/devtools-dev-e434ce2f7e64f63e597edf03f4465694-9b07a.png 420w,
/static/devtools-dev-e434ce2f7e64f63e597edf03f4465694-1e9b4.png 600w" sizes="(max-width: 600px) 100vw, 600px">
    </span>
  </span>
  
  </a>
    
<p>There is also one more bad scenario. Sometimes, <code class="gatsby-code-text">process.env.NODE_ENV</code> is set to <code class="gatsby-code-text">&quot;production&quot;</code> at runtime rather than at the build time. This is how it should work in Node.js, but it is bad for the client-side builds because the unnecessary development code is bundled even though it never executes. This is harder to detect but we found a heuristic that works well in most cases and doesn’t seem to produce false positives.</p>
<p>We can write a function that contains a <a href="https://github.com/facebook/react/blob/d906de7f602df810c38aa622c83023228b047db6/packages/react-dom/npm/index.js#L11-L20">development-only branch</a> with an arbitrary string literal. Then, if <code class="gatsby-code-text">process.env.NODE_ENV</code> is set to <code class="gatsby-code-text">&quot;production&quot;</code>, we can <a href="https://github.com/facebook/react-devtools/blob/b370497ba6e873c63479408f11d784095523a630/backend/installGlobalHook.js#L143">call <code class="gatsby-code-text">toString()</code> on that function</a> and verify that the string literal in the development-only has been stripped out. If it is still there, the dead code elimination didn’t work, and we need to warn the developer. Since developers might not notice the React DevTools warnings on a production website, we also <a href="https://github.com/facebook/react-devtools/blob/b370497ba6e873c63479408f11d784095523a630/backend/installGlobalHook.js#L153-L160">throw an error inside <code class="gatsby-code-text">setTimeout</code></a> from React DevTools in the hope that it will be picked up by the error analytics.</p>
<p>We recognize this approach is somewhat fragile. The <code class="gatsby-code-text">toString()</code> method is not reliable and may change its behavior in future browser versions. This is why we put that logic into React DevTools itself rather than into React. This allows us to remove it later if it becomes problematic. We also warn only if we <em>found</em> the special string literal rather than if we <em>didn’t</em> find it. This way, if the <code class="gatsby-code-text">toString()</code> output becomes opaque, or is overridden, the warning just won’t fire.</p>
<h2 id="catching-mistakes-early"><a href="#catching-mistakes-early" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Catching Mistakes Early</h2>
<p>We want to catch bugs as early as possible. However, even with our extensive test coverage, occasionally we make a blunder. We made several changes to our build and test infrastructure this year to make it harder to mess up.</p>
<h3 id="migrating-to-es-modules"><a href="#migrating-to-es-modules" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Migrating to ES Modules</h3>
<p>With the CommonJS <code class="gatsby-code-text">require()</code> and <code class="gatsby-code-text">module.exports</code>, it is easy to import a function that doesn’t really exist, and not realize that until you call it. However, tools like Rollup that natively support <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import"><code class="gatsby-code-text">import</code></a> and <a href="https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export"><code class="gatsby-code-text">export</code></a> syntax fail the build if you mistype a named import. After releasing React 16, <a href="https://github.com/facebook/react/pull/11389">we have converted the entire React source code</a> to the ES Modules syntax.</p>
<p>Not only did this provide some extra protection, but it also helped improve the build size. Many React modules only export utility functions, but CommonJS forced us to wrap them into an object. By turning those utility functions into named exports and eliminating the objects that contained them, we let Rollup place them into the top-level scope, and thus let the minifier mangle their names in the production builds.</p>
<p>For now, have decided to only convert the source code to ES Modules, but not the tests. We use powerful utilities like <code class="gatsby-code-text">jest.resetModules()</code> and want to retain tighter control over when the modules get initialized in tests. In order to consume ES Modules from our tests, we enabled the <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/jest/preprocessor.js#L28-L29">Babel CommonJS transform</a>, but only for the test environment.</p>
<h3 id="running-tests-in-production-mode"><a href="#running-tests-in-production-mode" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Running Tests in Production Mode</h3>
<p>Historically, we’ve been running all tests in a development environment. This let us assert on the warning messages produced by React, and seemed to make general sense. However, even though we try to keep the differences between the development and production code paths minimal, occasionally we would make a mistake in production-only code branches that weren’t covered by tests, and cause an issue at Facebook.</p>
<p>To solve this problem, we have added a new <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/package.json#L110"><code class="gatsby-code-text">yarn test-prod</code></a> command that runs on CI for every pull request, and <a href="https://github.com/facebook/react/pull/11616">executes all React test cases in the production mode</a>. We wrapped any assertions about warning messages into development-only conditional blocks in all tests so that they can still check the rest of the expected behavior in both environments. Since we have a custom Babel transform that replaces production error messages with the <a href="/blog/2016/07/11/introducing-reacts-error-code-system.html">error codes</a>, we also added a <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/jest/setupTests.js#L91-L126">reverse transformation</a> as part of the production test run.</p>
<h3 id="using-public-api-in-tests"><a href="#using-public-api-in-tests" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Using Public API in Tests</h3>
<p>When we were <a href="https://code.facebook.com/posts/1716776591680069/react-16-a-look-inside-an-api-compatible-rewrite-of-our-frontend-ui-library/">rewriting the React reconciler</a>, we recognized the importance of writing tests against the public API instead of internal modules. If the test is written against the public API, it is clear what is being tested from the user’s perspective, and you can run it even if you rewrite the implementation from scratch.</p>
<p>We reached out to the wonderful React community <a href="https://github.com/facebook/react/issues/11299">asking for help</a> converting the remaining tests to use the public API. Almost all of the tests are converted now! The process wasn’t easy. Sometimes a unit test just calls an internal method, and it’s hard to figure out what the observable behavior from user’s point of view was supposed to be tested. We found a few strategies that helped with this. The first thing we would try is to find the git history for when the test was added, and find clues in the issue and pull request description. Often they would contain reproducing cases that ended up being more valuable than the original unit tests! A good way to verify the guess is to try commenting out individual lines in the source code being tested. If the test fails, we know for sure that it stresses the given code path.</p>
<p>We would like to give our deepest thanks to <a href="https://github.com/facebook/react/issues?q=is%3Apr+11299+is%3Aclosed">everyone who contributed to this effort</a>.</p>
<h3 id="running-tests-on-compiled-bundles"><a href="#running-tests-on-compiled-bundles" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Running Tests on Compiled Bundles</h3>
<p>There is also one more benefit to writing tests against the public API: now we can <a href="https://github.com/facebook/react/pull/11633">run them against the compiled bundles</a>.</p>
<p>This helps us ensure that tools like Babel, Rollup, and Google Closure Compiler don’t introduce any regressions. This also opens the door for future more aggressive optimizations, as we can be confident that React still behaves exactly as expected after them.</p>
<p>To implement this, we have created a <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/jest/config.build.js">second Jest config</a>. It overrides our default config but points <code class="gatsby-code-text">react</code>, <code class="gatsby-code-text">react-dom</code>, and other entry points to the <code class="gatsby-code-text">/build/packages/</code> folder. This folder doesn’t contain any React source code, and reflects what gets published to npm. It is populated after you run <code class="gatsby-code-text">yarn build</code>.</p>
<p>This lets us run the same exact tests that we normally run against the source, but execute them using both development and production pre-built React bundles produced with Rollup and Google Closure Compiler.</p>
<p>Unlike the normal test run, the bundle test run depends on the build products so it is not great for quick iteration. However, it still runs on the CI server so if something breaks, the test will display as failed, and we will know it’s not safe to merge into master.</p>
<p>There are still some test files that we intentionally don’t run against the bundles. Sometimes we want to mock an internal module or override a feature flag that isn’t exposed to the public yet. For those cases, we blacklist a test file by renaming it from <code class="gatsby-code-text">MyModule-test.js</code> to <code class="gatsby-code-text">MyModule-test.internal.js</code>.</p>
<p>Currently, over 93% out of 2,650 React tests run against the compiled bundles.</p>
<h3 id="linting-compiled-bundles"><a href="#linting-compiled-bundles" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Linting Compiled Bundles</h3>
<p>In addition to linting our source code, we run a much more limited set of lint rules (really, <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/rollup/validate/eslintrc.cjs.js#L26-L27">just two of them</a>) on the compiled bundles. This gives us an extra layer of protection against regressions in the underlying tools and <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/rollup/validate/eslintrc.cjs.js#L22">ensures</a> that the bundles don’t use any language features that aren’t supported by older browsers.</p>
<h3 id="simulating-package-publishing"><a href="#simulating-package-publishing" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Simulating Package Publishing</h3>
<p>Even running the tests on the built packages is not enough to avoid shipping a broken update. For example, we use the <code class="gatsby-code-text">files</code> field in our <code class="gatsby-code-text">package.json</code> files to specify a whitelist of folders and files that should be published on npm. However, it is easy to add a new entry point to a package but forget to add it to the whitelist. Even the bundle tests would pass, but after publishing the new entry point would be missing.</p>
<p>To avoid situations like this, we are now simulating the npm publish by <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/rollup/packaging.js#L129-L134">running <code class="gatsby-code-text">npm pack</code> and then immediately unpacking the archive</a> after the build. Just like <code class="gatsby-code-text">npm publish</code>, this command filters out anything that isn’t in the <code class="gatsby-code-text">files</code> whitelist. With this approach, if we were to forget adding an entry point to the list, it would be missing in the build folder, and the bundle tests relying on it would fail.</p>
<h3 id="creating-manual-test-fixtures"><a href="#creating-manual-test-fixtures" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Creating Manual Test Fixtures</h3>
<p>Our unit tests run only in the Node environment, but not in the browsers. This was an intentional decision because browser-based testing tools were flaky in our experience, and didn’t catch many issues anyway.</p>
<p>We could get away with this because the code that touches the DOM is consolidated in a few files, and doesn’t change that often. Every week, we update the Facebook.com codebase to the latest React commit on master. At Facebook, we use a set of internal <a href="http://www.seleniumhq.org/projects/webdriver/">WebDriver</a> tests for critical product workflows, and these catch some regressions. React updates are first delivered to employees, so severe bugs get reported immediately before they reach two billion users.</p>
<p>Still, it was hard to review DOM-related changes, and occasionally we would make mistakes. In particular, it was hard to remember all the edge cases that the code had to handle, why they were added, and when it was safe to remove them. We considered adding some automatic tests that run in the browser but we didn’t want to slow down the development cycle and deal with a fragile CI. Additionally, automatic tests don’t always catch DOM issues. For example, an input value displayed by the browser may not match what it reports as a DOM property.</p>
<p>We’ve chatted about this with <a href="https://github.com/aweary">Brandon Dail</a>, <a href="https://github.com/jquense">Jason Quense</a>, and <a href="https://github.com/nhunzaker">Nathan Hunzaker</a>. They were sending substantial patches to React DOM but were frustrated that we failed to review them timely. We decided to give them commit access, but asked them to <a href="https://github.com/facebook/react/pull/8589">create a set of manual tests</a> for DOM-related areas like input management. The initial set of manual fixtures <a href="https://github.com/facebook/react/commits/master/fixtures/dom">kept growing</a> over the year.</p>
<p>These fixtures are implemented as a React app located in <a href="https://github.com/facebook/react/tree/d906de7f602df810c38aa622c83023228b047db6/fixtures/dom"><code class="gatsby-code-text">fixtures/dom</code></a>. Adding a fixture involves writing a React component with a description of the expected behavior, and links to the appropriate issues and browser quirks, like <a href="https://github.com/facebook/react/pull/11760">in this example</a>:</p>
<img src="https://user-images.githubusercontent.com/590904/33555298-dd52fb4e-d8cd-11e7-80e9-8369538eb633.png" style="max-width:100%" alt="DOM fixture example">
<p>The fixture app lets you choose a version of React (local or one of the published versions) which is handy for comparing the behavior before and after the changes. When we change the behavior related to how we interact with the DOM, we can verify that it didn’t regress by going through the related fixtures in different browsers.</p>
<p>In some cases, a change proved to be so complex that it necessitated a standalone purpose-built fixture to verify it. For example, the <a href="/blog/2017/09/08/dom-attributes-in-react-16.html">DOM attribute handling in React 16</a> was very hard to pull off with confidence at first. We kept discovering different edge cases, and almost gave up on doing it in time for the React 16 release. However, then we’ve built an <a href="https://github.com/facebook/react/tree/d906de7f602df810c38aa622c83023228b047db6/fixtures/attribute-behavior">“attribute table” fixture</a> that renders all supported attributes and their misspellings with previous and next version of React, and displays the differences. It took a few iterations (the key insight was to group attributes with similar behavior together) but it ultimately allowed us to fix all major issues in just a few days.</p>
<br>
<blockquote class="twitter-tweet"><p lang="en" dir="ltr">We went through the table to vet the new behavior for every case (and discovered some old bugs too) <a href="https://t.co/cmF2qnK9Q9">pic.twitter.com/cmF2qnK9Q9</a></p>&mdash; Dan Abramov (@dan_abramov) <a href="https://twitter.com/dan_abramov/status/906244378066345984?ref_src=twsrc%5Etfw">September 8, 2017</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
<p>Going through the fixtures is still a lot of work, and we are considering automating some of it. Still, the fixture app is invaluable even as documentation for the existing behavior and all the edge cases and browser bugs that React currently handles. Having it gives us confidence in making significant changes to the logic without breaking important use cases. Another improvement we’re considering is to have a GitHub bot build and deploy the fixtures automatically for every pull request that touches the relevant files so anyone can help with browser testing.</p>
<h3 id="preventing-infinite-loops"><a href="#preventing-infinite-loops" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Preventing Infinite Loops</h3>
<p>The React 16 codebase contains many <code class="gatsby-code-text">while</code> loops. They let us avoid the dreaded deep stack traces that occurred with earlier versions of React, but can make development of React really difficult. Every time there is a mistake in an exit condition our tests would just hang, and it took a while to figure out which of the loops is causing the issue.</p>
<p>Inspired by the <a href="https://repl.it/site/blog/infinite-loops">strategy adopted by Repl.it</a>, we have added a <a href="https://github.com/facebook/react/blob/d906de7f602df810c38aa622c83023228b047db6/scripts/babel/transform-prevent-infinite-loops.js">Babel plugin that prevents infinite loops</a> in the test environment. If some loop continues for more than the maximum allowed number of iterations, we throw an error and immediately fail it so that Jest can display where exactly this happened.</p>
<p>This approach has a pitfall. If an error thrown from the Babel plugin gets caught and ignored up the call stack, the test will pass even though it has an infinite loop. This is really, really bad. To solve this problem, we <a href="https://github.com/facebook/react/blob/d906de7f602df810c38aa622c83023228b047db6/scripts/babel/transform-prevent-infinite-loops.js#L26-L30">set a global field</a> before throwing the error. Then, after every test run, we <a href="https://github.com/facebook/react/blob/d906de7f602df810c38aa622c83023228b047db6/scripts/jest/setupTests.js#L42-L56">rethrow that error if the global field has been set</a>. This way any infinite loop will cause a test failure, no matter whether the error from the Babel plugin was caught or not.</p>
<h2 id="customizing-the-build"><a href="#customizing-the-build" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Customizing the Build</h2>
<p>There were a few things that we had to fine-tune after introducing our new build process. It took us a while to figure them out, but we’re moderately happy with the solutions that we arrived at.</p>
<h3 id="dead-code-elimination"><a href="#dead-code-elimination" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Dead Code Elimination</h3>
<p>The combination of Rollup and Google Closure Compiler already gets us pretty far in terms of stripping development-only code in production bundles. We <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/rollup/build.js#L223-L226">replace</a> the <code class="gatsby-code-text">__DEV__</code> literal with a boolean constant during the build, and both Rollup together and Google Closure Compiler can strip out the <code class="gatsby-code-text">if (false) {}</code> code branches and even some more sophisticated patterns. However, there is one particularly nasty case:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">import</span> warning <span class="token keyword">from</span> <span class="token string">'fbjs/lib/warning'</span><span class="token punctuation">;</span>

<span class="token keyword">if</span> <span class="token punctuation">(</span>__DEV__<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">warning</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token string">'Blimey!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>This pattern is very common in the React source code. However <code class="gatsby-code-text">fbjs/lib/warning</code> is an external import that isn’t being bundled by Rollup for the CommonJS bundle. Therefore, even if <code class="gatsby-code-text">warning()</code> call ends up being removed, Rollup doesn’t know whether it’s safe to remove to the import itself. What if the module performs a side effect during initialization? Then removing it would not be safe.</p>
<p>To solve this problem, we use the <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/rollup/build.js#L338-L340"><code class="gatsby-code-text">treeshake.pureExternalModules</code> Rollup option</a> which takes an array of modules that we can guarantee don’t have side effects. This lets Rollup know that an import to <code class="gatsby-code-text">fbjs/lib/warning</code> is safe to completely strip out if its value is not being used. However, if it <em>is</em> being used (e.g. if we decide to add warnings in production), the import will be preserved. That’s why this approach is safer than replacing modules with empty shims.</p>
<p>When we optimize something, we need to ensure it doesn’t regress in the future. What if somebody introduces a new development-only import of an external module, and not realize they also need to add it to <code class="gatsby-code-text">pureExternalModules</code>? Rollup prints a warning in such cases but we’ve <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/rollup/build.js#L395-L412">decided to fail the build completely</a> instead. This forces the person adding a new external development-only import to <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/rollup/modules.js#L10-L22">explicitly specify whether it has side effects or not</a> every time.</p>
<h3 id="forking-modules"><a href="#forking-modules" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Forking Modules</h3>
<p>In some cases, different bundles need to contain slightly different code. For example, React Native bundles have a different error handling mechanism that shows a redbox instead of printing a message to the console. However, it can be very inconvenient to thread these differences all the way through the calling modules.</p>
<p>Problems like this are often solved with runtime configuration. However, sometimes it is impossible: for example, the React DOM bundles shouldn’t even attempt to import the React Native redbox helpers. It is also unfortunate to bundle the code that never gets used in a particular environment.</p>
<p>Another solution is to use dynamic dependency injection. However, it often produces code that is hard to understand, and may cause cyclical dependencies. It also defies some optimization opportunities.</p>
<p>From the code point of view, ideally we just want to “redirect” a module to its different “forks” for specific bundles. The “forks” have the exact same API as the original modules, but do something different. We found this mental model very intuitive, and <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/rollup/forks.js">created a fork configuration file</a> that specifies how the original modules map to their forks, and the conditions under which this should happen.</p>
<p>For example, this fork config entry specifies different <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/packages/shared/ReactFeatureFlags.js">feature flags</a> for different bundles:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token string">'shared/ReactFeatureFlags'</span><span class="token punctuation">:</span> <span class="token punctuation">(</span>bundleType<span class="token punctuation">,</span> entry<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">switch</span> <span class="token punctuation">(</span>entry<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token string">'react-native-renderer'</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> <span class="token string">'shared/forks/ReactFeatureFlags.native.js'</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token string">'react-cs-renderer'</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> <span class="token string">'shared/forks/ReactFeatureFlags.native-cs.js'</span><span class="token punctuation">;</span>
    <span class="token keyword">default</span><span class="token punctuation">:</span>
      <span class="token keyword">switch</span> <span class="token punctuation">(</span>bundleType<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">case</span> <span class="token constant">FB_DEV</span><span class="token punctuation">:</span>
        <span class="token keyword">case</span> <span class="token constant">FB_PROD</span><span class="token punctuation">:</span>
          <span class="token keyword">return</span> <span class="token string">'shared/forks/ReactFeatureFlags.www.js'</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span></code></pre>
      </div>
<p>During the build, <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/scripts/rollup/plugins/use-forks-plugin.js#L40">our custom Rollup plugin</a> replaces modules with their forks if the conditions have matched. Since both the original modules and the forks are written as ES Modules, Rollup and Google Closure Compiler can inline constants like numbers or booleans, and thus efficiently eliminate dead code for disabled feature flags. In tests, when necessary, we <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/packages/react-cs-renderer/src/__tests__/ReactNativeCS-test.internal.js#L15-L17">use <code class="gatsby-code-text">jest.mock()</code></a> to point the module to the appropriate forked version.</p>
<p>As a bonus, we might want to verify that the export types of the original modules match the export types of the forks exactly. We can use a <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/packages/shared/forks/ReactFeatureFlags.native.js#L32-L36">slightly odd but totally working Flow trick</a> to accomplish this:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">import</span> <span class="token keyword">typeof</span> <span class="token operator">*</span> <span class="token keyword">as</span> FeatureFlagsType <span class="token keyword">from</span> <span class="token string">'shared/ReactFeatureFlags'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token keyword">typeof</span> <span class="token operator">*</span> <span class="token keyword">as</span> FeatureFlagsShimType <span class="token keyword">from</span> <span class="token string">'./ReactFeatureFlags.native'</span><span class="token punctuation">;</span>
type Check<span class="token operator">&lt;</span>_X<span class="token punctuation">,</span> <span class="token constant">Y</span><span class="token punctuation">:</span> _X<span class="token punctuation">,</span> <span class="token constant">X</span><span class="token punctuation">:</span> <span class="token constant">Y</span> <span class="token operator">=</span> _X<span class="token operator">></span> <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">:</span> Check<span class="token operator">&lt;</span>FeatureFlagsShimType<span class="token punctuation">,</span> FeatureFlagsType<span class="token operator">></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
      </div>
<p>This works by essentially forcing Flow to verify that two types are assignable to each other (and thus are equivalent). Now if we modify the exports of either the original module or the fork without changing the other file, the type check will fail. This might be a little goofy but we found this helpful in practice.</p>
<p>To conclude this section, it is important to note that you can’t specify your own module forks if you consume React from npm. This is intentional because none of these files are public API, and they are not covered by the <a href="https://semver.org/">semver</a> guarantees. However, you are always welcome to build React from master or even fork it if you don’t mind the instability and the risk of divergence. We hope that this writeup was still helpful in documenting one possible approach to targeting different environments from a single JavaScript library.</p>
<h3 id="tracking-bundle-size"><a href="#tracking-bundle-size" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Tracking Bundle Size</h3>
<p>As a final build step, we now <a href="https://github.com/facebook/react/blob/d906de7f602df810c38aa622c83023228b047db6/scripts/rollup/build.js#L264-L272">record build sizes for all bundles</a> and write them to a file that <a href="https://github.com/facebook/react/blob/d906de7f602df810c38aa622c83023228b047db6/scripts/rollup/results.json">looks like this</a>. When you run <code class="gatsby-code-text">yarn build</code>, it prints a table with the results:</p>
<br>
<img src="https://user-images.githubusercontent.com/1519870/28427900-80487dbc-6d6f-11e7-828d-1b594bd1ddb5.png" style="max-width:100%" alt="Build results after running GCC">
<p><em>(It doesn’t always look as good as this. This was the commit that migrated React from Uglify to Google Closure Compiler.)</em></p>
<p>Keeping the file sizes committed for everyone to see was helpful for tracking size changes and motivating people to find optimization opportunities.</p>
<p>We haven’t been entirely happy with this strategy because the JSON file often causes merge conflicts on larger branches. Updating it is also not currently enforced so it gets out of date. In the future, we’re considering integrating a bot that would comment on pull requests with the size changes.</p>
<h2 id="simplifying-the-release-process"><a href="#simplifying-the-release-process" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Simplifying the Release Process</h2>
<p>We like to release updates to the open source community often. Unfortunately, the old process of creating a release was slow and would typically take an entire day. After some changes to this process, we’re now able to do a full release in less than an hour. Here’s what we changed.</p>
<h3 id="branching-strategy"><a href="#branching-strategy" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Branching Strategy</h3>
<p>Most of the time spent in the old release process was due to our branching strategy. The <code class="gatsby-code-text">master</code> branch was assumed to be unstable and would often contain breaking changes. Releases were done from a <code class="gatsby-code-text">stable</code> branch, and changes were manually cherry-picked into this branch prior to a release. We had <a href="https://github.com/facebook/react/pull/7330">tooling to help automate</a> some of this process, but it was still <a href="https://github.com/facebook/react/blob/b5a2a1349d6e804d534f673612357c0be7e1d701/scripts/release-manager/Readme.md">pretty complicated to use</a>.</p>
<p>As of version 16, we now release from the <code class="gatsby-code-text">master</code> branch. Experimental features and breaking changes are allowed, but must be hidden behind <a href="https://github.com/facebook/react/blob/cc52e06b490e0dc2482b345aa5d0d65fae931095/packages/shared/ReactFeatureFlags.js">feature flags</a> so they can be removed during the build process. The new flat bundles and dead code elimination make it possible for us to do this without fear of leaking unwanted code into open source builds.</p>
<h3 id="automated-scripts"><a href="#automated-scripts" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Automated Scripts</h3>
<p>After changing to a stable <code class="gatsby-code-text">master</code>, we created a new <a href="https://github.com/facebook/react/issues/10620">release process checklist</a>. Although much simpler than the previous process, this still involved dozens of steps and forgetting one could result in a broken release.</p>
<p>To address this, we created a new <a href="https://github.com/facebook/react/pull/11223">automated release process</a> that is <a href="https://github.com/facebook/react/tree/master/scripts/release#react-release-script">much easier to use</a> and has several built-in checks to ensure that we release a working build. The new process is split into two steps: <em>build</em> and <em>publish</em>. Here’s what it looks like the first time you run it:</p>
<p>
  <a
    class="gatsby-resp-image-link"
    href="/static/release-script-build-overview-adca79c3a0e003b317fe62e68ce9d4a0-a0a5f.png"
    style="display: block"
    target="_blank"
    rel="noopener"
  >
  
  <span
    class="gatsby-resp-image-wrapper"
    style="position: relative; display: block; ; max-width: 822px; margin-left: auto; margin-right: auto;"
  >
    <span
      class="gatsby-resp-image-background-image"
      style="padding-bottom: 45.37712895377128%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAAAsSAAALEgHS3X78AAAA8UlEQVQoz41Ra3OEIAzUKgH1kJeIaMGr19H//w+7Xvuh0+fthJ0Mk90kUAzejyEYa4dhsIAfrfvIq6oq/gaZASH8xJQBk7ZFWRYP4okRgpRmF8W1rZsOl+Ud/4uVUsGPfd9bY3opkTDGHu28bZvW+r0bZMQ5EWHbMyc6D9GvYrS6vpxAMs/zcRy32+u+7zCd4pRyWtcV7njRuq6/imOcQwgYvus60TRt23LOL/LyQ+l3+HHMOePDYCGE4BwkMDNcwEbrZXl2zsUYpZRc8OLzO6I053VZlpRSvgNFUIKxsHM2pzzFuO/nOigJYTLGwA7DvgHuiRQhY2NzFgAAAABJRU5ErkJggg=='); background-size: cover; display: block;"
    >
      <img
        class="gatsby-resp-image-image"
        style="width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;"
        alt="Release Script overview"
        title=""
        src="/static/release-script-build-overview-adca79c3a0e003b317fe62e68ce9d4a0-a0a5f.png"
        srcset="/static/release-script-build-overview-adca79c3a0e003b317fe62e68ce9d4a0-d0a04.png 210w,
/static/release-script-build-overview-adca79c3a0e003b317fe62e68ce9d4a0-c8d6c.png 420w,
/static/release-script-build-overview-adca79c3a0e003b317fe62e68ce9d4a0-a0a5f.png 822w"
        sizes="(max-width: 822px) 100vw, 822px"
      />
    </span>
  </span>
  
  </a>
    </p>
<p>The <em>build</em> step does most of the work- verifying permissions, running tests, and checking CI status. Once it finishes, it prints a reminder to update the CHANGELOG and to verify the bundle using the <a href="#creating-manual-test-fixtures">manual fixtures</a> described above.</p>
<p>
  <a
    class="gatsby-resp-image-link"
    href="/static/release-script-build-confirmation-17f44f353d82a22f6a18707956ad065f-d15b8.png"
    style="display: block"
    target="_blank"
    rel="noopener"
  >
  
  <span
    class="gatsby-resp-image-wrapper"
    style="position: relative; display: block; ; max-width: 840px; margin-left: auto; margin-right: auto;"
  >
    <span
      class="gatsby-resp-image-background-image"
      style="padding-bottom: 95.52407932011332%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAATCAIAAAAf7rriAAAACXBIWXMAAAsSAAALEgHS3X78AAAByUlEQVQ4y5VT2XKcMBDkEAsIJHSLS1jAruPy/39gGrb84EqyOF3DIKpoZnp6SLQ1IYR1ddOkrHXeeSFE0zSccUrp7XZ75uSvUL3e9/u+x7iGrhNZlqVpmvwQ0qtt3eYQ5jlIqZL/gu71GrdpGruua5pWdIIxhjMhxTVZeR3jtiyL772Qh1qIREb/PyA7vcVtnuZxHKVS9QnO+bN+VVWvyEwzP/Taaee4Uh2qojLIKA5+UbxsvlY11bSWtCjIMeksy/M8Pe7HgRDyisxbriTMhsXefEEpheIwnHH+SnzLWsYPeXgVmZ3A2KEWu3HheVXVeA9DQm7aA5h1WYJYoucLMjr0fe+cm+cZGaSXOtNv8VQ7DAOsglpUBj8/Z5WdyPNjZc+nPz7lvR+nyfse6+msC2HBQWBdcClkI8Qg5ITg3XgrZVXrslK06avaJFiMYQSzHwa/LBOl0JuftTNyAD6jIklSNEDOKI7IbzgnQso59Ptuf727jw9vbXWEqaVoKW0vNsxaM474o8O2hc/P++Px7vzyFu/WekLyi92GNK1HIWelo3UPazdj3qpaPU26sGoYuriafTcxKufYskhjmNawnNXn71WW5b+4vwHXuCcp5bYOFgAAAABJRU5ErkJggg=='); background-size: cover; display: block;"
    >
      <img
        class="gatsby-resp-image-image"
        style="width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;"
        alt="Release Script build confirmation screen"
        title=""
        src="/static/release-script-build-confirmation-17f44f353d82a22f6a18707956ad065f-acf85.png"
        srcset="/static/release-script-build-confirmation-17f44f353d82a22f6a18707956ad065f-c1418.png 210w,
/static/release-script-build-confirmation-17f44f353d82a22f6a18707956ad065f-5d5d8.png 420w,
/static/release-script-build-confirmation-17f44f353d82a22f6a18707956ad065f-acf85.png 840w,
/static/release-script-build-confirmation-17f44f353d82a22f6a18707956ad065f-de0cd.png 1260w,
/static/release-script-build-confirmation-17f44f353d82a22f6a18707956ad065f-bd6c2.png 1680w,
/static/release-script-build-confirmation-17f44f353d82a22f6a18707956ad065f-d15b8.png 1765w"
        sizes="(max-width: 840px) 100vw, 840px"
      />
    </span>
  </span>
  
  </a>
    </p>
<p>All that’s left is to tag and publish the release to NPM using the <em>publish</em> script.</p>
<p>
  <a
    class="gatsby-resp-image-link"
    href="/static/release-script-publish-confirmation-a3846d4b3e50d29415846b58612310f2-bd35c.png"
    style="display: block"
    target="_blank"
    rel="noopener"
  >
  
  <span
    class="gatsby-resp-image-wrapper"
    style="position: relative; display: block; ; max-width: 840px; margin-left: auto; margin-right: auto;"
  >
    <span
      class="gatsby-resp-image-background-image"
      style="padding-bottom: 125.12733446519523%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAZCAIAAAC+dZmEAAAACXBIWXMAAAsSAAALEgHS3X78AAACuklEQVQ4y41Ui5KjIBDMywgiYBQQ8AHktbv+wN3/f9m1enuXpLZqt5OiFOkZpqdh0/o2pfM4jimGGEbn/DAMXddJKU8L6rpWSmNkjBWMVVVVFMXxeCzLcsNrEcforFPa2z621oGw3+83P0GhWGNVY5tc2620lHEh+OaHKBwjmuaKME9JlWFmu93uVuz3uwfsl9cnMme8ElWNnzg1teJScs5RD2pby8YrYyVGpbWU4ok/r6hrfKxOFVZAjDXDfsHhcPgi4T9Up5PWes3WzKiFkNWCplHGtHU9a04LikCvUcCRlcQ3QikXvOQlGlIuEEJgO+Un8jzPsuy5Zs7Rw2IBlgopUaHArOBI2LbtWrxctMAayPlAFjOQCx/4gjknlBFzBEopEh4XEILN0aedIzAWyWWslmKlrMolBFZ/02ffdVCp6/rOd2EMfd+HMELjH5kE2oLvnBtDaFvrva+bqM1bo+9CJlp0rIyMx4L1RekZH0o+HjL2lwzTX6+38/l8vd0CjkcK93ucpreP9/D71zBNpu9ReZYTKI3CoQCBCT7tWRTYNlqNnFqbEBxWC7mjFF0kEFlrohUpyyMtcKgYZv43zBijlAohpBSx+RiTUs3ikAZ6P/blC1hrkTal5H3nvO/7obXJutQPbxixrTyX6/94hLEPrw4zbYts3suUVIzNMFrT6taeCQXzRKjKSY1nQprd7tlhqCGA0TQpjff71TqTzt37+wUBs+zwTavaOS0OgNHGzJoZM46xoMX6dfsMTDyRQQMfNOcdri7rHPwym5ES+PIbwaA/mNg2QuAahGYIcbvdpmn6mCZMWme72YUKIyz84jBhFrQW12iCW3DCcXTRUiSfD+xySCjuqKp6dTuyoVuXywU5Qb5erwj00wsQGXB7nJb7BAe7UQrPIUTUggIQuvM+xghbrvo9cv8AdA1GgMm+jmYAAAAASUVORK5CYII='); background-size: cover; display: block;"
    >
      <img
        class="gatsby-resp-image-image"
        style="width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;"
        alt="Release Script publish confirmation screen"
        title=""
        src="/static/release-script-publish-confirmation-a3846d4b3e50d29415846b58612310f2-acf85.png"
        srcset="/static/release-script-publish-confirmation-a3846d4b3e50d29415846b58612310f2-c1418.png 210w,
/static/release-script-publish-confirmation-a3846d4b3e50d29415846b58612310f2-5d5d8.png 420w,
/static/release-script-publish-confirmation-a3846d4b3e50d29415846b58612310f2-acf85.png 840w,
/static/release-script-publish-confirmation-a3846d4b3e50d29415846b58612310f2-bd35c.png 1178w"
        sizes="(max-width: 840px) 100vw, 840px"
      />
    </span>
  </span>
  
  </a>
    </p>
<p>(You may have noticed a <code class="gatsby-code-text">--dry</code> flag in the screenshots above. This flag allows us to run a release, end-to-end, without actually publishing to NPM. This is useful when working on the release script itself.)</p>
<h2 id="in-conclusion"><a href="#in-conclusion" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>In Conclusion</h2>
<p>Did this post inspire you to try some of these ideas in your own projects? We certainly hope so! If you have other ideas about how React build, test, or contribution workflow could be improved, please let us know on <a href="https://github.com/facebook/react/issues">our issue tracker</a>.</p>
<p>You can find the related issues by the <a href="https://github.com/facebook/react/labels/Component%3A%20Build%20Infrastructure">build infrastructure label</a>. These are often great first contribution opportunities!</p>
<h2 id="acknowledgements"><a href="#acknowledgements" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Acknowledgements</h2>
<p>We would like to thank:</p>
<ul>
<li><a href="https://github.com/Rich-Harris">Rich Harris</a> and <a href="https://github.com/lukastaegert">Lukas Taegert</a> for maintaining Rollup and helping us integrate it.</li>
<li><a href="https://github.com/dimvar">Dimitris Vardoulakis</a>, <a href="https://github.com/ChadKillingsworth">Chad Killingsworth</a>, and <a href="https://github.com/MatrixFrog">Tyler Breisacher</a> for their work on Google Closure Compiler and timely advice.</li>
<li><a href="https://github.com/watadarkstar">Adrian Carolli</a>, <a href="https://github.com/rivenhk">Adams Au</a>, <a href="https://github.com/accordeiro">Alex Cordeiro</a>, <a href="https://github.com/HeroProtagonist">Jordan Tepper</a>, <a href="https://github.com/sjy">Johnson Shi</a>, <a href="https://github.com/misoguy">Soo Jae Hwang</a>, <a href="https://github.com/xjlim">Joe Lim</a>, <a href="https://github.com/yu-tian113">Yu Tian</a>, and others for helping prototype and implement some of these and other improvements.</li>
<li><a href="https://github.com/anushreesubramani">Anushree Subramani</a>, <a href="https://github.com/abiduzz420">Abid Uzair</a>, <a href="https://github.com/skiritsis">Sotiris Kiritsis</a>, <a href="https://github.com/timjacobi">Tim Jacobi</a>, <a href="https://github.com/aarboleda1">Anton Arboleda</a>, <a href="https://github.com/jeremenichelli">Jeremias Menichelli</a>, <a href="https://github.com/audyodi">Audy Tanudjaja</a>, <a href="https://github.com/gordyd">Gordon Dent</a>, <a href="https://github.com/enapupe">Iacami Gevaerd
</a>, <a href="https://github.com/sadpandabear">Lucas Lentz</a>, <a href="https://github.com/silvestrijonathan">Jonathan Silvestri</a>, <a href="https://github.com/mjw56">Mike Wilcox</a>, <a href="https://github.com/smaniotto">Bernardo Smaniotto</a>, <a href="https://github.com/douglasgimli">Douglas Gimli</a>, <a href="https://github.com/ethan-arrowood">Ethan Arrowood</a>, and others for their help porting the React test suite to use the public API.</li>
</ul>]]></description><link>https://reactjs.org/blog/2017/12/15/improving-the-repository-infrastructure.html</link><guid isPermaLink="false">https://reactjs.org/blog/2017/12/15/improving-the-repository-infrastructure.html</guid><pubDate>Fri, 15 Dec 2017 00:00:00 GMT</pubDate></item><item><title><![CDATA[Introducing the React RFC Process]]></title><description><![CDATA[<p>We’re adopting an RFC (“request for comments”) process for contributing ideas to React. </p>
<p>Inspired by <a href="https://github.com/yarnpkg/rfcs">Yarn</a>, <a href="https://github.com/emberjs/rfcs">Ember</a>, and <a href="https://github.com/rust-lang/rfcs">Rust</a>, the goal is to allow React core team members and community members to collaborate on the design of new features. It’s also intended to provide a clear path for ideas to enter the project:</p>
<ul>
<li>Create an RFC document detailing your proposal.</li>
<li>Submit a PR to the <a href="https://github.com/reactjs/rfcs">RFC repository</a>.</li>
<li>Incorporate feedback into the proposal.</li>
<li>After discussion, the core team may or may not accept the RFC.</li>
<li>If the RFC is accepted, the PR is merged.</li>
</ul>
<p>RFCs are accepted when they are approved for implementation in React. A more thorough description of the process is available in the repository’s <a href="https://github.com/reactjs/rfcs/blob/master/README.md">README</a>. The exact details may be refined in the future.</p>
<h2 id="who-can-submit-rfcs"><a href="#who-can-submit-rfcs" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Who Can Submit RFCs?</h2>
<p>Anyone! No knowledge of React’s internals is required, nor are you expected to implement the proposal yourself.</p>
<p>As with our other repositories, we do ask that you complete a <a href="https://github.com/reactjs/rfcs#contributor-license-agreement-cla">Contributor License Agreement</a> before we can accept your PR.</p>
<h2 id="what-types-of-changes-should-be-submitted-as-rfcs"><a href="#what-types-of-changes-should-be-submitted-as-rfcs" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>What Types of Changes Should Be Submitted As RFCs?</h2>
<p>Generally, any idea that would benefit from additional review or design before being implemented is a good candidate for an RFC. As a rule of thumb, this means any proposal that adds, changes, or removes a React API.</p>
<p>Not every change must go through the RFC process. Bug fixes or performance improvements that don’t touch the API can be submitted directly to the main library.</p>
<p>We now have several repositories where you can submit contributions to React:</p>
<ul>
<li><strong>Issues, bugfixes, and code changes to the main library</strong>: <a href="https://github.com/facebook/react">facebook/react</a></li>
<li><strong>Website and documentation</strong>: <a href="https://github.com/reactjs/reactjs.org">reactjs/reactjs.org</a></li>
<li><strong>Ideas for changes that need additional review before being implemented</strong>: <a href="https://github.com/reactjs/rfcs">reactjs/rfcs</a></li>
</ul>
<h2 id="rfc-for-a-new-context-api"><a href="#rfc-for-a-new-context-api" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>RFC for A New Context API</h2>
<p>Coinciding with the launch of our RFC process, we’ve submitted a <a href="https://github.com/reactjs/rfcs/pull/2">proposal for a new version of context</a>. The proposal has already received many valuable comments from the community that we will incorporate into the design of the new API.</p>
<p>The context PR is a good example of how a typical RFC should be structured. We’re excited to start receiving your proposals!</p>]]></description><link>https://reactjs.org/blog/2017/12/07/introducing-the-react-rfc-process.html</link><guid isPermaLink="false">https://reactjs.org/blog/2017/12/07/introducing-the-react-rfc-process.html</guid><pubDate>Thu, 07 Dec 2017 00:00:00 GMT</pubDate></item><item><title><![CDATA[React v16.2.0: Improved Support for Fragments]]></title><description><![CDATA[<p>React 16.2 is now available! The biggest addition is improved support for returning multiple children from a component’s render method. We call this feature <em>fragments</em>:</p>
<p>Fragments look like empty JSX tags. They let you group a list of children without adding extra nodes to the DOM:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">(</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span></span><span class="token punctuation">></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ChildA</span> <span class="token punctuation">/></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ChildB</span> <span class="token punctuation">/></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ChildC</span> <span class="token punctuation">/></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span></span><span class="token punctuation">></span></span>
  <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>This exciting new feature is made possible by additions to both React and JSX.</p>
<h2 id="what-are-fragments"><a href="#what-are-fragments" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>What Are Fragments?</h2>
<p>A common pattern is for a component to return a list of children. Take this example HTML:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-html"><code class="gatsby-code-html">Some text.
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">></span></span>A heading<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">></span></span>
More text.
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">></span></span>Another heading<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">></span></span>
Even more text.</code></pre>
      </div>
<p>Prior to version 16, the only way to achieve this in React was by wrapping the children in an extra element, usually a <code class="gatsby-code-text">div</code> or <code class="gatsby-code-text">span</code>:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">(</span>
    <span class="token comment">// Extraneous div element :(</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">></span></span>
      Some text<span class="token punctuation">.</span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">></span></span><span class="token constant">A</span> heading<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">></span></span>
      More text<span class="token punctuation">.</span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">></span></span>Another heading<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">></span></span>
      Even more text<span class="token punctuation">.</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
  <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>To address this limitation, React 16.0 added support for <a href="/blog/2017/09/26/react-v16.0.html#new-render-return-types-fragments-and-strings">returning an array of elements from a component’s <code class="gatsby-code-text">render</code> method</a>. Instead of wrapping the children in a DOM element, you can put them into an array:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
 <span class="token keyword">return</span> <span class="token punctuation">[</span>
  <span class="token string">"Some text."</span><span class="token punctuation">,</span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>heading-1<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token constant">A</span> heading<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">></span></span><span class="token punctuation">,</span>
  <span class="token string">"More text."</span><span class="token punctuation">,</span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>heading-2<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Another heading<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">></span></span><span class="token punctuation">,</span>
  <span class="token string">"Even more text."</span>
 <span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>However, this has some confusing differences from normal JSX:</p>
<ul>
<li>Children in an array must be separated by commas.</li>
<li>Children in an array must have a key to prevent React’s <a href="/docs/lists-and-keys.html#keys">key warning</a>.</li>
<li>Strings must be wrapped in quotes.</li>
</ul>
<p>To provide a more consistent authoring experience for fragments, React now provides a first-class <code class="gatsby-code-text">Fragment</code> component that can be used in place of arrays.</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">(</span>
<span class="gatsby-highlight-code-line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Fragment</span><span class="token punctuation">></span></span>
</span>      Some text<span class="token punctuation">.</span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">></span></span><span class="token constant">A</span> heading<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">></span></span>
      More text<span class="token punctuation">.</span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">></span></span>Another heading<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">></span></span>
      Even more text<span class="token punctuation">.</span>
<span class="gatsby-highlight-code-line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>Fragment</span><span class="token punctuation">></span></span>
</span>  <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>You can use <code class="gatsby-code-text">&lt;Fragment /&gt;</code> the same way you’d use any other element, without changing the way you write JSX. No commas, no keys, no quotes.</p>
<p>The Fragment component is available on the main React object:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">const</span> Fragment <span class="token operator">=</span> React<span class="token punctuation">.</span>Fragment<span class="token punctuation">;</span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Fragment</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ChildA</span> <span class="token punctuation">/></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ChildB</span> <span class="token punctuation">/></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ChildC</span> <span class="token punctuation">/></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>Fragment</span><span class="token punctuation">></span></span>

<span class="token comment">// This also works</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>React.Fragment</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ChildA</span> <span class="token punctuation">/></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ChildB</span> <span class="token punctuation">/></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ChildC</span> <span class="token punctuation">/></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>React.Fragment</span><span class="token punctuation">></span></span></code></pre>
      </div>
<h2 id="jsx-fragment-syntax"><a href="#jsx-fragment-syntax" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>JSX Fragment Syntax</h2>
<p>Fragments are a common pattern in our codebases at Facebook. We anticipate they’ll be widely adopted by other teams, too. To make the authoring experience as convenient as possible, we’re adding syntactical support for fragments to JSX:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">(</span>
<span class="gatsby-highlight-code-line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span></span><span class="token punctuation">></span></span>
</span>      Some text<span class="token punctuation">.</span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">></span></span><span class="token constant">A</span> heading<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">></span></span>
      More text<span class="token punctuation">.</span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">></span></span>Another heading<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">></span></span>
      Even more text<span class="token punctuation">.</span>
<span class="gatsby-highlight-code-line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span></span><span class="token punctuation">></span></span>
</span>  <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>In React, this desugars to a <code class="gatsby-code-text">&lt;React.Fragment/&gt;</code> element, as in the example from the previous section. (Non-React frameworks that use JSX may compile to something different.)</p>
<p>Fragment syntax in JSX was inspired by prior art such as the <code class="gatsby-code-text">XMLList() &lt;&gt;&lt;/&gt;</code> constructor in <a href="https://developer.mozilla.org/en-US/docs/Archive/Web/E4X/E4X_for_templating">E4X</a>. Using a pair of empty tags is meant to represent the idea it won’t add an actual element to the DOM.</p>
<h3 id="keyed-fragments"><a href="#keyed-fragments" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Keyed Fragments</h3>
<p>Note that the <code class="gatsby-code-text">&lt;&gt;&lt;/&gt;</code> syntax does not accept attributes, including keys.</p>
<p>If you need a keyed fragment, you can use <code class="gatsby-code-text">&lt;Fragment /&gt;</code> directly. A use case for this is mapping a collection to an array of fragments — for example, to create a description list:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">function</span> <span class="token function">Glossary</span><span class="token punctuation">(</span>props<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">(</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dl</span><span class="token punctuation">></span></span>
      <span class="token punctuation">{</span>props<span class="token punctuation">.</span>items<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>item <span class="token operator">=></span> <span class="token punctuation">(</span>
        <span class="token comment">// Without the `key`, React will fire a key warning</span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Fragment</span> <span class="token attr-name">key</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>item<span class="token punctuation">.</span>id<span class="token punctuation">}</span></span><span class="token punctuation">></span></span>
          <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dt</span><span class="token punctuation">></span></span><span class="token punctuation">{</span>item<span class="token punctuation">.</span>term<span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dt</span><span class="token punctuation">></span></span>
          <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dd</span><span class="token punctuation">></span></span><span class="token punctuation">{</span>item<span class="token punctuation">.</span>description<span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dd</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>Fragment</span><span class="token punctuation">></span></span>
      <span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">}</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dl</span><span class="token punctuation">></span></span>
  <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p><code class="gatsby-code-text">key</code> is the only attribute that can be passed to <code class="gatsby-code-text">Fragment</code>. In the future, we may add support for additional attributes, such as event handlers.</p>
<h3 id="live-demo"><a href="#live-demo" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Live Demo</h3>
<p>You can experiment with JSX fragment syntax with this <a href="https://codepen.io/reactjs/pen/VrEbjE?editors=1000">CodePen</a>.</p>
<h2 id="support-for-fragment-syntax"><a href="#support-for-fragment-syntax" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Support for Fragment Syntax</h2>
<p>Support for fragment syntax in JSX will vary depending on the tools you use to build your app. Please be patient as the JSX community works to adopt the new syntax. We’ve been working closely with maintainers of the most popular projects:</p>
<h3 id="create-react-app"><a href="#create-react-app" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Create React App</h3>
<p>Experimental support for fragment syntax will be added to Create React App within the next few days. A stable release may take a bit longer as we await adoption by upstream projects.</p>
<h3 id="babel"><a href="#babel" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Babel</h3>
<p>Support for JSX fragments is available in <a href="https://github.com/babel/babel/releases/tag/v7.0.0-beta.31">Babel v7.0.0-beta.31</a> and above! If you are already on Babel 7, simply update to the latest Babel and plugin transform:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-bash"><code class="gatsby-code-bash"><span class="token comment"># for yarn users</span>
yarn upgrade @babel/core @babel/plugin-transform-react-jsx
<span class="token comment"># for npm users</span>
<span class="token function">npm</span> update @babel/core @babel/plugin-transform-react-jsx</code></pre>
      </div>
<p>Or if you are using the <a href="https://www.npmjs.com/package/@babel/preset-react">react preset</a>:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-bash"><code class="gatsby-code-bash"><span class="token comment"># for yarn users</span>
yarn upgrade @babel/core @babel/preset-react
<span class="token comment"># for npm users</span>
<span class="token function">npm</span> update @babel/core @babel/preset-react</code></pre>
      </div>
<p>Note that Babel 7 is technically still in beta, but a <a href="https://babeljs.io/blog/2017/09/12/planning-for-7.0">stable release is coming soon</a>.</p>
<p>Unfortunately, support for Babel 6.x is not available, and there are currently no plans to backport.</p>
<h4 id="babel-with-webpack-babel-loader"><a href="#babel-with-webpack-babel-loader" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Babel with Webpack (babel-loader)</h4>
<p>If you are using Babel with <a href="https://webpack.js.org/">Webpack</a>, no additional steps are needed because <a href="https://github.com/babel/babel-loader">babel-loader</a> will use your peer-installed version of Babel.</p>
<h4 id="babel-with-other-frameworks"><a href="#babel-with-other-frameworks" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Babel with Other Frameworks</h4>
<p>If you use JSX with a non-React framework like Inferno or Preact, there is a <a href="https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-react-jsx#pragmafrag">pragma option available in babel-plugin-transform-react-jsx</a> that configures the Babel compiler to de-sugar the <code class="gatsby-code-text">&lt;&gt;&lt;/&gt;</code> syntax to a custom identifier.</p>
<h3 id="typescript"><a href="#typescript" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>TypeScript</h3>
<p>TypeScript has full support for fragment syntax! Please upgrade to <a href="https://github.com/Microsoft/TypeScript/releases/tag/v2.6.2">version 2.6.2</a>. (Note that this is important even if you are already on version 2.6.1, since support was added as patch release in 2.6.2.)</p>
<p>Upgrade to the latest TypeScript with the command:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-bash"><code class="gatsby-code-bash"><span class="token comment"># for yarn users</span>
yarn upgrade typescript
<span class="token comment"># for npm users</span>
<span class="token function">npm</span> update typescript</code></pre>
      </div>
<h3 id="flow"><a href="#flow" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Flow</h3>
<p><a href="https://flow.org/">Flow</a> support for JSX fragments is available starting in <a href="https://github.com/facebook/flow/releases/tag/v0.59.0">version 0.59</a>! Simply run</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-bash"><code class="gatsby-code-bash"><span class="token comment"># for yarn users</span>
yarn upgrade flow-bin
<span class="token comment"># for npm users</span>
<span class="token function">npm</span> update flow-bin</code></pre>
      </div>
<p>to update Flow to the latest version.</p>
<h3 id="prettier"><a href="#prettier" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Prettier</h3>
<p><a href="https://github.com/prettier/prettier">Prettier</a> added support for fragments in their <a href="https://prettier.io/blog/2017/12/05/1.9.0.html#jsx-fragment-syntax-3237-https-githubcom-prettier-prettier-pull-3237-by-duailibe-https-githubcom-duailibe">1.9 release</a>.</p>
<h3 id="eslint"><a href="#eslint" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>ESLint</h3>
<p>JSX Fragments are supported by <a href="https://eslint.org/">ESLint</a> 3.x when it is used together with <a href="https://github.com/babel/babel-eslint">babel-eslint</a>:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-bash"><code class="gatsby-code-bash"><span class="token comment"># for yarn users</span>
yarn add eslint@3.x babel-eslint@7
<span class="token comment"># for npm users</span>
<span class="token function">npm</span> <span class="token function">install</span> eslint@3.x babel-eslint@7</code></pre>
      </div>
<p>or if you already have it, then upgrade:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-bash"><code class="gatsby-code-bash"><span class="token comment"># for yarn users</span>
yarn upgrade eslint@3.x babel-eslint@7
<span class="token comment"># for npm users</span>
<span class="token function">npm</span> update eslint@3.x babel-eslint@7</code></pre>
      </div>
<p>Ensure you have the following line inside your <code class="gatsby-code-text">.eslintrc</code>:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsxon"><code class="gatsby-code-jsxon">&quot;parser&quot;: &quot;babel-eslint&quot;</code></pre>
      </div>
<p>That’s it!</p>
<p>Note that <code class="gatsby-code-text">babel-eslint</code> is not officially supported by ESLint. We’ll be looking into adding support for fragments to ESLint 4.x itself in the coming weeks (see <a href="https://github.com/eslint/eslint/issues/9662">issue #9662</a>).</p>
<h3 id="editor-support"><a href="#editor-support" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Editor Support</h3>
<p>It may take a while for fragment syntax to be supported in your text editor. Please be patient as the community works to adopt the latest changes. In the meantime, you may see errors or inconsistent highlighting if your editor does not yet support fragment syntax. Generally, these errors can be safely ignored.</p>
<h4 id="typescript-editor-support"><a href="#typescript-editor-support" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>TypeScript Editor Support</h4>
<p>If you’re a TypeScript user — great news! Editor support for JSX fragments is already available in <a href="https://www.microsoft.com/en-us/download/details.aspx?id=48593">Visual Studio 2015</a>, <a href="https://www.microsoft.com/en-us/download/details.aspx?id=55258">Visual Studio 2017</a>, <a href="https://code.visualstudio.com/updates/v1_19#_jsx-fragment-syntax">Visual Studio Code</a> and <a href="https://packagecontrol.io/packages/TypeScript">Sublime Text via Package Control</a>.</p>
<h3 id="other-tools"><a href="#other-tools" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Other Tools</h3>
<p>For other tools, please check with the corresponding documentation to check if there is support available. However, if you’re blocked by your tooling, you can always start with using the <code class="gatsby-code-text">&lt;Fragment&gt;</code> component and perform a codemod later to replace it with the shorthand syntax when the appropriate support is available.</p>
<h2 id="installation"><a href="#installation" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Installation</h2>
<p>React v16.2.0 is available on the npm registry.</p>
<p>To install React 16 with Yarn, run:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-bash"><code class="gatsby-code-bash">yarn add react@^16.2.0 react-dom@^16.2.0</code></pre>
      </div>
<p>To install React 16 with npm, run:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-bash"><code class="gatsby-code-bash"><span class="token function">npm</span> <span class="token function">install</span> --save react@^16.2.0 react-dom@^16.2.0</code></pre>
      </div>
<p>We also provide UMD builds of React via a CDN:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-html"><code class="gatsby-code-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">crossorigin</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>https://unpkg.com/react@16/umd/react.production.min.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script language-javascript"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">crossorigin</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>https://unpkg.com/react-dom@16/umd/react-dom.production.min.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script language-javascript"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span></code></pre>
      </div>
<p>Refer to the documentation for <a href="/docs/installation.html">detailed installation instructions</a>.</p>
<h2 id="changelog"><a href="#changelog" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Changelog</h2>
<h3 id="react"><a href="#react" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>React</h3>
<ul>
<li>Add <code class="gatsby-code-text">Fragment</code> as named export to React. (<a href="https://github.com/clemmy">@clemmy</a> in <a href="https://github.com/facebook/react/pull/10783">#10783</a>)</li>
<li>Support experimental Call/Return types in <code class="gatsby-code-text">React.Children</code> utilities. (<a href="https://github.com/MatteoVH">@MatteoVH</a> in <a href="https://github.com/facebook/react/pull/11422">#11422</a>)</li>
</ul>
<h3 id="react-dom"><a href="#react-dom" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>React DOM</h3>
<ul>
<li>Fix radio buttons not getting checked when using multiple lists of radios. (<a href="https://github.com/landvibe">@landvibe</a> in <a href="https://github.com/facebook/react/pull/11227">#11227</a>)</li>
<li>Fix radio buttons not receiving the <code class="gatsby-code-text">onChange</code> event in some cases. (<a href="https://github.com/jquense">@jquense</a> in <a href="https://github.com/facebook/react/pull/11028">#11028</a>)</li>
</ul>
<h3 id="react-test-renderer"><a href="#react-test-renderer" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>React Test Renderer</h3>
<ul>
<li>Fix <code class="gatsby-code-text">setState()</code> callback firing too early when called from <code class="gatsby-code-text">componentWillMount</code>. (<a href="https://github.com/accordeiro">@accordeiro</a> in <a href="https://github.com/facebook/react/pull/11507">#11507</a>)</li>
</ul>
<h3 id="react-reconciler"><a href="#react-reconciler" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>React Reconciler</h3>
<ul>
<li>Expose <code class="gatsby-code-text">react-reconciler/reflection</code> with utilities useful to custom renderers. (<a href="https://github.com/rivenhk">@rivenhk</a> in <a href="https://github.com/facebook/react/pull/11683">#11683</a>)</li>
</ul>
<h3 id="internal-changes"><a href="#internal-changes" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Internal Changes</h3>
<ul>
<li>Many tests were rewritten against the public API. Big thanks to <a href="https://github.com/facebook/react/issues/11299">everyone who contributed</a>!</li>
</ul>
<h2 id="acknowledgments"><a href="#acknowledgments" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Acknowledgments</h2>
<p>This release was made possible by our open source contributors. A big thanks to everyone who filed issues, contributed to syntax discussions, reviewed pull requests, added support for JSX fragments in third party libraries, and more!</p>
<p>Special thanks to the <a href="https://www.typescriptlang.org/">TypeScript</a> and <a href="https://flow.org/">Flow</a> teams, as well as the <a href="https://babeljs.io/">Babel</a> maintainers, who helped make tooling support for the new syntax go seamlessly.</p>
<p>Thanks to <a href="https://github.com/gajus/">Gajus Kuizinas</a> and other contributors who prototyped the <code class="gatsby-code-text">Fragment</code> component in open source.</p>]]></description><link>https://reactjs.org/blog/2017/11/28/react-v16.2.0-fragment-support.html</link><guid isPermaLink="false">https://reactjs.org/blog/2017/11/28/react-v16.2.0-fragment-support.html</guid><pubDate>Tue, 28 Nov 2017 00:00:00 GMT</pubDate></item><item><title><![CDATA[React v16.0]]></title><description><![CDATA[<p>We’re excited to announce the release of React v16.0! Among the changes are some long-standing feature requests, including <a href="#new-render-return-types-fragments-and-strings"><strong>fragments</strong></a>, <a href="#better-error-handling"><strong>error boundaries</strong></a>, <a href="#portals"><strong>portals</strong></a>, support for <a href="#support-for-custom-dom-attributes"><strong>custom DOM attributes</strong></a>, improved <a href="#better-server-side-rendering"><strong>server-side rendering</strong></a>, and <a href="#reduced-file-size"><strong>reduced file size</strong></a>.</p>
<h3 id="new-render-return-types-fragments-and-strings"><a href="#new-render-return-types-fragments-and-strings" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>New render return types: fragments and strings</h3>
<p>You can now return an array of elements from a component’s <code class="gatsby-code-text">render</code> method. Like with other arrays, you’ll need to add a key to each element to avoid the key warning:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// No need to wrap list items in an extra element!</span>
  <span class="token keyword">return</span> <span class="token punctuation">[</span>
    <span class="token comment">// Don't forget the keys :)</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>A<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>First item<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span><span class="token punctuation">,</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>B<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Second item<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span><span class="token punctuation">,</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>C<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Third item<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span><span class="token punctuation">,</span>
  <span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p><a href="/blog/2017/11/28/react-v16.2.0-fragment-support.html">Starting with React 16.2.0</a>, we are adding support for a special fragment syntax to JSX that doesn’t require keys.</p>
<p>We’ve added support for returning strings, too:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token string">'Look ma, no spans!'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p><a href="/docs/react-component.html#render">See the full list of supported return types</a>.</p>
<h3 id="better-error-handling"><a href="#better-error-handling" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Better error handling</h3>
<p>Previously, runtime errors during rendering could put React in a broken state, producing cryptic error messages and requiring a page refresh to recover. To address this problem, React 16 uses a more resilient error-handling strategy. By default, if an error is thrown inside a component’s render or lifecycle methods, the whole component tree is unmounted from the root. This prevents the display of corrupted data. However, it’s probably not the ideal user experience.</p>
<p>Instead of unmounting the whole app every time there’s an error, you can use error boundaries. Error boundaries are special components that capture errors inside their subtree and display a fallback UI in its place. Think of error boundaries like try-catch statements, but for React components.</p>
<p>For more details, check out our <a href="/blog/2017/07/26/error-handling-in-react-16.html">previous post on error handling in React 16</a>.</p>
<h3 id="portals"><a href="#portals" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Portals</h3>
<p>Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// React does *not* create a new div. It renders the children into `domNode`.</span>
  <span class="token comment">// `domNode` is any valid DOM node, regardless of its location in the DOM.</span>
  <span class="token keyword">return</span> ReactDOM<span class="token punctuation">.</span><span class="token function">createPortal</span><span class="token punctuation">(</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>children<span class="token punctuation">,</span>
    domNode<span class="token punctuation">,</span>
  <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
      </div>
<p>See a full example in the <a href="/docs/portals.html">documentation for portals</a>.</p>
<h3 id="better-server-side-rendering"><a href="#better-server-side-rendering" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Better server-side rendering</h3>
<p>React 16 includes a completely rewritten server renderer. It’s really fast. It supports <strong>streaming</strong>, so you can start sending bytes to the client faster. And thanks to a <a href="#reduced-file-size">new packaging strategy</a> that compiles away <code class="gatsby-code-text">process.env</code> checks (Believe it or not, reading <code class="gatsby-code-text">process.env</code> in Node is really slow!), you no longer need to bundle React to get good server-rendering performance.</p>
<p>Core team member Sasha Aickin wrote a <a href="https://medium.com/@aickin/whats-new-with-server-side-rendering-in-react-16-9b0d78585d67">great article describing React 16’s SSR improvements</a>. According to Sasha’s synthetic benchmarks, server rendering in React 16 is roughly <strong>three times faster</strong> than React 15. “When comparing against React 15 with <code class="gatsby-code-text">process.env</code> compiled out, there’s about a 2.4x improvement in Node 4, about a 3x performance improvement in Node 6, and a full 3.8x improvement in the new Node 8.4 release. And if you compare against React 15 without compilation, React 16 has a full order of magnitude gain in SSR in the latest version of Node!” (As Sasha points out, please be aware that these numbers are based on synthetic benchmarks and may not reflect real-world performance.)</p>
<p>In addition, React 16 is better at hydrating server-rendered HTML once it reaches the client. It no longer requires the initial render to exactly match the result from the server. Instead, it will attempt to reuse as much of the existing DOM as possible. No more checksums! In general, we don’t recommend that you render different content on the client versus the server, but it can be useful in some cases (e.g. timestamps). <strong>However, it’s dangerous to have missing nodes on the server render as this might cause sibling nodes to be created with incorrect attributes.</strong></p>
<p>See the <a href="/docs/react-dom-server.html">documentation for <code class="gatsby-code-text">ReactDOMServer</code></a> for more details.</p>
<h3 id="support-for-custom-dom-attributes"><a href="#support-for-custom-dom-attributes" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Support for custom DOM attributes</h3>
<p>Instead of ignoring unrecognized HTML and SVG attributes, React will now <a href="/blog/2017/09/08/dom-attributes-in-react-16.html">pass them through to the DOM</a>. This has the added benefit of allowing us to get rid of most of React’s attribute whitelist, resulting in reduced file sizes.</p>
<h3 id="reduced-file-size"><a href="#reduced-file-size" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Reduced file size</h3>
<p>Despite all these additions, React 16 is actually <strong>smaller</strong> compared to 15.6.1!</p>
<ul>
<li><code class="gatsby-code-text">react</code> is 5.3 kb (2.2 kb gzipped), down from 20.7 kb (6.9 kb gzipped).</li>
<li><code class="gatsby-code-text">react-dom</code> is 103.7 kb (32.6 kb gzipped), down from 141 kb (42.9 kb gzipped).</li>
<li><code class="gatsby-code-text">react</code> + <code class="gatsby-code-text">react-dom</code> is 109 kb (34.8 kb gzipped), down from 161.7 kb (49.8 kb gzipped).</li>
</ul>
<p>That amounts to a combined <strong>32% size decrease compared to the previous version (30% post-gzip)</strong>.</p>
<p>The size difference is partly attributable to a change in packaging. React now uses <a href="https://rollupjs.org/">Rollup</a> to create flat bundles for each of its different target formats, resulting in both size and runtime performance wins. The flat bundle format also means that React’s impact on bundle size is roughly consistent regardless of how you ship your app, whether it’s with Webpack, Browserify, the pre-built UMD bundles, or any other system.</p>
<h3 id="mit-licensed"><a href="#mit-licensed" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>MIT licensed</h3>
<p><a href="https://code.facebook.com/posts/300798627056246/relicensing-react-jest-flow-and-immutable-js/">In case you missed it</a>, React 16 is available under the MIT license. We’ve also published React 15.6.2 under MIT, for those who are unable to upgrade immediately.</p>
<h3 id="new-core-architecture"><a href="#new-core-architecture" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>New core architecture</h3>
<p>React 16 is the first version of React built on top of a new core architecture, codenamed “Fiber.” You can read all about this project over on <a href="https://code.facebook.com/posts/1716776591680069/react-16-a-look-inside-an-api-compatible-rewrite-of-our-frontend-ui-library/">Facebook’s engineering blog</a>. (Spoiler: we rewrote React!)</p>
<p>Fiber is responsible for most of the new features in React 16, like error boundaries and fragments. Over the next few releases, you can expect more new features as we begin to unlock the full potential of React.</p>
<p>Perhaps the most exciting area we’re working on is <strong>async rendering</strong>—a strategy for cooperatively scheduling rendering work by periodically yielding execution to the browser. The upshot is that, with async rendering, apps are more responsive because React avoids blocking the main thread.</p>
<p>This demo provides an early peek at the types of problems async rendering can solve:</p>
<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">Ever wonder what &quot;async rendering&quot; means? Here&#39;s a demo of how to coordinate an async React tree with non-React work <a href="https://t.co/3snoahB3uV">https://t.co/3snoahB3uV</a> <a href="https://t.co/egQ988gBjR">pic.twitter.com/egQ988gBjR</a></p>&mdash; Andrew Clark (@acdlite) <a href="https://twitter.com/acdlite/status/909926793536094209">September 18, 2017</a></blockquote>
<p><em>Tip: Pay attention to the spinning black square.</em></p>
<p>We think async rendering is a big deal, and represents the future of React. To make migration to v16.0 as smooth as possible, we’re not enabling any async features yet, but we’re excited to start rolling them out in the coming months. Stay tuned!</p>
<h2 id="installation"><a href="#installation" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Installation</h2>
<p>React v16.0.0 is available on the npm registry.</p>
<p>To install React 16 with Yarn, run:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-bash"><code class="gatsby-code-bash">yarn add react@^16.0.0 react-dom@^16.0.0</code></pre>
      </div>
<p>To install React 16 with npm, run:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-bash"><code class="gatsby-code-bash"><span class="token function">npm</span> <span class="token function">install</span> --save react@^16.0.0 react-dom@^16.0.0</code></pre>
      </div>
<p>We also provide UMD builds of React via a CDN:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-html"><code class="gatsby-code-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">crossorigin</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>https://unpkg.com/react@16/umd/react.production.min.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script language-javascript"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">crossorigin</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>https://unpkg.com/react-dom@16/umd/react-dom.production.min.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script language-javascript"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span></code></pre>
      </div>
<p>Refer to the documentation for <a href="/docs/installation.html">detailed installation instructions</a>.</p>
<h2 id="upgrading"><a href="#upgrading" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Upgrading</h2>
<p>Although React 16 includes significant internal changes, in terms of upgrading, you can think of this like any other major React release. We’ve been serving React 16 to Facebook and Messenger.com users since earlier this year, and we released several beta and release candidate versions to flush out additional issues. With minor exceptions, <strong>if your app runs in 15.6 without any warnings, it should work in 16.</strong></p>
<p>For deprecations listed in <a href="#packaging">packaging</a> below, codemods are provided to automatically transform your deprecated code.
See the <a href="/blog/2017/04/07/react-v15.5.0.html">15.5.0</a> blog post for more information, or browse the codemods in the <a href="https://github.com/reactjs/react-codemod">react-codemod</a> project.</p>
<h3 id="new-deprecations"><a href="#new-deprecations" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>New deprecations</h3>
<p>Hydrating a server-rendered container now has an explicit API. If you’re reviving server-rendered HTML, use <a href="/docs/react-dom.html#hydrate"><code class="gatsby-code-text">ReactDOM.hydrate</code></a> instead of <code class="gatsby-code-text">ReactDOM.render</code>. Keep using <code class="gatsby-code-text">ReactDOM.render</code> if you’re just doing client-side rendering.</p>
<h3 id="react-addons"><a href="#react-addons" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>React Addons</h3>
<p>As previously announced, we’ve <a href="/blog/2017/04/07/react-v15.5.0.html#discontinuing-support-for-react-addons">discontinued support for React Addons</a>. We expect the latest version of each addon (except <code class="gatsby-code-text">react-addons-perf</code>; see below) to work for the foreseeable future, but we won’t publish additional updates.</p>
<p>Refer to the previous announcement for <a href="/blog/2017/04/07/react-v15.5.0.html#discontinuing-support-for-react-addons">suggestions on how to migrate</a>.</p>
<p><code class="gatsby-code-text">react-addons-perf</code> no longer works at all in React 16. It’s likely that we’ll release a new version of this tool in the future. In the meantime, you can <a href="/docs/optimizing-performance.html#profiling-components-with-the-chrome-performance-tab">use your browser’s performance tools to profile React components</a>.</p>
<h3 id="breaking-changes"><a href="#breaking-changes" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Breaking changes</h3>
<p>React 16 includes a number of small breaking changes. These only affect uncommon use cases and we don’t expect them to break most apps.</p>
<ul>
<li>React 15 had limited, undocumented support for error boundaries using <code class="gatsby-code-text">unstable_handleError</code>. This method has been renamed to <code class="gatsby-code-text">componentDidCatch</code>. You can use a codemod to <a href="https://github.com/reactjs/react-codemod#error-boundaries">automatically migrate to the new API</a>.</li>
<li><code class="gatsby-code-text">ReactDOM.render</code> and <code class="gatsby-code-text">ReactDOM.unstable_renderSubtreeIntoContainer</code> now return null if called from inside a lifecycle method. To work around this, you can use <a href="https://github.com/facebook/react/issues/10309#issuecomment-318433235">portals</a> or <a href="https://github.com/facebook/react/issues/10309#issuecomment-318434635">refs</a>.</li>
<li>
<p><code class="gatsby-code-text">setState</code>:</p>
<ul>
<li>Calling <code class="gatsby-code-text">setState</code> with null no longer triggers an update. This allows you to decide in an updater function if you want to re-render.</li>
<li>Calling <code class="gatsby-code-text">setState</code> directly in render always causes an update. This was not previously the case. Regardless, you should not be calling setState from render.</li>
<li><code class="gatsby-code-text">setState</code> callbacks (second argument) now fire immediately after <code class="gatsby-code-text">componentDidMount</code> / <code class="gatsby-code-text">componentDidUpdate</code> instead of after all components have rendered.</li>
</ul>
</li>
<li>When replacing <code class="gatsby-code-text">&lt;A /&gt;</code> with <code class="gatsby-code-text">&lt;B /&gt;</code>,  <code class="gatsby-code-text">B.componentWillMount</code> now always happens before  <code class="gatsby-code-text">A.componentWillUnmount</code>. Previously, <code class="gatsby-code-text">A.componentWillUnmount</code> could fire first in some cases.</li>
<li>Previously, changing the ref to a component would always detach the ref before that component’s render is called. Now, we change the ref later, when applying the changes to the DOM.</li>
<li>It is not safe to re-render into a container that was modified by something other than React. This worked previously in some cases but was never supported. We now emit a warning in this case. Instead you should clean up your component trees using <code class="gatsby-code-text">ReactDOM.unmountComponentAtNode</code>. <a href="https://github.com/facebook/react/issues/10294#issuecomment-318820987">See this example.</a></li>
<li><code class="gatsby-code-text">componentDidUpdate</code> lifecycle no longer receives <code class="gatsby-code-text">prevContext</code> param. (See <a href="https://github.com/facebook/react/issues/8631">#8631</a>)</li>
<li>Shallow renderer no longer calls <code class="gatsby-code-text">componentDidUpdate</code> because DOM refs are not available. This also makes it consistent with <code class="gatsby-code-text">componentDidMount</code> (which does not get called in previous versions either).</li>
<li>Shallow renderer does not implement <code class="gatsby-code-text">unstable_batchedUpdates</code> anymore.</li>
<li><code class="gatsby-code-text">ReactDOM.unstable_batchedUpdates</code> now only takes one extra argument after the callback.</li>
</ul>
<h3 id="packaging"><a href="#packaging" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Packaging</h3>
<ul>
<li>There is no <code class="gatsby-code-text">react/lib/*</code> and <code class="gatsby-code-text">react-dom/lib/*</code> anymore. Even in CommonJS environments, React and ReactDOM are precompiled to single files (“flat bundles”). If you previously relied on undocumented React internals, and they don’t work anymore, let us know about your specific case in a new issue, and we’ll try to figure out a migration strategy for you.</li>
<li>There is no <code class="gatsby-code-text">react-with-addons.js</code> build anymore. All compatible addons are published separately on npm, and have single-file browser versions if you need them.</li>
<li>The deprecations introduced in 15.x have been removed from the core package. <code class="gatsby-code-text">React.createClass</code> is now available as <code class="gatsby-code-text">create-react-class</code>, <code class="gatsby-code-text">React.PropTypes</code> as <code class="gatsby-code-text">prop-types</code>, <code class="gatsby-code-text">React.DOM</code> as <code class="gatsby-code-text">react-dom-factories</code>, <code class="gatsby-code-text">react-addons-test-utils</code> as <code class="gatsby-code-text">react-dom/test-utils</code>, and shallow renderer as <code class="gatsby-code-text">react-test-renderer/shallow</code>. See <a href="/blog/2017/04/07/react-v15.5.0.html">15.5.0</a> and <a href="/blog/2017/06/13/react-v15.6.0.html">15.6.0</a> blog posts for instructions on migrating code and automated codemods.</li>
<li>
<p>The names and paths to the single-file browser builds have changed to emphasize the difference between development and production builds. For example:</p>
<ul>
<li><code class="gatsby-code-text">react/dist/react.js</code> → <code class="gatsby-code-text">react/umd/react.development.js</code></li>
<li><code class="gatsby-code-text">react/dist/react.min.js</code> → <code class="gatsby-code-text">react/umd/react.production.min.js</code></li>
<li><code class="gatsby-code-text">react-dom/dist/react-dom.js</code> → <code class="gatsby-code-text">react-dom/umd/react-dom.development.js</code></li>
<li><code class="gatsby-code-text">react-dom/dist/react-dom.min</code>.js → <code class="gatsby-code-text">react-dom/umd/react-dom.production.min.js</code></li>
</ul>
</li>
</ul>
<h2 id="javascript-environment-requirements"><a href="#javascript-environment-requirements" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>JavaScript Environment Requirements</h2>
<p>React 16 depends on the collection types <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map">Map</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set">Set</a>. If you support older browsers and devices which may not yet provide these natively (e.g. IE &#x3C; 11), consider including a global polyfill in your bundled application, such as <a href="https://github.com/zloirock/core-js">core-js</a> or <a href="https://babeljs.io/docs/usage/polyfill/">babel-polyfill</a>.</p>
<p>A polyfilled environment for React 16 using core-js to support older browsers might look like:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">import</span> <span class="token string">'core-js/es6/map'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token string">'core-js/es6/set'</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> React <span class="token keyword">from</span> <span class="token string">'react'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> ReactDOM <span class="token keyword">from</span> <span class="token string">'react-dom'</span><span class="token punctuation">;</span>

ReactDOM<span class="token punctuation">.</span><span class="token function">render</span><span class="token punctuation">(</span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span><span class="token punctuation">></span></span>Hello<span class="token punctuation">,</span> world<span class="token operator">!</span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">></span></span><span class="token punctuation">,</span>
  document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">'root'</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
      </div>
<p>React also depends on <code class="gatsby-code-text">requestAnimationFrame</code> (even in test environments).<br>
You can use the <a href="https://www.npmjs.com/package/raf">raf</a> package to shim <code class="gatsby-code-text">requestAnimationFrame</code>:</p>
<div class="gatsby-highlight">
      <pre class="gatsby-code-jsx"><code class="gatsby-code-jsx"><span class="token keyword">import</span> <span class="token string">'raf/polyfill'</span><span class="token punctuation">;</span></code></pre>
      </div>
<h2 id="acknowledgments"><a href="#acknowledgments" aria-hidden="true" class="anchor"><svg aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Acknowledgments</h2>
<p>As always, this release would not have been possible without our open source contributors. Thanks to everyone who filed bugs, opened PRs, responded to issues, wrote documentation, and more!</p>
<p>Special thanks to our core contributors, especially for their heroic efforts over the past few weeks during the prerelease cycle: <a href="https://twitter.com/aweary">Brandon Dail</a>, <a href="https://twitter.com/monasticpanic">Jason Quense</a>, <a href="https://twitter.com/natehunzaker">Nathan Hunzaker</a>, and <a href="https://twitter.com/xander76">Sasha Aickin</a>.</p>]]></description><link>https://reactjs.org/blog/2017/09/26/react-v16.0.html</link><guid isPermaLink="false">https://reactjs.org/blog/2017/09/26/react-v16.0.html</guid><pubDate>Tue, 26 Sep 2017 00:00:00 GMT</pubDate></item></channel></rss>