{"componentChunkName":"component---src-components-blog-template-js","path":"/blog/react-state-props/","result":{"data":{"markdownRemark":{"frontmatter":{"title":"React State and Props","date":"2019-03-21"},"html":"<p>The first time I learned about React state and props, I was confused and incorrectly implemented it in a project until I read more about it and understand the differences between those two. Therefore, I want to share how to use state and props properly in React.</p>\n<p>Both state and props are variables in a React component. One obvious difference would be: props are variables (or should I say: constants) passed down to a child component, it is a way of communication between parent and child components.\nTake a look at the code below:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token operator\">&lt;</span>Header title<span class=\"token operator\">=</span><span class=\"token string\">'Hello World'</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span></code></pre></div>\n<p>The Header component above is receiving a title as props with value <code class=\"language-text\">\"Hello World\"</code>. It can be accessed inside the Header component using <code class=\"language-text\">this.props.title</code>.\nAnother difference would be: props are immutable while states are mutable, and the way to change state is via a function called <code class=\"language-text\">setState</code>. A state is usually used to hold data that changes over time, and changes in the state causes that component to re-render.</p>\n<p>A state is usually initialised inside a constructor of a component, like the code below:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">class</span> <span class=\"token class-name\">Header</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>\n      <span class=\"token literal-property property\">color</span><span class=\"token operator\">:</span> <span class=\"token string\">'blue'</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>As the above state initialised, we can later change the data inside the state, for example, color can be changed to red via the code below:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><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 literal-property property\">color</span><span class=\"token operator\">:</span> 'red<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>One common mistake for a beginner user of React is initialising state from props. Many would say that this is an anti-pattern because it creates a second source of truth which can lead to bugs. However, there is a trick as this quote:</p>\n<blockquote>\n<p>It's <strong>not</strong> an anti-pattern if you make it clear that the props is only seed data for the component's internally-controlled state</p>\n</blockquote>\n<p>So I think as long as we follow the rule above, bugs can be avoided.</p>"}},"pageContext":{"slug":"/react-state-props/"}},"staticQueryHashes":[]}