{"componentChunkName":"component---src-templates-blog-post-js","path":"/react-hooks/","result":{"data":{"site":{"siteMetadata":{"title":"Freddy Bihozagara's Blog"}},"markdownRemark":{"id":"32a7da65-2c1f-5d9c-b3d6-72a4257f2e0a","excerpt":"React Hooks are a new addition in React 16.8. They allow the  use of state without writing a class. Example: Class based components require the use of classes…","html":"<p>React Hooks are a new addition in React 16.8. They allow the </p>\n<p>use of state without writing a class.</p>\n<p>Example:</p>\n<div class=\"gatsby-highlight\" data-language=\"js:\"><pre class=\"language-js:\"><code class=\"language-js:\">// for example, when updating state, for a counter function, while calling onclick()\n\nimport React, { useState } from &#39;react&#39;;\n\nfunction Counter() {\n\n  const [counter, setCounter] = useState(0);\n\n  return (\n    &lt;div&gt;\n      &lt;p&gt; {counter} times&lt;/p&gt;\n      &lt;button onClick={() =&gt; setCounter(counter + 1)}&gt;\n        Press\n      &lt;/button&gt;\n    &lt;/div&gt;\n  );\n}</code></pre></div>\n<p>Class based components require the use of classes, hence</p>\n<p>they may take more time to write. </p>\n<p>However, it is always good to start with class components.</p>\n<p>As developers we must not forget that we are dealing with</p>\n<p>classes and objects.</p>\n<p>It is always good to err on the side of more OOP than the</p>\n<p>other way.</p>\n<p>With class based components, as soon as we make use of</p>\n<p>a constructor, we know that there is a class that inherits</p>\n<p>from a super class. </p>\n<p>For more on React Hooks: <a href=\"https://react-hooks.org/\">https://react-hooks.org/</a></p>","frontmatter":{"title":"React Hooks vs class based components","date":"December 05, 2020","description":"React Hooks vs class based components"}},"previous":{"fields":{"slug":"/git-add/"},"frontmatter":{"title":"Git-add"}},"next":{"fields":{"slug":"/jsx/"},"frontmatter":{"title":"JSX and double brackets"}}},"pageContext":{"id":"32a7da65-2c1f-5d9c-b3d6-72a4257f2e0a","previousPostId":"a67e5c0e-8e1b-5ec6-ab8e-fc08334231b1","nextPostId":"c2cb1581-5bc7-5c72-afca-14feceeb6d1a"}},"staticQueryHashes":["2841359383","916993862"]}