{"componentChunkName":"component---src-templates-docs-js","path":"/docs/react-component.html","result":{"data":{"markdownRemark":{"html":"<p>This page contains a detailed API reference for the React component class definition. It assumes you’re familiar with fundamental React concepts, such as <a href=\"/docs/components-and-props.html\">Components and Props</a>, as well as <a href=\"/docs/state-and-lifecycle.html\">State and Lifecycle</a>. If you’re not, read them first.</p>\n<h2 id=\"overview\"><a href=\"#overview\" aria-hidden 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>Overview </h2>\n<p>React lets you define components as classes or functions. Components defined as classes currently provide more features which are described in detail on this page. To define a React component class, you need to extend <code class=\"gatsby-code-text\">React.Component</code>:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">class</span> <span class=\"token class-name\">Welcome</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>\n  <span class=\"token function\">render</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</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 plain-text\">Hello, </span><span class=\"token punctuation\">{</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>props<span class=\"token punctuation\">.</span>name<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><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>The only method you <em>must</em> define in a <code class=\"gatsby-code-text\">React.Component</code> subclass is called <a href=\"#render\"><code class=\"gatsby-code-text\">render()</code></a>. All the other methods described on this page are optional.</p>\n<p><strong>We strongly recommend against creating your own base component classes.</strong> In React components, <a href=\"/docs/composition-vs-inheritance.html\">code reuse is primarily achieved through composition rather than inheritance</a>.</p>\n<blockquote>\n<p>Note:</p>\n<p>React doesn’t force you to use the ES6 class syntax. If you prefer to avoid it, you may use the <code class=\"gatsby-code-text\">create-react-class</code> module or a similar custom abstraction instead. Take a look at <a href=\"/docs/react-without-es6.html\">Using React without ES6</a> to learn more.</p>\n</blockquote>\n<h3 id=\"the-component-lifecycle\"><a href=\"#the-component-lifecycle\" aria-hidden 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>The Component Lifecycle </h3>\n<p>Each component has several “lifecycle methods” that you can override to run code at particular times in the process. <strong>You can use <a href=\"https://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">this lifecycle diagram</a> as a cheat sheet.</strong> In the list below, commonly used lifecycle methods are marked as <strong>bold</strong>. The rest of them exist for relatively rare use cases.</p>\n<h4 id=\"mounting\"><a href=\"#mounting\" aria-hidden 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>Mounting </h4>\n<p>These methods are called in the following order when an instance of a component is being created and inserted into the DOM:</p>\n<ul>\n<li><a href=\"#constructor\"><strong><code class=\"gatsby-code-text\">constructor()</code></strong></a></li>\n<li><a href=\"#static-getderivedstatefromprops\"><code class=\"gatsby-code-text\">static getDerivedStateFromProps()</code></a></li>\n<li><a href=\"#render\"><strong><code class=\"gatsby-code-text\">render()</code></strong></a></li>\n<li><a href=\"#componentdidmount\"><strong><code class=\"gatsby-code-text\">componentDidMount()</code></strong></a></li>\n</ul>\n<blockquote>\n<p>Note:</p>\n<p>These methods are considered legacy and you should <a href=\"/blog/2018/03/27/update-on-async-rendering.html\">avoid them</a> in new code:</p>\n<ul>\n<li><a href=\"#unsafe_componentwillmount\"><code class=\"gatsby-code-text\">UNSAFE_componentWillMount()</code></a></li>\n</ul>\n</blockquote>\n<h4 id=\"updating\"><a href=\"#updating\" aria-hidden 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 </h4>\n<p>An update can be caused by changes to props or state. These methods are called in the following order when a component is being re-rendered:</p>\n<ul>\n<li><a href=\"#static-getderivedstatefromprops\"><code class=\"gatsby-code-text\">static getDerivedStateFromProps()</code></a></li>\n<li><a href=\"#shouldcomponentupdate\"><code class=\"gatsby-code-text\">shouldComponentUpdate()</code></a></li>\n<li><a href=\"#render\"><strong><code class=\"gatsby-code-text\">render()</code></strong></a></li>\n<li><a href=\"#getsnapshotbeforeupdate\"><code class=\"gatsby-code-text\">getSnapshotBeforeUpdate()</code></a></li>\n<li><a href=\"#componentdidupdate\"><strong><code class=\"gatsby-code-text\">componentDidUpdate()</code></strong></a></li>\n</ul>\n<blockquote>\n<p>Note:</p>\n<p>These methods are considered legacy and you should <a href=\"/blog/2018/03/27/update-on-async-rendering.html\">avoid them</a> in new code:</p>\n<ul>\n<li><a href=\"#unsafe_componentwillupdate\"><code class=\"gatsby-code-text\">UNSAFE_componentWillUpdate()</code></a></li>\n<li><a href=\"#unsafe_componentwillreceiveprops\"><code class=\"gatsby-code-text\">UNSAFE_componentWillReceiveProps()</code></a></li>\n</ul>\n</blockquote>\n<h4 id=\"unmounting\"><a href=\"#unmounting\" aria-hidden 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>Unmounting </h4>\n<p>This method is called when a component is being removed from the DOM:</p>\n<ul>\n<li><a href=\"#componentwillunmount\"><strong><code class=\"gatsby-code-text\">componentWillUnmount()</code></strong></a></li>\n</ul>\n<h4 id=\"error-handling\"><a href=\"#error-handling\" aria-hidden 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>Error Handling </h4>\n<p>These methods are called when there is an error during rendering, in a lifecycle method, or in the constructor of any child component.</p>\n<ul>\n<li><a href=\"#static-getderivedstatefromerror\"><code class=\"gatsby-code-text\">static getDerivedStateFromError()</code></a></li>\n<li><a href=\"#componentdidcatch\"><code class=\"gatsby-code-text\">componentDidCatch()</code></a></li>\n</ul>\n<h3 id=\"other-apis\"><a href=\"#other-apis\" aria-hidden 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 APIs </h3>\n<p>Each component also provides some other APIs:</p>\n<ul>\n<li><a href=\"#setstate\"><code class=\"gatsby-code-text\">setState()</code></a></li>\n<li><a href=\"#forceupdate\"><code class=\"gatsby-code-text\">forceUpdate()</code></a></li>\n</ul>\n<h3 id=\"class-properties\"><a href=\"#class-properties\" aria-hidden 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>Class Properties </h3>\n<ul>\n<li><a href=\"#defaultprops\"><code class=\"gatsby-code-text\">defaultProps</code></a></li>\n<li><a href=\"#displayname\"><code class=\"gatsby-code-text\">displayName</code></a></li>\n</ul>\n<h3 id=\"instance-properties\"><a href=\"#instance-properties\" aria-hidden 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>Instance Properties </h3>\n<ul>\n<li><a href=\"#props\"><code class=\"gatsby-code-text\">props</code></a></li>\n<li><a href=\"#state\"><code class=\"gatsby-code-text\">state</code></a></li>\n</ul>\n<hr>\n<h2 id=\"reference\"><a href=\"#reference\" aria-hidden 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>Reference </h2>\n<h3 id=\"commonly-used-lifecycle-methods\"><a href=\"#commonly-used-lifecycle-methods\" aria-hidden 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>Commonly Used Lifecycle Methods </h3>\n<p>The methods in this section cover the vast majority of use cases you’ll encounter creating React components. <strong>For a visual reference, check out <a href=\"https://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">this lifecycle diagram</a>.</strong></p>\n<h3 id=\"render\"><a href=\"#render\" aria-hidden 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\">render()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><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></code></pre></div>\n<p>The <code class=\"gatsby-code-text\">render()</code> method is the only required method in a class component.</p>\n<p>When called, it should examine <code class=\"gatsby-code-text\">this.props</code> and <code class=\"gatsby-code-text\">this.state</code> and return one of the following types:</p>\n<ul>\n<li><strong>React elements.</strong> Typically created via <a href=\"/docs/introducing-jsx.html\">JSX</a>. For example, <code class=\"gatsby-code-text\">&lt;div /&gt;</code> and <code class=\"gatsby-code-text\">&lt;MyComponent /&gt;</code> are React elements that instruct React to render a DOM node, or another user-defined component, respectively.</li>\n<li><strong>Arrays and fragments.</strong> Let you return multiple elements from render. See the documentation on <a href=\"/docs/fragments.html\">fragments</a> for more details.</li>\n<li><strong>Portals</strong>. Let you render children into a different DOM subtree. See the documentation on <a href=\"/docs/portals.html\">portals</a> for more details.</li>\n<li><strong>String and numbers.</strong> These are rendered as text nodes in the DOM.</li>\n<li><strong>Booleans or <code class=\"gatsby-code-text\">null</code></strong>. Render nothing. (Mostly exists to support <code class=\"gatsby-code-text\">return test &amp;&amp; &lt;Child /&gt;</code> pattern, where <code class=\"gatsby-code-text\">test</code> is boolean.)</li>\n</ul>\n<p>The <code class=\"gatsby-code-text\">render()</code> function should be pure, meaning that it does not modify component state, it returns the same result each time it’s invoked, and it does not directly interact with the browser.</p>\n<p>If you need to interact with the browser, perform your work in <code class=\"gatsby-code-text\">componentDidMount()</code> or the other lifecycle methods instead. Keeping <code class=\"gatsby-code-text\">render()</code> pure makes components easier to think about.</p>\n<blockquote>\n<p>Note</p>\n<p><code class=\"gatsby-code-text\">render()</code> will not be invoked if <a href=\"#shouldcomponentupdate\"><code class=\"gatsby-code-text\">shouldComponentUpdate()</code></a> returns false.</p>\n</blockquote>\n<hr>\n<h3 id=\"constructor\"><a href=\"#constructor\" aria-hidden 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\">constructor()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span>props<span class=\"token punctuation\">)</span></code></pre></div>\n<p><strong>If you don’t initialize state and you don’t bind methods, you don’t need to implement a constructor for your React component.</strong></p>\n<p>The constructor for a React component is called before it is mounted. When implementing the constructor for a <code class=\"gatsby-code-text\">React.Component</code> subclass, you should call <code class=\"gatsby-code-text\">super(props)</code> before any other statement. Otherwise, <code class=\"gatsby-code-text\">this.props</code> will be undefined in the constructor, which can lead to bugs.</p>\n<p>Typically, in React constructors are only used for two purposes:</p>\n<ul>\n<li>Initializing <a href=\"/docs/state-and-lifecycle.html\">local state</a> by assigning an object to <code class=\"gatsby-code-text\">this.state</code>.</li>\n<li>Binding <a href=\"/docs/handling-events.html\">event handler</a> methods to an instance.</li>\n</ul>\n<p>You <strong>should not call <code class=\"gatsby-code-text\">setState()</code></strong> in the <code class=\"gatsby-code-text\">constructor()</code>. Instead, if your component needs to use local state, <strong>assign the initial state to <code class=\"gatsby-code-text\">this.state</code></strong> directly in the constructor:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">props</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">super</span><span class=\"token punctuation\">(</span>props<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token comment\">// Don't call this.setState() here!</span>\n  <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> counter<span class=\"token operator\">:</span> <span class=\"token number\">0</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n  <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>handleClick <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">handleClick</span><span class=\"token punctuation\">.</span><span class=\"token function\">bind</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Constructor is the only place where you should assign <code class=\"gatsby-code-text\">this.state</code> directly. In all other methods, you need to use <code class=\"gatsby-code-text\">this.setState()</code> instead.</p>\n<p>Avoid introducing any side-effects or subscriptions in the constructor. For those use cases, use <code class=\"gatsby-code-text\">componentDidMount()</code> instead.</p>\n<blockquote>\n<p>Note</p>\n<p><strong>Avoid copying props into state! This is a common mistake:</strong></p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">props</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n <span class=\"token keyword\">super</span><span class=\"token punctuation\">(</span>props<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n <span class=\"token comment\">// Don't do this!</span>\n <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> color<span class=\"token operator\">:</span> props<span class=\"token punctuation\">.</span>color <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>The problem is that it’s both unnecessary (you can use <code class=\"gatsby-code-text\">this.props.color</code> directly instead), and creates bugs (updates to the <code class=\"gatsby-code-text\">color</code> prop won’t be reflected in the state).</p>\n<p><strong>Only use this pattern if you intentionally want to ignore prop updates.</strong> In that case, it makes sense to rename the prop to be called <code class=\"gatsby-code-text\">initialColor</code> or <code class=\"gatsby-code-text\">defaultColor</code>. You can then force a component to “reset” its internal state by <a href=\"/blog/2018/06/07/you-probably-dont-need-derived-state.html#recommendation-fully-uncontrolled-component-with-a-key\">changing its <code class=\"gatsby-code-text\">key</code></a> when necessary.</p>\n<p>Read our <a href=\"/blog/2018/06/07/you-probably-dont-need-derived-state.html\">blog post on avoiding derived state</a> to learn about what to do if you think you need some state to depend on the props.</p>\n</blockquote>\n<hr>\n<h3 id=\"componentdidmount\"><a href=\"#componentdidmount\" aria-hidden 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\">componentDidMount()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">componentDidMount</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p><code class=\"gatsby-code-text\">componentDidMount()</code> is invoked immediately after a component is mounted (inserted into the tree). Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request.</p>\n<p>This method is a good place to set up any subscriptions. If you do that, don’t forget to unsubscribe in <code class=\"gatsby-code-text\">componentWillUnmount()</code>.</p>\n<p>You <strong>may call <code class=\"gatsby-code-text\">setState()</code> immediately</strong> in <code class=\"gatsby-code-text\">componentDidMount()</code>. It will trigger an extra rendering, but it will happen before the browser updates the screen. This guarantees that even though the <code class=\"gatsby-code-text\">render()</code> will be called twice in this case, the user won’t see the intermediate state. Use this pattern with caution because it often causes performance issues. In most cases, you should be able to assign the initial state in the <code class=\"gatsby-code-text\">constructor()</code> instead. It can, however, be necessary for cases like modals and tooltips when you need to measure a DOM node before rendering something that depends on its size or position.</p>\n<hr>\n<h3 id=\"componentdidupdate\"><a href=\"#componentdidupdate\" aria-hidden 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\">componentDidUpdate()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><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></code></pre></div>\n<p><code class=\"gatsby-code-text\">componentDidUpdate()</code> is invoked immediately after updating occurs. This method is not called for the initial render.</p>\n<p>Use this as an opportunity to operate on the DOM when the component has been updated. This is also a good place to do network requests as long as you compare the current props to previous props (e.g. a network request may not be necessary if the props have not changed).</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">componentDidUpdate</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">prevProps</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">// Typical usage (don't forget to compare props):</span>\n  <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>userID <span class=\"token operator\">!==</span> prevProps<span class=\"token punctuation\">.</span>userID<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">fetchData</span><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>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>You <strong>may call <code class=\"gatsby-code-text\">setState()</code> immediately</strong> in <code class=\"gatsby-code-text\">componentDidUpdate()</code> but note that <strong>it must be wrapped in a condition</strong> like in the example above, or you’ll cause an infinite loop. It would also cause an extra re-rendering which, while not visible to the user, can affect the component performance. If you’re trying to “mirror” some state to a prop coming from above, consider using the prop directly instead. Read more about <a href=\"/blog/2018/06/07/you-probably-dont-need-derived-state.html\">why copying props into state causes bugs</a>.</p>\n<p>If your component implements the <code class=\"gatsby-code-text\">getSnapshotBeforeUpdate()</code> lifecycle (which is rare), the value it returns will be passed as a third “snapshot” parameter to <code class=\"gatsby-code-text\">componentDidUpdate()</code>. Otherwise this parameter will be undefined.</p>\n<blockquote>\n<p>Note</p>\n<p><code class=\"gatsby-code-text\">componentDidUpdate()</code> will not be invoked if <a href=\"#shouldcomponentupdate\"><code class=\"gatsby-code-text\">shouldComponentUpdate()</code></a> returns false.</p>\n</blockquote>\n<hr>\n<h3 id=\"componentwillunmount\"><a href=\"#componentwillunmount\" aria-hidden 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\">componentWillUnmount()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">componentWillUnmount</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p><code class=\"gatsby-code-text\">componentWillUnmount()</code> is invoked immediately before a component is unmounted and destroyed. Perform any necessary cleanup in this method, such as invalidating timers, canceling network requests, or cleaning up any subscriptions that were created in <code class=\"gatsby-code-text\">componentDidMount()</code>.</p>\n<p>You <strong>should not call <code class=\"gatsby-code-text\">setState()</code></strong> in <code class=\"gatsby-code-text\">componentWillUnmount()</code> because the component will never be re-rendered. Once a component instance is unmounted, it will never be mounted again.</p>\n<hr>\n<h3 id=\"rarely-used-lifecycle-methods\"><a href=\"#rarely-used-lifecycle-methods\" aria-hidden 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>Rarely Used Lifecycle Methods </h3>\n<p>The methods in this section correspond to uncommon use cases. They’re handy once in a while, but most of your components probably don’t need any of them. <strong>You can see most of the methods below on <a href=\"https://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">this lifecycle diagram</a> if you click the “Show less common lifecycles” checkbox at the top of it.</strong></p>\n<h3 id=\"shouldcomponentupdate\"><a href=\"#shouldcomponentupdate\" aria-hidden 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\">shouldComponentUpdate()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">shouldComponentUpdate</span><span class=\"token punctuation\">(</span>nextProps<span class=\"token punctuation\">,</span> nextState<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Use <code class=\"gatsby-code-text\">shouldComponentUpdate()</code> to let React know if a component’s output is not affected by the current change in state or props. The default behavior is to re-render on every state change, and in the vast majority of cases you should rely on the default behavior.</p>\n<p><code class=\"gatsby-code-text\">shouldComponentUpdate()</code> is invoked before rendering when new props or state are being received. Defaults to <code class=\"gatsby-code-text\">true</code>. This method is not called for the initial render or when <code class=\"gatsby-code-text\">forceUpdate()</code> is used.</p>\n<p>This method only exists as a <strong><a href=\"/docs/optimizing-performance.html\">performance optimization</a>.</strong> Do not rely on it to “prevent” a rendering, as this can lead to bugs. <strong>Consider using the built-in <a href=\"/docs/react-api.html#reactpurecomponent\"><code class=\"gatsby-code-text\">PureComponent</code></a></strong> instead of writing <code class=\"gatsby-code-text\">shouldComponentUpdate()</code> by hand. <code class=\"gatsby-code-text\">PureComponent</code> performs a shallow comparison of props and state, and reduces the chance that you’ll skip a necessary update.</p>\n<p>If you are confident you want to write it by hand, you may compare <code class=\"gatsby-code-text\">this.props</code> with <code class=\"gatsby-code-text\">nextProps</code> and <code class=\"gatsby-code-text\">this.state</code> with <code class=\"gatsby-code-text\">nextState</code> and return <code class=\"gatsby-code-text\">false</code> to tell React the update can be skipped. Note that returning <code class=\"gatsby-code-text\">false</code> does not prevent child components from re-rendering when <em>their</em> state changes.</p>\n<p>We do not recommend doing deep equality checks or using <code class=\"gatsby-code-text\">JSON.stringify()</code> in <code class=\"gatsby-code-text\">shouldComponentUpdate()</code>. It is very inefficient and will harm performance.</p>\n<p>Currently, if <code class=\"gatsby-code-text\">shouldComponentUpdate()</code> returns <code class=\"gatsby-code-text\">false</code>, then <a href=\"#unsafe_componentwillupdate\"><code class=\"gatsby-code-text\">UNSAFE_componentWillUpdate()</code></a>, <a href=\"#render\"><code class=\"gatsby-code-text\">render()</code></a>, and <a href=\"#componentdidupdate\"><code class=\"gatsby-code-text\">componentDidUpdate()</code></a> will not be invoked. In the future React may treat <code class=\"gatsby-code-text\">shouldComponentUpdate()</code> as a hint rather than a strict directive, and returning <code class=\"gatsby-code-text\">false</code> may still result in a re-rendering of the component.</p>\n<hr>\n<h3 id=\"static-getderivedstatefromprops\"><a href=\"#static-getderivedstatefromprops\" aria-hidden 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\">static getDerivedStateFromProps()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><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></code></pre></div>\n<p><code class=\"gatsby-code-text\">getDerivedStateFromProps</code> is invoked right before calling the render method, both on the initial mount and on subsequent updates. It should return an object to update the state, or <code class=\"gatsby-code-text\">null</code> to update nothing.</p>\n<p>This method exists for <a href=\"/blog/2018/06/07/you-probably-dont-need-derived-state.html#when-to-use-derived-state\">rare use cases</a> where the state depends on changes in props over time. For example, it might be handy for implementing a <code class=\"gatsby-code-text\">&lt;Transition&gt;</code> component that compares its previous and next children to decide which of them to animate in and out.</p>\n<p>Deriving state leads to verbose code and makes your components difficult to think about.\n<a href=\"/blog/2018/06/07/you-probably-dont-need-derived-state.html\">Make sure you’re familiar with simpler alternatives:</a></p>\n<ul>\n<li>If you need to <strong>perform a side effect</strong> (for example, data fetching or an animation) in response to a change in props, use <a href=\"#componentdidupdate\"><code class=\"gatsby-code-text\">componentDidUpdate</code></a> lifecycle instead.</li>\n<li>If you want to <strong>re-compute some data only when a prop changes</strong>, <a href=\"/blog/2018/06/07/you-probably-dont-need-derived-state.html#what-about-memoization\">use a memoization helper instead</a>.</li>\n<li>If you want to <strong>“reset” some state when a prop changes</strong>, consider either making a component <a href=\"/blog/2018/06/07/you-probably-dont-need-derived-state.html#recommendation-fully-controlled-component\">fully controlled</a> or <a href=\"/blog/2018/06/07/you-probably-dont-need-derived-state.html#recommendation-fully-uncontrolled-component-with-a-key\">fully uncontrolled with a <code class=\"gatsby-code-text\">key</code></a> instead.</li>\n</ul>\n<p>This method doesn’t have access to the component instance. If you’d like, you can reuse some code between <code class=\"gatsby-code-text\">getDerivedStateFromProps()</code> and the other class methods by extracting pure functions of the component props and state outside the class definition.</p>\n<p>Note that this method is fired on <em>every</em> render, regardless of the cause. This is in contrast to <code class=\"gatsby-code-text\">UNSAFE_componentWillReceiveProps</code>, which only fires when the parent causes a re-render and not as a result of a local <code class=\"gatsby-code-text\">setState</code>.</p>\n<hr>\n<h3 id=\"getsnapshotbeforeupdate\"><a href=\"#getsnapshotbeforeupdate\" aria-hidden 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\">getSnapshotBeforeUpdate()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">getSnapshotBeforeUpdate</span><span class=\"token punctuation\">(</span>prevProps<span class=\"token punctuation\">,</span> prevState<span class=\"token punctuation\">)</span></code></pre></div>\n<p><code class=\"gatsby-code-text\">getSnapshotBeforeUpdate()</code> is invoked right before the most recently rendered output is committed to e.g. the DOM. It enables your component to capture some information from the DOM (e.g. scroll position) before it is potentially changed. Any value returned by this lifecycle will be passed as a parameter to <code class=\"gatsby-code-text\">componentDidUpdate()</code>.</p>\n<p>This use case is not common, but it may occur in UIs like a chat thread that need to handle scroll position in a special way.</p>\n<p>A snapshot value (or <code class=\"gatsby-code-text\">null</code>) should be returned.</p>\n<p>For example:</p>\n<p><div class=\"gatsby-highlight\">\n        <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>\n  <span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">props</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">super</span><span class=\"token punctuation\">(</span>props<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>listRef <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>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token function\">getSnapshotBeforeUpdate</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">prevProps<span class=\"token punctuation\">,</span> prevState</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">// Are we adding new items to the list?</span>\n    <span class=\"token comment\">// Capture the scroll position so we can adjust scroll later.</span>\n    <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>\n      <span class=\"token keyword\">const</span> list <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>listRef<span class=\"token punctuation\">.</span>current<span class=\"token punctuation\">;</span>\n      <span class=\"token keyword\">return</span> list<span class=\"token punctuation\">.</span>scrollHeight <span class=\"token operator\">-</span> list<span class=\"token punctuation\">.</span>scrollTop<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    <span class=\"token keyword\">return</span> <span class=\"token keyword\">null</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token function\">componentDidUpdate</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">prevProps<span class=\"token punctuation\">,</span> prevState<span class=\"token punctuation\">,</span> snapshot</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">// If we have a snapshot value, we've just added new items.</span>\n    <span class=\"token comment\">// Adjust scroll so these new items don't push the old ones out of view.</span>\n    <span class=\"token comment\">// (snapshot here is the value returned from getSnapshotBeforeUpdate)</span>\n    <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>\n      <span class=\"token keyword\">const</span> list <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>listRef<span class=\"token punctuation\">.</span>current<span class=\"token punctuation\">;</span>\n      list<span class=\"token punctuation\">.</span>scrollTop <span class=\"token operator\">=</span> list<span class=\"token punctuation\">.</span>scrollHeight <span class=\"token operator\">-</span> snapshot<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token function\">render</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n      <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>listRef<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>\n    <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre>\n        </div></p>\n<p>In the above examples, it is important to read the <code class=\"gatsby-code-text\">scrollHeight</code> property in <code class=\"gatsby-code-text\">getSnapshotBeforeUpdate</code> because there may be delays between “render” phase lifecycles (like <code class=\"gatsby-code-text\">render</code>) and “commit” phase lifecycles (like <code class=\"gatsby-code-text\">getSnapshotBeforeUpdate</code> and <code class=\"gatsby-code-text\">componentDidUpdate</code>).</p>\n<hr>\n<h3 id=\"error-boundaries\"><a href=\"#error-boundaries\" aria-hidden 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>Error boundaries </h3>\n<p><a href=\"/docs/error-boundaries.html\">Error boundaries</a> are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed. Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.</p>\n<p>A class component becomes an error boundary if it defines either (or both) of the lifecycle methods <code class=\"gatsby-code-text\">static getDerivedStateFromError()</code> or <code class=\"gatsby-code-text\">componentDidCatch()</code>. Updating state from these lifecycles lets you capture an unhandled JavaScript error in the below tree and display a fallback UI.</p>\n<p>Only use error boundaries for recovering from unexpected exceptions; <strong>don’t try to use them for control flow.</strong></p>\n<p>For more details, see <a href=\"/blog/2017/07/26/error-handling-in-react-16.html\"><em>Error Handling in React 16</em></a>.</p>\n<blockquote>\n<p>Note</p>\n<p>Error boundaries only catch errors in the components <strong>below</strong> them in the tree. An error boundary can’t catch an error within itself.</p>\n</blockquote>\n<h3 id=\"static-getderivedstatefromerror\"><a href=\"#static-getderivedstatefromerror\" aria-hidden 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\">static getDerivedStateFromError()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">static</span> <span class=\"token function\">getDerivedStateFromError</span><span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span></code></pre></div>\n<p>This lifecycle is invoked after an error has been thrown by a descendant component.\nIt receives the error that was thrown as a parameter and should return a value to update state.</p>\n<div class=\"gatsby-highlight has-highlighted-lines\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">class</span> <span class=\"token class-name\">ErrorBoundary</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>\n  <span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">props</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">super</span><span class=\"token punctuation\">(</span>props<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> hasError<span class=\"token operator\">:</span> <span class=\"token boolean\">false</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n\n<span class=\"gatsby-highlight-code-line\">  <span class=\"token keyword\">static</span> <span class=\"token function\">getDerivedStateFromError</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">error</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></span><span class=\"gatsby-highlight-code-line\">    <span class=\"token comment\">// Update state so the next render will show the fallback UI.</span></span><span class=\"gatsby-highlight-code-line\">    <span class=\"token keyword\">return</span> <span class=\"token punctuation\">{</span> hasError<span class=\"token operator\">:</span> <span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></span><span class=\"gatsby-highlight-code-line\">  <span class=\"token punctuation\">}</span></span>\n  <span class=\"token function\">render</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n<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>hasError<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></span><span class=\"gatsby-highlight-code-line\">      <span class=\"token comment\">// You can render any custom fallback UI</span></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>h1</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">Something went wrong.</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></span><span class=\"gatsby-highlight-code-line\">    <span class=\"token punctuation\">}</span></span>\n    <span class=\"token keyword\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>props<span class=\"token punctuation\">.</span>children<span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<blockquote>\n<p>Note</p>\n<p><code class=\"gatsby-code-text\">getDerivedStateFromError()</code> is called during the “render” phase, so side-effects are not permitted.\nFor those use cases, use <code class=\"gatsby-code-text\">componentDidCatch()</code> instead.</p>\n</blockquote>\n<hr>\n<h3 id=\"componentdidcatch\"><a href=\"#componentdidcatch\" aria-hidden 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\">componentDidCatch()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">componentDidCatch</span><span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">,</span> info<span class=\"token punctuation\">)</span></code></pre></div>\n<p>This lifecycle is invoked after an error has been thrown by a descendant component.\nIt receives two parameters:</p>\n<ol>\n<li><code class=\"gatsby-code-text\">error</code> - The error that was thrown.</li>\n<li><code class=\"gatsby-code-text\">info</code> - An object with a <code class=\"gatsby-code-text\">componentStack</code> key containing <a href=\"/docs/error-boundaries.html#component-stack-traces\">information about which component threw the error</a>.</li>\n</ol>\n<p><code class=\"gatsby-code-text\">componentDidCatch()</code> is called during the “commit” phase, so side-effects are permitted.\nIt should be used for things like logging errors:</p>\n<div class=\"gatsby-highlight has-highlighted-lines\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">class</span> <span class=\"token class-name\">ErrorBoundary</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>\n  <span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">props</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">super</span><span class=\"token punctuation\">(</span>props<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> hasError<span class=\"token operator\">:</span> <span class=\"token boolean\">false</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token keyword\">static</span> <span class=\"token function\">getDerivedStateFromError</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">error</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">// Update state so the next render will show the fallback UI.</span>\n    <span class=\"token keyword\">return</span> <span class=\"token punctuation\">{</span> hasError<span class=\"token operator\">:</span> <span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n\n<span class=\"gatsby-highlight-code-line\">  <span class=\"token function\">componentDidCatch</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">error<span class=\"token punctuation\">,</span> info</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></span><span class=\"gatsby-highlight-code-line\">    <span class=\"token comment\">// Example \"componentStack\":</span></span><span class=\"gatsby-highlight-code-line\">    <span class=\"token comment\">//   in ComponentThatThrows (created by App)</span></span><span class=\"gatsby-highlight-code-line\">    <span class=\"token comment\">//   in ErrorBoundary (created by App)</span></span><span class=\"gatsby-highlight-code-line\">    <span class=\"token comment\">//   in div (created by App)</span></span><span class=\"gatsby-highlight-code-line\">    <span class=\"token comment\">//   in App</span></span><span class=\"gatsby-highlight-code-line\">    <span class=\"token function\">logComponentStackToMyService</span><span class=\"token punctuation\">(</span>info<span class=\"token punctuation\">.</span>componentStack<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></span><span class=\"gatsby-highlight-code-line\">  <span class=\"token punctuation\">}</span></span>\n  <span class=\"token function\">render</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <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>hasError<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token comment\">// You can render any custom fallback UI</span>\n      <span class=\"token keyword\">return</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 plain-text\">Something went wrong.</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>\n    <span class=\"token punctuation\">}</span>\n\n    <span class=\"token keyword\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>props<span class=\"token punctuation\">.</span>children<span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Production and development builds of React slightly differ in the way <code class=\"gatsby-code-text\">componentDidCatch()</code> handles errors.</p>\n<p>On development, the errors will bubble up to <code class=\"gatsby-code-text\">window</code>, this means that any <code class=\"gatsby-code-text\">window.onerror</code> or <code class=\"gatsby-code-text\">window.addEventListener(&#39;error&#39;, callback)</code> will intercept the errors that have been caught by <code class=\"gatsby-code-text\">componentDidCatch()</code>.</p>\n<p>On production, instead, the errors will not bubble up, which means any ancestor error handler will only receive errors not explicitly caught by <code class=\"gatsby-code-text\">componentDidCatch()</code>.</p>\n<blockquote>\n<p>Note</p>\n<p>In the event of an error, you can render a fallback UI with <code class=\"gatsby-code-text\">componentDidCatch()</code> by calling <code class=\"gatsby-code-text\">setState</code>, but this will be deprecated in a future release.\nUse <code class=\"gatsby-code-text\">static getDerivedStateFromError()</code> to handle fallback rendering instead.</p>\n</blockquote>\n<hr>\n<h3 id=\"legacy-lifecycle-methods\"><a href=\"#legacy-lifecycle-methods\" aria-hidden 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>Legacy Lifecycle Methods </h3>\n<p>The lifecycle methods below are marked as “legacy”. They still work, but we don’t recommend using them in the new code. You can learn more about migrating away from legacy lifecycle methods in <a href=\"/blog/2018/03/27/update-on-async-rendering.html\">this blog post</a>.</p>\n<h3 id=\"unsafe_componentwillmount\"><a href=\"#unsafe_componentwillmount\" aria-hidden 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\">UNSAFE_componentWillMount()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">UNSAFE_componentWillMount</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code></pre></div>\n<blockquote>\n<p>Note</p>\n<p>This lifecycle was previously named <code class=\"gatsby-code-text\">componentWillMount</code>. That name will continue to work until version 17. Use the <a href=\"https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">rename-unsafe-lifecycles</code> codemod</a> to automatically update your components.</p>\n</blockquote>\n<p><code class=\"gatsby-code-text\">UNSAFE_componentWillMount()</code> is invoked just before mounting occurs. It is called before <code class=\"gatsby-code-text\">render()</code>, therefore calling <code class=\"gatsby-code-text\">setState()</code> synchronously in this method will not trigger an extra rendering. Generally, we recommend using the <code class=\"gatsby-code-text\">constructor()</code> instead for initializing state.</p>\n<p>Avoid introducing any side-effects or subscriptions in this method. For those use cases, use <code class=\"gatsby-code-text\">componentDidMount()</code> instead.</p>\n<p>This is the only lifecycle method called on server rendering.</p>\n<hr>\n<h3 id=\"unsafe_componentwillreceiveprops\"><a href=\"#unsafe_componentwillreceiveprops\" aria-hidden 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\">UNSAFE_componentWillReceiveProps()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">UNSAFE_componentWillReceiveProps</span><span class=\"token punctuation\">(</span>nextProps<span class=\"token punctuation\">)</span></code></pre></div>\n<blockquote>\n<p>Note</p>\n<p>This lifecycle was previously named <code class=\"gatsby-code-text\">componentWillReceiveProps</code>. That name will continue to work until version 17. Use the <a href=\"https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">rename-unsafe-lifecycles</code> codemod</a> to automatically update your components.</p>\n</blockquote>\n<blockquote>\n<p>Note:</p>\n<p>Using this lifecycle method often leads to bugs and inconsistencies</p>\n<ul>\n<li>If you need to <strong>perform a side effect</strong> (for example, data fetching or an animation) in response to a change in props, use <a href=\"#componentdidupdate\"><code class=\"gatsby-code-text\">componentDidUpdate</code></a> lifecycle instead.</li>\n<li>If you used <code class=\"gatsby-code-text\">componentWillReceiveProps</code> for <strong>re-computing some data only when a prop changes</strong>, <a href=\"/blog/2018/06/07/you-probably-dont-need-derived-state.html#what-about-memoization\">use a memoization helper instead</a>.</li>\n<li>If you used <code class=\"gatsby-code-text\">componentWillReceiveProps</code> to <strong>“reset” some state when a prop changes</strong>, consider either making a component <a href=\"/blog/2018/06/07/you-probably-dont-need-derived-state.html#recommendation-fully-controlled-component\">fully controlled</a> or <a href=\"/blog/2018/06/07/you-probably-dont-need-derived-state.html#recommendation-fully-uncontrolled-component-with-a-key\">fully uncontrolled with a <code class=\"gatsby-code-text\">key</code></a> instead.</li>\n</ul>\n<p>For other use cases, <a href=\"/blog/2018/06/07/you-probably-dont-need-derived-state.html\">follow the recommendations in this blog post about derived state</a>.</p>\n</blockquote>\n<p><code class=\"gatsby-code-text\">UNSAFE_componentWillReceiveProps()</code> is invoked before a mounted component receives new props. If you need to update the state in response to prop changes (for example, to reset it), you may compare <code class=\"gatsby-code-text\">this.props</code> and <code class=\"gatsby-code-text\">nextProps</code> and perform state transitions using <code class=\"gatsby-code-text\">this.setState()</code> in this method.</p>\n<p>Note that if a parent component causes your component to re-render, this method will be called even if props have not changed. Make sure to compare the current and next values if you only want to handle changes.</p>\n<p>React doesn’t call <code class=\"gatsby-code-text\">UNSAFE_componentWillReceiveProps()</code> with initial props during <a href=\"#mounting\">mounting</a>. It only calls this method if some of component’s props may update. Calling <code class=\"gatsby-code-text\">this.setState()</code> generally doesn’t trigger <code class=\"gatsby-code-text\">UNSAFE_componentWillReceiveProps()</code>.</p>\n<hr>\n<h3 id=\"unsafe_componentwillupdate\"><a href=\"#unsafe_componentwillupdate\" aria-hidden 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\">UNSAFE_componentWillUpdate()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">UNSAFE_componentWillUpdate</span><span class=\"token punctuation\">(</span>nextProps<span class=\"token punctuation\">,</span> nextState<span class=\"token punctuation\">)</span></code></pre></div>\n<blockquote>\n<p>Note</p>\n<p>This lifecycle was previously named <code class=\"gatsby-code-text\">componentWillUpdate</code>. That name will continue to work until version 17. Use the <a href=\"https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">rename-unsafe-lifecycles</code> codemod</a> to automatically update your components.</p>\n</blockquote>\n<p><code class=\"gatsby-code-text\">UNSAFE_componentWillUpdate()</code> is invoked just before rendering when new props or state are being received. Use this as an opportunity to perform preparation before an update occurs. This method is not called for the initial render.</p>\n<p>Note that you cannot call <code class=\"gatsby-code-text\">this.setState()</code> here; nor should you do anything else (e.g. dispatch a Redux action) that would trigger an update to a React component before <code class=\"gatsby-code-text\">UNSAFE_componentWillUpdate()</code> returns.</p>\n<p>Typically, this method can be replaced by <code class=\"gatsby-code-text\">componentDidUpdate()</code>. If you were reading from the DOM in this method (e.g. to save a scroll position), you can move that logic to <code class=\"gatsby-code-text\">getSnapshotBeforeUpdate()</code>.</p>\n<blockquote>\n<p>Note</p>\n<p><code class=\"gatsby-code-text\">UNSAFE_componentWillUpdate()</code> will not be invoked if <a href=\"#shouldcomponentupdate\"><code class=\"gatsby-code-text\">shouldComponentUpdate()</code></a> returns false.</p>\n</blockquote>\n<hr>\n<h2 id=\"other-apis-1\"><a href=\"#other-apis-1\" aria-hidden 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 APIs </h2>\n<p>Unlike the lifecycle methods above (which React calls for you), the methods below are the methods <em>you</em> can call from your components.</p>\n<p>There are just two of them: <code class=\"gatsby-code-text\">setState()</code> and <code class=\"gatsby-code-text\">forceUpdate()</code>.</p>\n<h3 id=\"setstate\"><a href=\"#setstate\" aria-hidden 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\">setState()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">setState</span><span class=\"token punctuation\">(</span>updater<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span>callback<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p><code class=\"gatsby-code-text\">setState()</code> enqueues changes to the component state and tells React that this component and its children need to be re-rendered with the updated state. This is the primary method you use to update the user interface in response to event handlers and server responses.</p>\n<p>Think of <code class=\"gatsby-code-text\">setState()</code> as a <em>request</em> rather than an immediate command to update the component. For better perceived performance, React may delay it, and then update several components in a single pass. React does not guarantee that the state changes are applied immediately.</p>\n<p><code class=\"gatsby-code-text\">setState()</code> does not always immediately update the component. It may batch or defer the update until later. This makes reading <code class=\"gatsby-code-text\">this.state</code> right after calling <code class=\"gatsby-code-text\">setState()</code> a potential pitfall. Instead, use <code class=\"gatsby-code-text\">componentDidUpdate</code> or a <code class=\"gatsby-code-text\">setState</code> callback (<code class=\"gatsby-code-text\">setState(updater, callback)</code>), either of which are guaranteed to fire after the update has been applied. If you need to set the state based on the previous state, read about the <code class=\"gatsby-code-text\">updater</code> argument below.</p>\n<p><code class=\"gatsby-code-text\">setState()</code> will always lead to a re-render unless <code class=\"gatsby-code-text\">shouldComponentUpdate()</code> returns <code class=\"gatsby-code-text\">false</code>. If mutable objects are being used and conditional rendering logic cannot be implemented in <code class=\"gatsby-code-text\">shouldComponentUpdate()</code>, calling <code class=\"gatsby-code-text\">setState()</code> only when the new state differs from the previous state will avoid unnecessary re-renders.</p>\n<p>The first argument is an <code class=\"gatsby-code-text\">updater</code> function with the signature:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token punctuation\">(</span><span class=\"token parameter\">state<span class=\"token punctuation\">,</span> props</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> stateChange</code></pre></div>\n<p><code class=\"gatsby-code-text\">state</code> is a reference to the component state at the time the change is being applied. It should not be directly mutated. Instead, changes should be represented by building a new object based on the input from <code class=\"gatsby-code-text\">state</code> and <code class=\"gatsby-code-text\">props</code>. For instance, suppose we wanted to increment a value in state by <code class=\"gatsby-code-text\">props.step</code>:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><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 class=\"token parameter\">state<span class=\"token punctuation\">,</span> props</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">{</span>counter<span class=\"token operator\">:</span> state<span class=\"token punctuation\">.</span>counter <span class=\"token operator\">+</span> props<span class=\"token punctuation\">.</span>step<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Both <code class=\"gatsby-code-text\">state</code> and <code class=\"gatsby-code-text\">props</code> received by the updater function are guaranteed to be up-to-date. The output of the updater is shallowly merged with <code class=\"gatsby-code-text\">state</code>.</p>\n<p>The second parameter to <code class=\"gatsby-code-text\">setState()</code> is an optional callback function that will be executed once <code class=\"gatsby-code-text\">setState</code> is completed and the component is re-rendered. Generally we recommend using <code class=\"gatsby-code-text\">componentDidUpdate()</code> for such logic instead.</p>\n<p>You may optionally pass an object as the first argument to <code class=\"gatsby-code-text\">setState()</code> instead of a function:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">setState</span><span class=\"token punctuation\">(</span>stateChange<span class=\"token punctuation\">[</span><span class=\"token punctuation\">,</span> callback<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>This performs a shallow merge of <code class=\"gatsby-code-text\">stateChange</code> into the new state, e.g., to adjust a shopping cart item quantity:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><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>quantity<span class=\"token operator\">:</span> <span class=\"token number\">2</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>This form of <code class=\"gatsby-code-text\">setState()</code> is also asynchronous, and multiple calls during the same cycle may be batched together. For example, if you attempt to increment an item quantity more than once in the same cycle, that will result in the equivalent of:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"gatsby-code-javascript\"><code class=\"gatsby-code-javascript\">Object<span class=\"token punctuation\">.</span><span class=\"token function\">assign</span><span class=\"token punctuation\">(</span>\n  previousState<span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">{</span>quantity<span class=\"token operator\">:</span> state<span class=\"token punctuation\">.</span>quantity <span class=\"token operator\">+</span> <span class=\"token number\">1</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">{</span>quantity<span class=\"token operator\">:</span> state<span class=\"token punctuation\">.</span>quantity <span class=\"token operator\">+</span> <span class=\"token number\">1</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token operator\">...</span>\n<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Subsequent calls will override values from previous calls in the same cycle, so the quantity will only be incremented once. If the next state depends on the current state, we recommend using the updater function form, instead:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><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 class=\"token parameter\">state</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">{</span>quantity<span class=\"token operator\">:</span> state<span class=\"token punctuation\">.</span>quantity <span class=\"token operator\">+</span> <span class=\"token number\">1</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>For more detail, see:</p>\n<ul>\n<li><a href=\"/docs/state-and-lifecycle.html\">State and Lifecycle guide</a></li>\n<li><a href=\"https://stackoverflow.com/a/48610973/458193\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">In depth: When and why are <code class=\"gatsby-code-text\">setState()</code> calls batched?</a></li>\n<li><a href=\"https://github.com/facebook/react/issues/11527#issuecomment-360199710\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">In depth: Why isn’t <code class=\"gatsby-code-text\">this.state</code> updated immediately?</a></li>\n</ul>\n<hr>\n<h3 id=\"forceupdate\"><a href=\"#forceupdate\" aria-hidden 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\">forceUpdate()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\">component<span class=\"token punctuation\">.</span><span class=\"token function\">forceUpdate</span><span class=\"token punctuation\">(</span>callback<span class=\"token punctuation\">)</span></code></pre></div>\n<p>By default, when your component’s state or props change, your component will re-render. If your <code class=\"gatsby-code-text\">render()</code> method depends on some other data, you can tell React that the component needs re-rendering by calling <code class=\"gatsby-code-text\">forceUpdate()</code>.</p>\n<p>Calling <code class=\"gatsby-code-text\">forceUpdate()</code> will cause <code class=\"gatsby-code-text\">render()</code> to be called on the component, skipping <code class=\"gatsby-code-text\">shouldComponentUpdate()</code>. This will trigger the normal lifecycle methods for child components, including the <code class=\"gatsby-code-text\">shouldComponentUpdate()</code> method of each child. React will still only update the DOM if the markup changes.</p>\n<p>Normally you should try to avoid all uses of <code class=\"gatsby-code-text\">forceUpdate()</code> and only read from <code class=\"gatsby-code-text\">this.props</code> and <code class=\"gatsby-code-text\">this.state</code> in <code class=\"gatsby-code-text\">render()</code>.</p>\n<hr>\n<h2 id=\"class-properties-1\"><a href=\"#class-properties-1\" aria-hidden 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>Class Properties </h2>\n<h3 id=\"defaultprops\"><a href=\"#defaultprops\" aria-hidden 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\">defaultProps</code> </h3>\n<p><code class=\"gatsby-code-text\">defaultProps</code> can be defined as a property on the component class itself, to set the default props for the class. This is used for <code class=\"gatsby-code-text\">undefined</code> props, but not for <code class=\"gatsby-code-text\">null</code> props. For example:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">class</span> <span class=\"token class-name\">CustomButton</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>\n  <span class=\"token comment\">// ...</span>\n<span class=\"token punctuation\">}</span>\n\nCustomButton<span class=\"token punctuation\">.</span>defaultProps <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n  color<span class=\"token operator\">:</span> <span class=\"token string\">'blue'</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>If <code class=\"gatsby-code-text\">props.color</code> is not provided, it will be set by default to <code class=\"gatsby-code-text\">&#39;blue&#39;</code>:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><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>\n    <span class=\"token keyword\">return</span> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span><span class=\"token class-name\">CustomButton</span></span> <span class=\"token punctuation\">/></span></span> <span class=\"token punctuation\">;</span> <span class=\"token comment\">// props.color will be set to blue</span>\n  <span class=\"token punctuation\">}</span></code></pre></div>\n<p>If <code class=\"gatsby-code-text\">props.color</code> is set to <code class=\"gatsby-code-text\">null</code>, it will remain <code class=\"gatsby-code-text\">null</code>:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><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>\n    <span class=\"token keyword\">return</span> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span><span class=\"token class-name\">CustomButton</span></span> <span class=\"token attr-name\">color</span><span class=\"token script language-javascript\"><span class=\"token script-punctuation punctuation\">=</span><span class=\"token punctuation\">{</span><span class=\"token keyword\">null</span><span class=\"token punctuation\">}</span></span> <span class=\"token punctuation\">/></span></span> <span class=\"token punctuation\">;</span> <span class=\"token comment\">// props.color will remain null</span>\n  <span class=\"token punctuation\">}</span></code></pre></div>\n<hr>\n<h3 id=\"displayname\"><a href=\"#displayname\" aria-hidden 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\">displayName</code> </h3>\n<p>The <code class=\"gatsby-code-text\">displayName</code> string is used in debugging messages. Usually, you don’t need to set it explicitly because it’s inferred from the name of the function or class that defines the component. You might want to set it explicitly if you want to display a different name for debugging purposes or when you create a higher-order component, see <a href=\"/docs/higher-order-components.html#convention-wrap-the-display-name-for-easy-debugging\">Wrap the Display Name for Easy Debugging</a> for details.</p>\n<hr>\n<h2 id=\"instance-properties-1\"><a href=\"#instance-properties-1\" aria-hidden 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>Instance Properties </h2>\n<h3 id=\"props\"><a href=\"#props\" aria-hidden 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\">props</code> </h3>\n<p><code class=\"gatsby-code-text\">this.props</code> contains the props that were defined by the caller of this component. See <a href=\"/docs/components-and-props.html\">Components and Props</a> for an introduction to props.</p>\n<p>In particular, <code class=\"gatsby-code-text\">this.props.children</code> is a special prop, typically defined by the child tags in the JSX expression rather than in the tag itself.</p>\n<h3 id=\"state\"><a href=\"#state\" aria-hidden 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\">state</code> </h3>\n<p>The state contains data specific to this component that may change over time. The state is user-defined, and it should be a plain JavaScript object.</p>\n<p>If some value isn’t used for rendering or data flow (for example, a timer ID), you don’t have to put it in the state. Such values can be defined as fields on the component instance.</p>\n<p>See <a href=\"/docs/state-and-lifecycle.html\">State and Lifecycle</a> for more information about the state.</p>\n<p>Never mutate <code class=\"gatsby-code-text\">this.state</code> directly, as calling <code class=\"gatsby-code-text\">setState()</code> afterwards may replace the mutation you made. Treat <code class=\"gatsby-code-text\">this.state</code> as if it were immutable.</p>","frontmatter":{"title":"React.Component","next":null,"prev":null},"fields":{"path":"content/docs/reference-react-component.md","slug":"docs/react-component.html"}}},"pageContext":{"slug":"docs/react-component.html"}},"staticQueryHashes":[]}