{"id":20675,"date":"2022-10-11T05:53:01","date_gmt":"2022-10-11T12:53:01","guid":{"rendered":"https:\/\/coderpad.io\/?p=20675"},"modified":"2023-06-05T14:02:18","modified_gmt":"2023-06-05T21:02:18","slug":"rules-of-react-hooks","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/rules-of-react-hooks\/","title":{"rendered":"Rules of React Hooks"},"content":{"rendered":"\n<p>Over the years, React has become one of the most used and loved JavaScript frameworks in the world.&nbsp;<\/p>\n\n\n\n<p>React is a component-based library with two standard ways of defining its components:&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>As class components&nbsp;<\/li>\n\n\n\n<li>As functional components&nbsp;<\/li>\n<\/ol>\n\n\n\n<p>You can use these two methods to achieve the same outcome but with different syntax.<\/p>\n\n\n\n<p>The class components are ES6 classes that return JSX and necessitate a React class extension, while the functional components are ES6 functions that return a React element as JSX. It is essential to know that they both return JSX (JavaScript XML), allowing us to write HTML elements in JavaScript. React then takes this JSX and places the elements in the DOM.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/img_634478f033db9.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/img_634478f033db9.png\" alt=\"A comparison between class based and functional component returning JSX with a heading of &quot;Hello World&quot;\"\/><\/a><\/figure>\n<\/div>\n\n\n<p>Before the introduction of React Hooks in version 16.8, the class component had always been the superior method of creating a component when you need to work with state (data management) and handle lifecycle methods, like component mounts, renders, updates, and unmounts.&nbsp;<\/p>\n\n\n\n<p>In those days, you could only use the functional component to render a user interface. With the introduction of Hooks in 2019, functional components gained the superpowers that class components already possessed, which meant that you could work with state and handle lifecycle methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are React Hooks?<\/h2>\n\n\n\n<p>React Hooks help us <strong>Hook into<\/strong> React features in functional components. These features include state and side effects (similar to lifecycle methods).&nbsp;<\/p>\n\n\n\n<p>The two most common React Hooks are <code>useState()<\/code> and <code>useEffect()<\/code>. The <code>useState()<\/code> Hook acts as a store that allows it to use state variables and update them. You can even destructure the <code>useState()<\/code> Hook to hold the state variable and update function separately. It also allows you to pass an initial value directly into the Hook.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/img_634478f1f18be.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/img_634478f1f18be.png\" alt=\"A comparison of how the class and functional components work with states and output state values within JSX.\"\/><\/a><\/figure>\n<\/div>\n\n\n<p>With the <code>useEffect()<\/code> Hook, you can efficiently perform side effects (similar to lifecycle methods) such as when your application mounts\/renders or when a value updates. You can read about <a href=\"https:\/\/coderpad.io\/blog\/development\/rules-of-reacts-useeffect\/\" target=\"_blank\" rel=\"noreferrer noopener\"><code>useEffect()<\/code>, and its rules here<\/a>.<\/p>\n\n\n\n<p>Understanding the rules that guide the operation of React Hooks will help you and your team avoid some unnecessary errors and bugs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The two rules of React Hooks<\/h2>\n\n\n\n<p>Two significant rules need to be followed when working with Hooks. These rules are essential to maintain order, avoid unnecessary bugs, and help us write clean code. These rules are:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Only call Hooks at the top level<\/li>\n\n\n\n<li>Only call Hooks from React functional components or other Hooks<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Only call Hooks at the top level of your functional component<\/h3>\n\n\n\n<p>The top level of a functional component is the base of your function body before you return your JSX elements. This is where you can call all your Hooks, so React can preserve and take note of the order in which these Hooks are called.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/img_634478f3070bf.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/img_634478f3070bf.png\" alt=\"A display of the top level of functional components and an example of the type of Hooks that can go into it.\"\/><\/a><\/figure>\n<\/div>\n\n\n<p>Calling your Hooks at the top of the function means you&nbsp;<strong>must not be calling them within loops, conditions, and nested functions<\/strong>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { useState, useEffect } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> App = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n\u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ \u2705 - correct<\/span>\n\u00a0 \u00a0 <span class=\"hljs-keyword\">const<\/span> &#91;todos, setTodos] = useState(&#91;]);\n\u00a0 \u00a0\n\u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ \u274c - Breaks the call order<\/span>\n\u00a0 \u00a0 <span class=\"hljs-keyword\">if<\/span> (todos) {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">const<\/span> &#91;count, setSetcount] = useState(todos.length);\n\u00a0 \u00a0 }\n\n\u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ \u274c - Breaks the call order<\/span>\n\u00a0 \u00a0 todos.forEach(<span class=\"hljs-function\"><span class=\"hljs-params\">todo<\/span> =&gt;<\/span> {\n\u00a0 \u00a0 \u00a0 \u00a0 useEffect(<span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-built_in\">console<\/span>.log(todos);\n\u00a0 \u00a0 \u00a0 \u00a0 });\n\u00a0 \u00a0 });\n\u00a0 \u00a0\n\u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ \u2705 - correct<\/span>\n\u00a0 \u00a0 useEffect(<span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-built_in\">console<\/span>.log(todos);\n\u00a0 \u00a0 });\n\n\u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> (\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ ...<\/span>\n\u00a0 \u00a0 );\n};\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> App;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It is necessary to call Hooks at the top level of your functional component to ensure that every time your component renders, these Hooks are called in the same order. <strong>The call order is essential for Hooks to work correctly.<\/strong><\/p>\n\n\n\n<p>For example, when you consider the code above, it uses the value of the <code>todos<\/code> state to decide whether to call the second and third Hook. When the condition fails, or something happens to the loop, it breaks the order. React will then have a hard time figuring out how to preserve the state of your component.<\/p>\n\n\n\n<p>This is what React does when you make use of Hooks. It identifies the order in which the Hooks are used at the initial render, then in subsequent renders, React will be able to preserve the state of your component.<\/p>\n\n\n\n<p>A perfect example to explain this would be this: you have a functional component with a state that holds the user&#8217;s data, such as the <code>firstName<\/code> and <code>age<\/code>. Then you get the <code>lastName<\/code> from another state. You now use the <code>useEffect()<\/code> Hook to update your page title to reflect both names.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> App = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n\u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ 1. Use the user state variable<\/span>\n\u00a0 \u00a0 <span class=\"hljs-keyword\">const<\/span> &#91;user, setUser] = useState({\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">firstName<\/span>: <span class=\"hljs-string\">'John'<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">age<\/span>: <span class=\"hljs-number\">20<\/span>,\n\u00a0 \u00a0 });\n\n\u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ 2. Use the lastName state variable<\/span>\n\u00a0 \u00a0 <span class=\"hljs-keyword\">const<\/span> &#91;lastName, setLastName] = useState(<span class=\"hljs-string\">'Doe'<\/span>);\n\n\u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ 3. Use an effect for updating the user details and updating the title<\/span>\n\u00a0 \u00a0 useEffect(<span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-built_in\">document<\/span>.title = user.firstName + <span class=\"hljs-string\">' '<\/span> + lastName;\n\u00a0 \u00a0 });\n\n\u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ ...<\/span>\n\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This example will always work because the order is constant, and nothing affects the order on every render:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ First render<\/span>\n<span class=\"hljs-comment\">\/\/ ---<\/span>\n<span class=\"hljs-comment\">\/\/ 1. Initialize the name state variable with an object containing name and age<\/span>\nuseState({ <span class=\"hljs-attr\">firstName<\/span>: <span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-attr\">age<\/span>: <span class=\"hljs-number\">20<\/span> })\n<span class=\"hljs-comment\">\/\/ 2. Initialize the lastName state variable with 'Doe'<\/span>\nuseState(<span class=\"hljs-string\">'Doe'<\/span>)\n<span class=\"hljs-comment\">\/\/ 3. Add an effect to update the title<\/span>\nuseEffect()\n\n<span class=\"hljs-comment\">\/\/ Second render<\/span>\n<span class=\"hljs-comment\">\/\/ ---<\/span>\n<span class=\"hljs-comment\">\/\/ 1. Read the user state variable (the argument is ignored)<\/span>\nuseState({ <span class=\"hljs-attr\">firstName<\/span>: <span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-attr\">age<\/span>: <span class=\"hljs-number\">20<\/span> })\n<span class=\"hljs-comment\">\/\/ 2. Read the lastName state variable (the argument is ignored)<\/span>\nuseState(<span class=\"hljs-string\">'Doe'<\/span>)\n<span class=\"hljs-comment\">\/\/ 3. Replace the effect for updating the title<\/span>\nuseEffect()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>But in a situation where you put one of these Hooks in a condition, the order will break when the condition returns <code>false<\/code>, leading to bugs:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">\u274c - Breaks the call order\n<span class=\"hljs-keyword\">if<\/span>(user.firstName){\n\u00a0 \u00a0 <span class=\"hljs-keyword\">const<\/span> &#91;lastName, setLastName] = useState(<span class=\"hljs-string\">'Doe'<\/span>);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The condition will be <code>true<\/code> on the first render, making React store the Hook&#8217;s order. If it returns <code>false<\/code> in the next render, it then affects any other Hook declared after this one and returns the wrong state for that Hook.<\/p>\n\n\n\n<p>If you <em>must <\/em>have a condition, then you can take this Hook out of the condition statement and use the state update function (<code>setLastName()<\/code> in our example) instead:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> &#91;lastName, setLastName] = useState(<span class=\"hljs-string\">''<\/span>);\n\n<span class=\"hljs-keyword\">if<\/span>(users.firstName &amp;&amp; !lastName){\n\u00a0 \u00a0 setLastName(<span class=\"hljs-string\">'Doe'<\/span>);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If it is a <code>useEffect()<\/code> Hook, you can write your conditions or nested functions or loops within the Hook itself:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">useEffect(<span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n\u00a0 \u00a0 <span class=\"hljs-keyword\">if<\/span> (condition) {\n\u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ statement<\/span>\n\u00a0 \u00a0 }\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Only call Hooks from React functions<\/h3>\n\n\n\n<p>A second rule is that Hooks should only be used and called in <strong>React functional components or custom Hooks<\/strong>, not regular JavaScript functions or React class components.<\/p>\n\n\n\n<p>The example below is invalid because it is a standard JavaScript function. Even if you decided to import <code>useState()<\/code>, it won&#8217;t work because it&#8217;s not a React component or custom Hook.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { useState } = <span class=\"hljs-string\">\"react\"<\/span>;\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">getName<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n\u00a0 \u00a0 <span class=\"hljs-keyword\">const<\/span> &#91;name, setName] = useState(<span class=\"hljs-string\">\"John Doe\"<\/span>);\n\u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> name;\n}\n<span class=\"hljs-built_in\">document<\/span>.getElementById(<span class=\"hljs-string\">\"user-name\"<\/span>).innerHTML = getName();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To create a custom Hook, you simply create a JavaScript function where the function&#8217;s name begins with <code>use<\/code>. From there, you can use it to call other Hooks. For example, here\u2019s an implementation of the custom Hook <code>useMyName<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">useMyName<\/span>(<span class=\"hljs-params\">name<\/span>) <\/span>{\n\u00a0 \u00a0 <span class=\"hljs-keyword\">const<\/span> &#91;state, setState] = useState(value);\n\u00a0 \u00a0\n\u00a0 \u00a0 useEffect(<span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n\u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ ...<\/span>\n\u00a0 \u00a0 });\n\u00a0 \u00a0\n\u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> anyThing;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can learn more about custom Hooks and how to create yours in the <a href=\"https:\/\/reactjs.org\/docs\/hooks-custom.html\" target=\"_blank\" rel=\"noreferrer noopener\">official React documentation<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Enforcing Rules With ESLint Plug-in&nbsp;<\/h2>\n\n\n\n<p>Knowing these rules is not enough; you must enforce and abide by them too. It is easy to follow these rules when working alone on a project, but when working in a team or on an external project, it is a good idea to devise ways to check if these rules are followed to reduce the chance of introducing bugs into the code.<\/p>\n\n\n\n<p>React released an ESLint plugin called <a href=\"https:\/\/www.npmjs.com\/package\/eslint-plugin-react-hooks\" target=\"_blank\" rel=\"noreferrer noopener\"><code>eslint-plugin-react-hooks<\/code><\/a> to help enforce both React Hook rules. If you want to try it out, you can do so by installing it with the following command:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">npm install eslint-plugin-react-hooks --save-dev<\/code><\/span><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ Your ESLint configuration<\/span>\n{\n\u00a0 \u00a0 <span class=\"hljs-attr\">\"plugins\"<\/span>: &#91;\n\u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ ...<\/span>\n\u00a0 \u00a0 <span class=\"hljs-string\">\"react-hooks\"<\/span>\n\u00a0 \u00a0 ],\n\u00a0 \u00a0 <span class=\"hljs-attr\">\"rules\"<\/span>: {\n\u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ ...<\/span>\n\u00a0 \u00a0 <span class=\"hljs-attr\">\"react-hooks\/rules-of-hooks\"<\/span>: <span class=\"hljs-string\">\"error\"<\/span>, <span class=\"hljs-comment\">\/\/ Checks rules of Hooks<\/span>\n\u00a0 \u00a0 <span class=\"hljs-attr\">\"react-hooks\/exhaustive-deps\"<\/span>: <span class=\"hljs-string\">\"warn\"<\/span> <span class=\"hljs-comment\">\/\/ Checks effect dependencies<\/span>\n\u00a0 \u00a0 }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>\u2139\ufe0f If you installed React via <a href=\"https:\/\/reactjs.org\/docs\/create-a-new-react-app.html#create-react-app\" target=\"_blank\" rel=\"noreferrer noopener\">Create React App<\/a>, then this plugin is installed by default, and you will notice it will always flag situations where you don&#8217;t abide by the rules with errors like this:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/img_634478f3b2d2d.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/img_634478f3b2d2d.png\" alt=\"An error that appears when you break one of the rules. This occurs when you make use of the React Hooks eslint plugin.\"\/><\/a><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Try it out for yourself!<\/h2>\n\n\n\n<p>You can take a turn implementing React Hook rules for yourself:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=230849&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping up<\/h2>\n\n\n\n<p>In this article, you have learned what Hooks are, why and when you should use Hooks, and what they can do. You&#8217;ve also learned that the order in which your Hooks are called is vital to React.&nbsp;<\/p>\n\n\n\n<p>You should always call your Hooks at the top level of your components. Furthermore, you can use these Hooks only in React functional components and callback Hooks, not in JavaScript functions.<\/p>\n\n\n\n<p>Thanks for reading, and have fun coding!<\/p>\n\n\n\n<p><em>I\u2019m Joel Olawanle, a front-end developer and technical writer interested in making the web accessible to everyone by always looking for ways to give back to the tech community. Follow me and connect with me on <\/em><a href=\"https:\/\/twitter.com\/olawanle_joel\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Twitter<\/em><\/a><em>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since the introduction of React Hooks in v16.8.0, React Hooks is widely used by developers who prefer to write functional components to class components. In this short but detailed article, you will be taught the rules of React Hooks to improve your web development skills.<\/p>\n","protected":false},"author":12,"featured_media":20725,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[61],"keyword-cluster":[],"class_list":["post-20675","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/20675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=20675"}],"version-history":[{"count":85,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/20675\/revisions"}],"predecessor-version":[{"id":32656,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/20675\/revisions\/32656"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/20725"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=20675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=20675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=20675"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=20675"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=20675"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=20675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}