{"id":4457,"date":"2020-03-04T19:29:21","date_gmt":"2020-03-04T19:29:21","guid":{"rendered":"https:\/\/tinjurewp.com\/jsblog\/?p=4457"},"modified":"2020-03-04T19:29:21","modified_gmt":"2020-03-04T19:29:21","slug":"learning-to-use-react-hooks","status":"publish","type":"post","link":"https:\/\/learncode.tinjurewp.com\/learning-to-use-react-hooks\/","title":{"rendered":"Learning Use React Hooks"},"content":{"rendered":"<p class=\"note icon-note\">Note: This post is part <strong>1<\/strong> of three-parts <strong><em>Understanding react hooks<\/em><\/strong> post series. This learning post is still in active development and updated regularly.<\/p>\n<p>React hooks were introduced in <a href=\"https:\/\/reactjs.org\/docs\/hooks-intro.html\" target=\"_blank\" rel=\"noopener noreferrer\">React 16.8 release<\/a>. React <a href=\"https:\/\/reactjs.org\/docs\/hooks-overview.html#but-what-is-a-hook\" target=\"_blank\" rel=\"noopener noreferrer\">Doc<\/a> describes hooks as &#8220;functions that let you &#8216;<em>hook into<\/em>&#8216; React state and lifecycle features from function components. Hooks don\u2019t work inside classes \u2014 they let you use React without classes.&#8221; In <a href=\"https:\/\/medium.com\/@dan_abramov\/making-sense-of-react-hooks-fdbde8803889\" target=\"_blank\" rel=\"noopener noreferrer\">making sense of React<\/a>, <a href=\"https:\/\/twitter.com\/dan_abramov\" target=\"_blank\" rel=\"noopener noreferrer\">Dan Abramov<\/a> writes &#8220;unlike patterns like render props or higher-order components, Hooks don\u2019t introduce unnecessary nesting into your component tree. They also don\u2019t suffer from the drawbacks of mixins&#8221;.<\/p>\n<p>The main objective of this learning series is to understand the React Hooks, get started with an Overview of Hooks, then learn some basic use case examples and learn more deeply React context API.<\/p>\n<div class=\"article-series\"><strong>Learning series<\/strong><br \/>\n<strong>Part 1<\/strong>: React Hooks &#8211; An Overview (<strong>this post<\/strong>)<br \/>\nPart 2: Using UseStaticQuery Hook in a Gatsby Site<br \/>\nPart 3: Using React Context API in Gatsby site<\/div>\n<p>The objective this part <strong>1<\/strong> of the React Hooks series is to get an overview of basic react hooks: <code>useState()<\/code>, <code>useEffect()<\/code> and <code>useContext()<\/code>.<\/p>\n<h6>Prerequisite<\/h6>\n<p>To fully understand the React Hooks, it is essential to understand <a href=\"https:\/\/reactjs.org\/docs\/state-and-lifecycle.html#adding-local-state-to-a-class\" target=\"_blank\" rel=\"noopener noreferrer\">React classes<\/a> and React <a href=\"https:\/\/reactjs.org\/docs\/state-and-lifecycle.html#adding-local-state-to-a-class\" target=\"_blank\" rel=\"noopener noreferrer\">State &amp; lifecycle<\/a>. In this learning series only an overview of React Hooks &amp; its simple use in Gatsby site will be discussed.<\/p>\n<p class=\"tip icon-tip\">Deep dive into <a href=\"https:\/\/reactjs.org\/docs\/state-and-lifecycle.html#adding-local-state-to-a-class\" target=\"_blank\" rel=\"noopener noreferrer\">React Classes<\/a>, <a href=\"https:\/\/reactjs.org\/docs\/state-and-lifecycle.html#adding-local-state-to-a-class\" target=\"_blank\" rel=\"noopener noreferrer\">State &amp; lifecycle<\/a> will be discussed in separate learning note series.<\/p>\n<h3>React Hooks &#8211; An Overview<\/h3>\n<h6>What are Hooks?<\/h6>\n<p>The <a href=\"https:\/\/reactjs.org\/blog\/2019\/02\/06\/react-v16.8.0.html\" target=\"_blank\" rel=\"noopener noreferrer\">React Doc<\/a> describes Hooks &#8220;let you use state and other React features without writing a class&#8221;. This also allows to write <strong>customized Hooks<\/strong> to share reusable stateful logic between components.<\/p>\n<p>The React hooks are regular functions that are imported directly from React library and can be used in React components. The <a href=\"https:\/\/reactjs.org\/docs\/hooks-reference.html\" target=\"_blank\" rel=\"noopener noreferrer\">React document<\/a> lists a total 10 hooks of which following <code>useState<\/code>, <code>useEffect<\/code> and <code>useContext<\/code> are described as the most <a href=\"https:\/\/reactjs.org\/docs\/hooks-reference.html#basic-hooks\" target=\"_blank\" rel=\"noopener noreferrer\">basic hooks<\/a>.<\/p>\n<h6><strong>The <code>useState<\/code> Hook<\/strong><\/h6>\n<p>The <a href=\"https:\/\/reactjs.org\/docs\/hooks-overview.html#state-hook\" target=\"_blank\" rel=\"noopener noreferrer\"><code>useState<\/code><\/a> hook is &#8220;similar to <code>this.state<\/code> in class component, except it doesn&#8217;t merge the old &amp; new state together&#8217;. This can be inside a function to add local state to it.<\/p>\n<p>The following <a class=\"author-name\" href=\"https:\/\/css-tricks.com\/author\/kinglseysilas\/\" target=\"_blank\" rel=\"noopener noreferrer\">Kingsley Silas <\/a> example from the <a href=\"https:\/\/css-tricks.com\/intro-to-react-hooks\/\" target=\"_blank\" rel=\"noopener noreferrer\">CSS-TRICKS post<\/a>.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-jsx\">\/\/source: https:\/\/css-tricks.com\/intro-to-react-hooks\/\nclass MyComponent extends React.component {\n  constructor(props)\n  super(props);\n  this.state = {\n    name: John Smith \/\/  default state value    \n  }\n}<\/code><\/pre>\n<p>In the Class Component example above, a class is initialize with this.state (lines: 5-6).\u00a0 The above example can be refactored using <code>useState<\/code> hook as shown below:<\/p>\n<pre class=\"line-numbers\"><code class=\"language-jsx\">\/\/source: https:\/\/css-tricks.com\/intro-to-react-hooks\/\nimport { useState } from &#039;react&#039;;\n    \nfunction MyComponent() {\n \/\/define &amp; call state variable\n  const [name, setName] = useState(&#039;John Smith&#039;);\n  \n  return\n    &lt;div&gt;\n      &lt;p&gt;Hello!, {name}&lt;\/p&gt;\n    &lt;\/div&gt;\n}\n\/\/ OUPUT\nHello! John smith<\/code><\/pre>\n<p>In this code example above, the <code>useState<\/code> hook is used outside of class and the state can be defined with an array containing two variables as (<code>name<\/code>) and call with (<code>setName<\/code>) inside a React component (line 6). The value of <code>name<\/code> variable is &#8216;<code>John smith<\/code>&#8216; where as <code>setName<\/code> is a function that can be used to update the value.<\/p>\n<p>The default <code>setName<\/code> &amp; <code>setOtherName<\/code> variables can be updated as described in <a href=\"https:\/\/reactjs.org\/docs\/hooks-state.html#recap\" target=\"_blank\" rel=\"noopener noreferrer\">this React Doc<\/a> example using multiple state variables (below):<\/p>\n<pre class=\"line-numbers\"><code class=\"language-jsx\">\/\/src\/app.js\nimport React, { useState } from &#039;react&#039;;\n\nfunction App() {\n  const [name, setName] = useState(&#039;Peter&#039;);\n  const [otherName, setOtherName] = useState(&#039;Barney Stinson&#039;);\n\n  return (\n    &lt;section className=&quot;main&quot;&gt;\n     &lt;p&gt;Hello, {name}!&lt;\/p&gt;\n     &lt;button onClick={() =&gt; setName(&#039;John Smith&#039;)}&gt;\n        Click me\n      &lt;\/button&gt;\n      &lt;div&gt;\n      &lt;p&gt;Hello, {otherName}!&lt;\/p&gt;\n      &lt;button onClick={() =&gt; setOtherName(&#039;Sam Golberg&#039;)}&gt;\n        Click me\n      &lt;\/button&gt;\n      &lt;\/div&gt;\n    &lt;\/section&gt;\n  );\n}\nexport default App;<\/code><\/pre>\n<p>In the example above first, <code>useState<\/code> hook was imported (line 2) which allows to use local state in functional component. Next, multiple state variables (<code>name<\/code> &amp; <code>setName<\/code>) were defined inside <strong>App<\/strong> component with a default value of &#8216;Peter&#8217;\u00a0 and &#8216;Berney Stinson&#8217; (lines: 5-6). The <code>name<\/code> state is the property and <code>setName<\/code> (a function) which holds the state of the <code>onClick<\/code> element (an updated value). Likewise, the <code>otherName<\/code> and <code>setOtherName<\/code> state hold the default value of state (John smith) &amp; updated value as &#8216;Sam Golberg&#8217;.\u00a0 As <a href=\"https:\/\/reactjs.org\/docs\/hooks-state.html#recap\" target=\"_blank\" rel=\"noopener noreferrer\">React doc<\/a> describes,\u00a0 when the button is clicked, the <code>setName<\/code> or <code>setOtherName<\/code> function is called then React re-renders passing the updated values (lines: 11 &amp; 16).<\/p>\n<figure id=\"attachment_4548\" aria-describedby=\"caption-attachment-4548\" style=\"width: 712px\" class=\"wp-caption alignnone\"><img decoding=\"async\" class=\"wp-image-4548 size-full lazyload\" src=\"data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\" data-src=\"https:\/\/tinjurewp.com\/jsblog\/wp-content\/uploads\/2020\/04\/h-state.jpg\" alt=\"\" width=\"712\" height=\"166\" \/><figcaption id=\"caption-attachment-4548\" class=\"wp-caption-text\"><noscript><img decoding=\"async\" class=\"wp-image-4548 size-full lazyload\" src=\"https:\/\/tinjurewp.com\/jsblog\/wp-content\/uploads\/2020\/04\/h-state.jpg\" alt=\"\" width=\"712\" height=\"166\" \/><\/noscript> Screenshot of OUTPUT of <code>name<\/code> &amp; <code>setName<\/code> attributes (left) and updated <code>otherName<\/code> &amp; <code>setOtherName<\/code> attributes value after click me (right).<\/figcaption><\/figure>\n<h6><strong>The <a href=\"https:\/\/reactjs.org\/docs\/hooks-overview.html#effect-hook\" target=\"_blank\" rel=\"noopener noreferrer\"><code>useEffect<\/code><\/a> Hook<\/strong><\/h6>\n<p>The operations like API calls, data fetching, changing the DOM in React component are <a href=\"https:\/\/reactjs.org\/docs\/hooks-overview.html#effect-hook\" target=\"_blank\" rel=\"noopener noreferrer\">described<\/a> as &#8220;side effects&#8221; (or &#8220;effects&#8221;). According to the <a href=\"https:\/\/reactjs.org\/docs\/hooks-overview.html#effect-hook\" target=\"_blank\" rel=\"noopener noreferrer\">React Document<\/a> &#8220;<code>useEffect<\/code> serves the same purpose as <code>componentDidMount<\/code>, <code>componentDidUpdate<\/code>, and <code>componentWillUnmount<\/code> in React classes, but unified into a single API.&#8221;<\/p>\n<p>The difference between the <code>setState()<\/code> and the <code>useEffect()<\/code> is that the later runs after render.<\/p>\n<p>The following example from <a href=\"https:\/\/medium.com\/@hossein98.ahmadi?source=post_page-----ecea3e90d84f----------------------\" target=\"_blank\" rel=\"noopener noreferrer\">Hussein Almadi<\/a>&#8216;s article <a href=\"https:\/\/medium.com\/javascript-in-plain-english\/react-hooks-how-to-use-useeffect-ecea3e90d84f\" target=\"_blank\" rel=\"noopener noreferrer\">React Hooks: How to use useEffect()<\/a> using two state (objects) name and <code>familyName<\/code>. Initial state would be \u201cname\u201d and \u201cfamily\u201d and after rendering the component it should be changed to something else.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-jsx\">\/\/ Source: Hussein Almadi&#039;s article\n\/\/ File: src\/app.js\nimport React, {useEffect, useState} from &#039;react&#039;;\n\nexport const App = () =&gt; {\n  \/\/State\n  const [fullName, setFullName] = useState({name: &#039;name&#039;, familyName: &#039;family&#039;});\n  const [title, setTitle] = useState(&#039;Using useEffect() Hooks&#039;);\n\n  \/\/useEffect\n  useEffect(() =&gt; {\n     \/\/console.log(&#039;useEffect has been called!&#039;);\n     setFullName({name:&#039;John&#039;,familyName: &#039;Smith&#039;});\n   },[]); \/\/Pass Array as second argument\n\n   return(\n     &lt;section className=&quot;main&quot;&gt;\n        &lt;h1&gt;Title: {title}&lt;\/h1&gt;\n        &lt;h5&gt;Name: {fullName.name}&lt;\/h5&gt;\n        &lt;h5&gt;Family Name: {fullName.familyName}&lt;\/h5&gt;\n     &lt;\/section&gt;\n    );\n};\n\nexport default App;<\/code><\/pre>\n<p>In the example above, just like the previous <code>useState<\/code> hook example, both the <code>useEffect()<\/code> and <code>useState()<\/code> can be imported together from react (line 2).<\/p>\n<p>First, states are defines (line: 7-8). Next, <code>useEffect()<\/code> function was defined with set<code>FullName<\/code> argument, this functions just like <code>componentDidUpdate<\/code> in class-based components. The <code>useEffect()<\/code> call only implemented when the second argument (eg. <code>setFullName<\/code>) gets changed (line 13). Because, a value of &#8220;John &#8221; and &#8220;Smith&#8221; was passed to the <code>setFullName<\/code> (second argument) its value changed and <code>useEffect()<\/code> (see output screenshot below).<\/p>\n<p>Just like in <code>componerntDidMount<\/code> which is called based on conditional statements, to use <code>useEffect()<\/code> function similarly we need to pass an empty array [] (line 14).<\/p>\n<figure id=\"attachment_4537\" aria-describedby=\"caption-attachment-4537\" style=\"width: 701px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-4537 lazyload\" src=\"data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\" data-src=\"https:\/\/tinjurewp.com\/jsblog\/wp-content\/uploads\/2020\/04\/hook-2.png\" alt=\"\" width=\"701\" height=\"222\" \/><figcaption id=\"caption-attachment-4537\" class=\"wp-caption-text\"><noscript><img decoding=\"async\" class=\"size-full wp-image-4537 lazyload\" src=\"https:\/\/tinjurewp.com\/jsblog\/wp-content\/uploads\/2020\/04\/hook-2.png\" alt=\"\" width=\"701\" height=\"222\" \/><\/noscript> Figure: Screenshot of App component output which uses <code>useEffect<\/code> hook to update changed value.<\/figcaption><\/figure>\n<h6>Fetching Data useEffect() Hook<\/h6>\n<p>The Hussein Ahmadi&#8217;s <a href=\"https:\/\/medium.com\/javascript-in-plain-english\/react-hooks-how-to-use-useeffect-ecea3e90d84f\" target=\"_blank\" rel=\"noopener noreferrer\">article<\/a> uses another simple example case <code>useEffect()<\/code> hook to fetch data from an external API like <a href=\"https:\/\/github.com\/axios\/axios\" target=\"_blank\" rel=\"noopener noreferrer\">Axios.<\/a> The following code snippets from <a href=\"https:\/\/medium.com\/javascript-in-plain-english\/react-hooks-how-to-use-useeffect-ecea3e90d84f\" target=\"_blank\" rel=\"noopener noreferrer\">Ahmadi&#8217;s article<\/a>:<\/p>\n<pre class=\"line-numbers\"><code class=\"language-jsx\">\/\/ code source: Hussein Ahmadi&#039;s article\nimport React, {useEffect, useState} from &#039;react&#039;;\nimport axios from &#039;axios&#039;;\n\nexport const CountryList = () =&gt; {\n  const [countries, setCountries] = useState([]);\n  const [load, setLoad] = useState(false);\n  const [error, setError] = useState(&#039;&#039;);\n    \n  useEffect(() =&gt; {\n     axios.get(&#039;https:\/\/restcountries.eu\/rest\/v2\/all&#039;)\n        .then(res =&gt; {\n           setCountries(res.data);\n           setLoad(true);\n         })\n        .catch(err =&gt; {\n           setError(err.message);\n           setLoad(true)\n        })\n  }, []);\n    \n   \n  if (load) {\n     return (&lt;ul&gt;\n       {error ? &lt;li&gt;{error.message}&lt;\/li&gt; \n          : countries.map((country, index) =&gt; \n          &lt;li key={index}&gt;{country.name}&lt;\/li&gt;)}\n       &lt;\/ul&gt;);\n    } else {\n       return (\n         &lt;div&gt;\n            Loading...\n         &lt;\/div&gt;\n     );\n    }\n};<\/code><\/pre>\n<p>In the example above from Hussein Ahmadi&#8217;s <a href=\"https:\/\/medium.com\/javascript-in-plain-english\/react-hooks-how-to-use-useeffect-ecea3e90d84f\" target=\"_blank\" rel=\"noopener noreferrer\">article<\/a> uses three variables (lines: 6-8):\u00a0 <strong class=\"hq ja\">Countries <\/strong>(to list of our country names), <strong class=\"hq ja\">Load<\/strong> (to show a loading text until our fetch process completes; it is to avoid async\/await TypeErrors) and <strong class=\"hq ja\">Error<\/strong> (to show error returns from our fetch method instead of the country list).\u00a0 An an empty array as second argument is passed (line: 20) so that <code>useEffect<\/code> functions like <code>componentDidMount<\/code>. The screen output is shown below:<\/p>\n<figure id=\"attachment_4544\" aria-describedby=\"caption-attachment-4544\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-4544 lazyload\" src=\"data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\" data-src=\"https:\/\/tinjurewp.com\/jsblog\/wp-content\/uploads\/2020\/04\/hook-3.png\" alt=\"\" width=\"660\" height=\"203\" \/><figcaption id=\"caption-attachment-4544\" class=\"wp-caption-text\"><noscript><img decoding=\"async\" class=\"size-full wp-image-4544 lazyload\" src=\"https:\/\/tinjurewp.com\/jsblog\/wp-content\/uploads\/2020\/04\/hook-3.png\" alt=\"\" width=\"660\" height=\"203\" \/><\/noscript> Screenshot (portion) of the output. Source: <a href=\"https:\/\/medium.com\/javascript-in-plain-english\/react-hooks-how-to-use-useeffect-ecea3e90d84f\" target=\"_blank\" rel=\"noopener noreferrer\">Ahmadi&#8217;s article<\/a>.<\/figcaption><\/figure>\n<p class=\"tip icon-tip\">Tip: If <a href=\"https:\/\/github.com\/axios\/axios\" target=\"_blank\" rel=\"noopener noreferrer\">axios<\/a> is already not installed, it should be installed as <a href=\"https:\/\/github.com\/axios\/axios#installing\" target=\"_blank\" rel=\"noopener noreferrer\">described in this document<\/a> &amp; should be used as described in <a href=\"https:\/\/github.com\/axios\/axios#example\">this Git Hub example<\/a>.<\/p>\n<h6><strong>The <code>useContext<\/code> Hook<\/strong><\/h6>\n<p class=\"note\">To understand <a href=\"https:\/\/reactjs.org\/docs\/hooks-reference.html#usecontext\" target=\"_blank\" rel=\"noopener noreferrer\"><code>useContext<\/code><\/a> hook, a basic understanding of <a href=\"https:\/\/reactjs.org\/docs\/context.html\" target=\"_blank\" rel=\"noopener noreferrer\">React Context<\/a> is essential, which will be discussed in another post separately.<\/p>\n<h6>Definition<\/h6>\n<p>According the <a href=\"https:\/\/reactjs.org\/docs\/getting-started.html\" target=\"_blank\" rel=\"noopener noreferrer\">React Doc<\/a> the &#8220;<a href=\"https:\/\/reactjs.org\/docs\/context.html\" target=\"_blank\" rel=\"noopener noreferrer\">Context<\/a> provides a way to pass data through the component tree without having to pass props down manually at every level.&#8221;<\/p>\n<p>The <a href=\"https:\/\/reactjs.org\/blog\/2018\/03\/29\/react-v-16-3.html#official-context-api\" target=\"_blank\" rel=\"noopener noreferrer\">React Context API<\/a> allows to store a state value and access the value down in the tree, without drilling down with props. The React Documentation describes it &#8220;<a href=\"https:\/\/reactjs.org\/docs\/hooks-reference.html#usecontext\"><code class=\"gatsby-code-text\">useContext<\/code><\/a> lets you subscribe to React context without introducing nesting as shown below:<\/p>\n<pre class=\"line-numbers\"><code class=\"language-jsx\">\/\/ code source React Doc\nfunction Example() {\n  const locale = useContext(LocaleContext);\n  const theme = useContext(ThemeContext);\n  \/\/ ...\n}<\/code><\/pre>\n<p>The <a href=\"https:\/\/reactjs.org\/docs\/hooks-reference.html#usecontext\" target=\"_blank\" rel=\"noopener noreferrer\"><code>UseContext<\/code><\/a> API consists of three main parts:<\/p>\n<ul>\n<li><code>React.createContext<\/code> which is passed the initial <code>value<\/code>. This returns an object with a <code>Provider<\/code> and a <code>Consumer<\/code>. The context <code>value<\/code> is determined\u00a0 by the value prop from the nearest <code>provider<\/code> above.<\/li>\n<li>The <code>Provider<\/code> component is used higher in the component hierarchy and accepts a prop called <code>value<\/code> (which can be anything).<\/li>\n<li>The <code>Consumer<\/code> component is used anywhere below the provider in the component hierarchy and accepts a prop called \u201cchildren\u201d which must be a function that accepts the value and must return a react element (JSX).<\/li>\n<li>The argument to <code>useContext<\/code> must be the context object itself.<\/li>\n<\/ul>\n<p>The following example from the <a href=\"https:\/\/reactjs.org\/blog\/2018\/03\/29\/react-v-16-3.html\" target=\"_blank\" rel=\"noopener noreferrer\">React Blog<\/a> illustrates use of context API to inject a \u201ctheme\u201d:<\/p>\n<pre class=\"line-numbers\" data-line=\"8-10, 17-19\"><code class=\"language-jsx\">\/\/ example from React Blog\nconst ThemeContext = React.createContext(&#039;light&#039;);\nclass ThemeProvider extends React.Component {\n  state = {theme: &#039;light&#039;};\n\n  render() {\n    return (\n      &lt;ThemeContext.Provider value={this.state.theme}&gt;   \n        {this.props.children}      \n      &lt;\/ThemeContext.Provider&gt;    );\n  }\n}\n\nclass ThemedButton extends React.Component {\n  render() {\n    return (\n      &lt;ThemeContext.Consumer&gt;        \n        {theme =&gt; &lt;Button theme={theme} \/&gt;}      \n      &lt;\/ThemeContext.Consumer&gt;    );\n  }\n}<\/code><\/pre>\n<p>Explanation &amp; additional use cases will described in part 3 of this learning series. Additional Information: <a href=\"https:\/\/reactjs.org\/docs\/context.html\" target=\"_blank\" rel=\"noopener noreferrer\">Context API and use case examples<\/a><\/p>\n<h6>Wrapping Up<\/h6>\n<p>In this learning post series an an overview of three basic react hooks: <code>useState()<\/code>, <code>useEffect()<\/code> and <code>useContext()<\/code> was described with some basic use case examples. More detailed descriptions <a href=\"https:\/\/dev.to\/dan_abramov\/making-sense-of-react-hooks-2eib\" target=\"_blank\" rel=\"noopener noreferrer\">rationale of hooks<\/a> in React will be described in a separate learning post series.<\/p>\n<p>Next Post: Using <code>UseStaticQuery<\/code> Hook in a Gatsby Site<\/p>\n<h6>Useful Resources<\/h6>\n<p>While preparing this post, I have referred the following references extensively. Please refer to original posts for more detailed information.<\/p>\n<ul>\n<li><a href=\"https:\/\/reactjs.org\/docs\/hooks-intro.html\" target=\"_blank\" rel=\"noopener noreferrer\">Introducing Hooks<\/a> | <a href=\"https:\/\/reactjs.org\/docs\/getting-started.html\" target=\"_blank\" rel=\"noopener noreferrer\">React Docs<\/a><\/li>\n<li><a href=\"https:\/\/dev.to\/dan_abramov\/making-sense-of-react-hooks-2eib\" target=\"_blank\" rel=\"noopener noreferrer\">Making Sense of React Hooks<\/a> by <a href=\"http:\/\/twitter.com\/dan_abramov\" target=\"_blank\" rel=\"noopener noreferrer\">Dan Abramov<\/a> | <a href=\"https:\/\/dev.to\/\" target=\"_blank\" rel=\"noopener noreferrer\">dev.to<\/a><\/li>\n<li><a href=\"https:\/\/css-tricks.com\/intro-to-react-hooks\/\" target=\"_blank\" rel=\"noopener noreferrer\">Intro to React Hooks<\/a> |CSS-Tricks<\/li>\n<li><a href=\"https:\/\/www.valentinog.com\/blog\/hooks\/\" target=\"_blank\" rel=\"noopener noreferrer\">React Hooks Tutorial for Beginners: Getting Started With React Hooks<\/a> | <a href=\"https:\/\/www.valentinog.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">valentinog.com<\/a><\/li>\n<li><a href=\"https:\/\/medium.com\/javascript-in-plain-english\/react-hooks-how-to-use-useeffect-ecea3e90d84f\" target=\"_blank\" rel=\"noopener noreferrer\">React Hooks: How to use useEffect()<\/a> | Medium<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Note: This post is part 1 of three-parts Understanding react hooks post series. This learning post is still in active development and updated regularly. React hooks were introduced in React 16.8 release. React Doc describes hooks as &#8220;functions that let you &#8216;hook into&#8216; React state and lifecycle features from function components. Hooks don\u2019t work inside [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[34,5],"tags":[],"class_list":["post-4457","post","type-post","status-publish","format-standard","hentry","category-gatsbyjs","category-reactjs"],"_links":{"self":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts\/4457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/comments?post=4457"}],"version-history":[{"count":0,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts\/4457\/revisions"}],"wp:attachment":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/media?parent=4457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/categories?post=4457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/tags?post=4457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}