{"id":731,"date":"2024-02-02T09:39:22","date_gmt":"2024-02-02T09:39:22","guid":{"rendered":"https:\/\/www.cmsgalaxy.com\/blog\/?p=731"},"modified":"2024-02-05T11:08:36","modified_gmt":"2024-02-05T11:08:36","slug":"useeffect-useref-usecallback-hooks-in-react-js","status":"publish","type":"post","link":"https:\/\/www.cmsgalaxy.com\/blog\/useeffect-useref-usecallback-hooks-in-react-js\/","title":{"rendered":"useEffect, useRef, useCallback hooks in React.js"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li><strong>useEffect:<\/strong><br>The <code>useEffect<\/code> hook is used to perform side effects in functional components. It&#8217;s similar to lifecycle methods such as <code>componentDidMount<\/code>, <code>componentDidUpdate<\/code>, and <code>componentWillUnmount<\/code> in class components. Example:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   import React, { useState, useEffect } from 'react';\n\n   function Example() {\n       const &#91;count, setCount] = useState(0);\n\n       useEffect(() =&gt; {\n           \/\/ This function will run after every render\n           document.title = `You clicked ${count} times`;\n       });\n\n       return (\n           &lt;div&gt;\n               &lt;p&gt;Count: {count}&lt;\/p&gt;\n               &lt;button onClick={() =&gt; setCount(count + 1)}&gt;Increment&lt;\/button&gt;\n           &lt;\/div&gt;\n       );\n   }<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>useRef:<\/strong><br>The <code>useRef<\/code> hook is used to create a mutable reference that persists across renders. It&#8217;s useful for accessing and interacting with DOM elements, as well as storing mutable values that don&#8217;t trigger re-renders. Example:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   import React, { useRef } from 'react';\n\n   function TextInputWithFocusButton() {\n       const inputRef = useRef(null);\n\n       const handleClick = () =&gt; {\n           \/\/ Focus on the input element when the button is clicked\n           inputRef.current.focus();\n       };\n\n       return (\n           &lt;div&gt;\n               &lt;input ref={inputRef} type=\"text\" \/&gt;\n               &lt;button onClick={handleClick}&gt;Focus Input&lt;\/button&gt;\n           &lt;\/div&gt;\n       );\n   }<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>useCallback:<\/strong><br>The <code>useCallback<\/code> hook is used to memoize functions, preventing unnecessary re-renders of components that depend on those functions. It&#8217;s particularly useful for optimizing performance when passing callbacks to child components. Example:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   import React, { useState, useCallback } from 'react';\n\n   function ParentComponent() {\n       const &#91;count, setCount] = useState(0);\n\n       \/\/ Define a memoized callback function\n       const handleClick = useCallback(() =&gt; {\n           setCount(count + 1);\n       }, &#91;count]);\n\n       return (\n           &lt;div&gt;\n               &lt;p&gt;Count: {count}&lt;\/p&gt;\n               &lt;ChildComponent onClick={handleClick} \/&gt;\n           &lt;\/div&gt;\n       );\n   }\n\n   function ChildComponent({ onClick }) {\n       return (\n           &lt;button onClick={onClick}&gt;Increment&lt;\/button&gt;\n       );\n   }<\/code><\/pre>\n\n\n\n<p>These are just a few examples of how you can use <code>useEffect<\/code>, <code>useRef<\/code>, and <code>useCallback<\/code> hooks in React to handle side effects, manage references, and optimize performance. Each hook serves a specific purpose and can be used to enhance the functionality and efficiency of your React components.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>These are just a few examples of how you can use useEffect, useRef, and useCallback hooks in React to handle<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-731","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/comments?post=731"}],"version-history":[{"count":1,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/731\/revisions"}],"predecessor-version":[{"id":732,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/731\/revisions\/732"}],"wp:attachment":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/media?parent=731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/categories?post=731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/tags?post=731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}