{"id":55511,"date":"2022-09-23T14:00:30","date_gmt":"2022-09-23T08:30:30","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=55511"},"modified":"2022-09-29T14:13:18","modified_gmt":"2022-09-29T08:43:18","slug":"useeffect-hook-reactjs","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/useeffect-hook-reactjs\/","title":{"rendered":"useEffect Hook &#8211; Reactjs"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">React Hooks are the feature that was introduced in React version 16.8, allowing us to use state and other class components features in our functional components. Out of all the hooks introduced, there is one salient hook named the <strong>UseEffect<\/strong>.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The useEffect hook is a boon for the functional components. It basically allows us to execute side effects in our functional components. Side effects here are referring to api calls to the backend server, interacting with browser APIs, i.e. using document or window, and unanticipated timer functions such as setTimeout and setInterval. In short, the side effects are those whose results are in general unpredictable.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">However, we can perform side effects without using the mentioned hook within the component body itself.\u00a0\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Consider the below example:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55491 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-2022-09-18-at-1.39.52-PM.png\" alt=\"\" width=\"1359\" height=\"991\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-2022-09-18-at-1.39.52-PM.png 1359w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-2022-09-18-at-1.39.52-PM-300x219.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-2022-09-18-at-1.39.52-PM-1024x747.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-2022-09-18-at-1.39.52-PM-768x560.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-2022-09-18-at-1.39.52-PM-624x455.png 624w\" sizes=\"(max-width: 1359px) 100vw, 1359px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Here we are making an API call in the document body. It might not throw an error , but would definitely hamper the rendering process, which is why this is not the right technique. We can make use of useEffect for these side effects.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Below are the steps to use the UseEffect hook in our functional components:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><strong>Import it from \u201creact\u201d<\/strong><\/li>\n<li style=\"font-weight: 400;\"><strong>Call it above the returned JSX\u00a0<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\"><strong>Syntax:<\/strong>\u00a0<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55506 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-2022-09-10-at-12.48.22-PM.png\" alt=\"\" width=\"1069\" height=\"647\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-2022-09-10-at-12.48.22-PM.png 1069w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-2022-09-10-at-12.48.22-PM-300x182.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-2022-09-10-at-12.48.22-PM-1024x620.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-2022-09-10-at-12.48.22-PM-768x465.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-2022-09-10-at-12.48.22-PM-624x378.png 624w\" sizes=\"(max-width: 1069px) 100vw, 1069px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">useEffect takes two parameters:<\/span><\/p>\n<p><strong>\u00a0 \u00a0 \u00a0 \u00a01. Callback function<\/strong><\/p>\n<p><span style=\"font-weight: 400;\">This is where the required side effect is performed, which is called right after our component renders.<\/span><\/p>\n<p><strong>\u00a0 \u00a0 \u00a0 \u00a02. Array Of Dependencies<\/strong><\/p>\n<p><span style=\"font-weight: 400;\">This parameter is optional. By default, useEffect runs after every render, even the first one. With the help of the array parameter, we can call useEffect only after a particular state update by passing that state variable into this array.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Now consider the following example:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55507 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/3.png\" alt=\"\" width=\"1561\" height=\"1655\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/3.png 1561w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/3-283x300.png 283w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/3-966x1024.png 966w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/3-768x814.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/3-1449x1536.png 1449w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/3-624x662.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/3-24x24.png 24w\" sizes=\"(max-width: 1561px) 100vw, 1561px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">There are two state variables; counter and inputValue. Counter state variable is being updated with the click of the Increment button. And whenever we would increment the value of counter the console in the useEffect hook will be printed. The reason being, we have passed the counter state variable in the dependency array parameter of the useEffect hook.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">On the other hand, the same hook won\u2019t be called whenever we update the inputValue variable, since we haven\u2019t passed it in the array parameter. If we would have done so, the console in useEffect would have been called on both the state variables updation i.e., the counter and the inputValue variable.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">useEffect provides the functionality of three life cycle methods to the functional components:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><strong>componentDidMount()<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Once we pass an empty array as a second parameter in the useEffect hook, it runs only once, i.e. at the time of component mounting.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <strong>\u00a0 Syntax:\u00a0<\/strong><\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55508 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/4.png\" alt=\"\" width=\"949\" height=\"879\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/4.png 949w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/4-300x278.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/4-768x711.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/4-624x578.png 624w\" sizes=\"(max-width: 949px) 100vw, 949px\" \/><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li style=\"font-weight: 400;\"><strong>componentDidUpdate()<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">For the hook to be called at a certain state update or more than one state update, we have to pass those state variables in the array of dependencies. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">This also includes being called at mounting as well.<\/span><\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55509 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/5.png\" alt=\"\" width=\"1283\" height=\"1736\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/5.png 1283w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/5-222x300.png 222w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/5-757x1024.png 757w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/5-768x1039.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/5-1135x1536.png 1135w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/5-624x844.png 624w\" sizes=\"(max-width: 1283px) 100vw, 1283px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Taking into consideration the above example, the useEffect hook will be called at the time of mounting as well as whenever counter and <\/span><span style=\"font-weight: 400;\">inputValue state variables are updated.<\/span><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li style=\"font-weight: 400;\"><strong>componentWillUnmount()<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">To have the hook implemented just as componentWillUnmount, we will have to return a function from it.\u00a0<\/span><\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-55510 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/6.png\" alt=\"\" width=\"1054\" height=\"1033\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/6.png 1054w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/6-300x294.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/6-1024x1004.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/6-768x753.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/6-624x612.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/6-24x24.png 24w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/6-48x48.png 48w\" sizes=\"(max-width: 1054px) 100vw, 1054px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Here, the console in the function returned from useEffect will be called when the component unmounts. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Hence, any necessary action to be performed during unmounting can be performed here.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Principally, actions in unmounting are executed to perform cleanup. Cleanup here is referring to cleaning up the code before the component gets unmounted for ex., clearing the interval. <\/span><span style=\"font-weight: 400;\">The cleanup can prevent memory leaks and remove unwanted things. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Some use-cases for the same are:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><strong>Clean up subscriptions<\/strong><\/li>\n<li style=\"font-weight: 400;\"><strong>Clean up modals<\/strong><\/li>\n<li style=\"font-weight: 400;\"><strong>Remove event listeners<\/strong><\/li>\n<li style=\"font-weight: 400;\"><strong>Clear timeouts<\/strong><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>React Hooks are the feature that was introduced in React version 16.8, allowing us to use state and other class components features in our functional components. Out of all the hooks introduced, there is one salient hook named the UseEffect. The useEffect hook is a boon for the functional components. It basically allows us to [&hellip;]<\/p>\n","protected":false},"author":1293,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":22},"categories":[3038],"tags":[2046],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55511"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1293"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=55511"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55511\/revisions"}],"predecessor-version":[{"id":55588,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55511\/revisions\/55588"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=55511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=55511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=55511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}