{"id":59102,"date":"2023-10-21T14:24:52","date_gmt":"2023-10-21T08:54:52","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=59102"},"modified":"2024-01-02T17:36:59","modified_gmt":"2024-01-02T12:06:59","slug":"react-hooks-state-and-effects-guide","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/react-hooks-state-and-effects-guide\/","title":{"rendered":"React Hooks: State and Effects Guide"},"content":{"rendered":"<div class=\"mt\">\n<div class=\"ab ca\">\n<div class=\"mu mv mw mx my mz ce na cf nb ch bg\">\n<figure class=\"nf ng nh ni nj mt nk nl paragraph-image\">\n<div class=\"nm nn eb no bg np\" role=\"button\">\n<div class=\"nc nd ne\"><img decoding=\"async\" loading=\"lazy\" class=\"bg nq nr c\" role=\"presentation\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/0Z_l8mnSCpyxp26_5.png\" alt=\"\" width=\"1000\" height=\"563\" \/><\/div>\n<\/div>\n<\/figure>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"ab ca\">\n<div class=\"ch bg et eu ev ew\">\n<p id=\"7179\" class=\"pw-post-body-paragraph lv lw fo lx b ly ns ma mb mc nt me mf mg nu mi mj mk nv mm mn mo nw mq mr ms fh bj\" data-selectable-paragraph=\"\">React Hooks were introduced in version 16.8 to revolutionize how state and other React features are handled in functional components, eliminating the need for class components. They provide a clean and intuitive way to manage state, side effects, and access key React functionalities. This article will delve into the fundamental hooks in React and their practical applications.<\/p>\n<h3 id=\"9bca\" class=\"kx ky fo be kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu bj\">State Hooks<\/h3>\n<p id=\"3d89\" class=\"pw-post-body-paragraph lv lw fo lx b ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms fh bj\" data-selectable-paragraph=\"\">The\u00a0<code class=\"cw nx ny nz oa b\">useState<\/code>\u00a0hook allows functional components to incorporate state. It returns an array with two elements: the current state value and a function that allows you to update it.<\/p>\n<pre class=\"nf ng nh ni nj ob oa oc bo od ba bj\"><span id=\"b47b\" class=\"oe ky fo oa b bf of og l oh oi\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-title.class\">React<\/span>, { useState } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react\"<\/span>;\r\n\r\n<span class=\"hljs-comment\">\/\/ Functional component named Counter<\/span>\r\n<span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title.function\">Counter<\/span>() {\r\n  <span class=\"hljs-comment\">\/\/ useState hook to add state to functional component<\/span>\r\n  <span class=\"hljs-keyword\">const<\/span> [count, setCount] = <span class=\"hljs-title.function\">useState<\/span>(<span class=\"hljs-number\">0<\/span>);\r\n\r\n  <span class=\"hljs-keyword\">return<\/span> (\r\n    &lt;div&gt;\r\n      &lt;p&gt;Count: {count}&lt;\/p&gt;\r\n      &lt;button onClick={() =&gt; setCount(count + 1)}&gt;Increment&lt;\/button&gt;\r\n    &lt;\/div&gt;\r\n  );\r\n}\r\n\r\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-title.class\">Counter<\/span>;<\/span><\/pre>\n<p id=\"8d7c\" class=\"pw-post-body-paragraph lv lw fo lx b ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms fh bj\" data-selectable-paragraph=\"\"><code class=\"cw nx ny nz oa b\">useReducer<\/code>\u00a0is an alternative to\u00a0<code class=\"cw nx ny nz oa b\">useState,<\/code>\u00a0managing complex state logic. It takes a reducer function and an initial state.<\/p>\n<pre class=\"nf ng nh ni nj ob oa oc bo od ba bj\"><span id=\"e946\" class=\"oe ky fo oa b bf of og l oh oi\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-title.class\">React<\/span>, { useReducer } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\r\n\r\n<span class=\"hljs-comment\">\/\/ Initial state for the counter<\/span>\r\n<span class=\"hljs-keyword\">const<\/span> initialState = { <span class=\"hljs-attr\">count<\/span>: <span class=\"hljs-number\">0<\/span> };\r\n\r\n<span class=\"hljs-comment\">\/\/ Reducer function for managing state updates<\/span>\r\n<span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title.function\">reducer<\/span>(<span class=\"hljs-params\">state, action<\/span>) {\r\n  <span class=\"hljs-keyword\">switch<\/span> (action.<span class=\"hljs-property\">type<\/span>) {\r\n    <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">'increment'<\/span>:\r\n      <span class=\"hljs-keyword\">return<\/span> { <span class=\"hljs-attr\">count<\/span>: state.<span class=\"hljs-property\">count<\/span> + <span class=\"hljs-number\">1<\/span> }; <span class=\"hljs-comment\">\/\/ Increase count by 1<\/span>\r\n    <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">'decrement'<\/span>:\r\n      <span class=\"hljs-keyword\">return<\/span> { <span class=\"hljs-attr\">count<\/span>: state.<span class=\"hljs-property\">count<\/span> - <span class=\"hljs-number\">1<\/span> }; <span class=\"hljs-comment\">\/\/ Decrease count by 1<\/span>\r\n    <span class=\"hljs-attr\">default<\/span>:\r\n      <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">Error<\/span>(<span class=\"hljs-string\">'Unsupported action type'<\/span>); <span class=\"hljs-comment\">\/\/ Throw an error for unsupported actions<\/span>\r\n  }\r\n}\r\n\r\n<span class=\"hljs-comment\">\/\/ Functional component named Counter<\/span>\r\n<span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title.function\">Counter<\/span>() {\r\n  <span class=\"hljs-comment\">\/\/ useReducer hook to manage state with a reducer function<\/span>\r\n  <span class=\"hljs-keyword\">const<\/span> [state, dispatch] = <span class=\"hljs-title.function\">useReducer<\/span>(reducer, initialState);\r\n\r\n  <span class=\"hljs-keyword\">return<\/span> (\r\n    &lt;div&gt;\r\n      &lt;p&gt;Count: {state.count}&lt;\/p&gt;\r\n      &lt;button onClick={() =&gt; dispatch({ type: 'increment' })}&gt;Increment&lt;\/button&gt;\r\n      &lt;button onClick={() =&gt; dispatch({ type: 'decrement' })}&gt;Decrement&lt;\/button&gt;\r\n    &lt;\/div&gt;\r\n  );\r\n}\r\n\r\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-title.class\">Counter<\/span>;<\/span><\/pre>\n<h3 id=\"67d3\" class=\"kx ky fo be kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu bj\">Effect Hooks<\/h3>\n<p id=\"9b36\" class=\"pw-post-body-paragraph lv lw fo lx b ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms fh bj\" data-selectable-paragraph=\"\"><code class=\"cw nx ny nz oa b\">useEffect<\/code>\u00a0combines the functionalities of\u00a0<code class=\"cw nx ny nz oa b\">componentDidMount<\/code>,\u00a0<code class=\"cw nx ny nz oa b\">componentDidUpdate<\/code>, and\u00a0<code class=\"cw nx ny nz oa b\">componentWillUnmount<\/code>\u00a0into a single hook. It allows you to perform side effects in function components.<\/p>\n<pre class=\"nf ng nh ni nj ob oa oc bo od ba bj\"><span id=\"b3ad\" class=\"oe ky fo oa b bf of og l oh oi\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-title.class\">React<\/span>, { useState, useEffect } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\r\n\r\n<span class=\"hljs-comment\">\/\/ Functional component named DataFetcher<\/span>\r\n<span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title.function\">DataFetcher<\/span>() {\r\n  <span class=\"hljs-keyword\">const<\/span> [data, setData] = <span class=\"hljs-title.function\">useState<\/span>(<span class=\"hljs-literal\">null<\/span>);\r\n\r\n  <span class=\"hljs-comment\">\/\/ useEffect hook for fetching data<\/span>\r\n  <span class=\"hljs-title.function\">useEffect<\/span>(<span class=\"hljs-function\">() =&gt;<\/span> {\r\n    <span class=\"hljs-comment\">\/\/ Fetch data from an API<\/span>\r\n    <span class=\"hljs-title.function\">fetch<\/span>(<span class=\"hljs-string\">'https:\/\/api.example.com\/data'<\/span>)\r\n      .<span class=\"hljs-title.function\">then<\/span>(<span class=\"hljs-function\">(<span class=\"hljs-params\">response<\/span>) =&gt;<\/span> response.<span class=\"hljs-title.function\">json<\/span>()) <span class=\"hljs-comment\">\/\/ Parse response as JSON<\/span>\r\n      .<span class=\"hljs-title.function\">then<\/span>(<span class=\"hljs-function\">(<span class=\"hljs-params\">data<\/span>) =&gt;<\/span> <span class=\"hljs-title.function\">setData<\/span>(data)); <span class=\"hljs-comment\">\/\/ Update 'data' state with fetched data<\/span>\r\n  }, []); <span class=\"hljs-comment\">\/\/ Empty dependency array means it only runs on mount<\/span>\r\n\r\n  <span class=\"hljs-comment\">\/\/ Display the message from 'data', or 'Loading...' if 'data' is null<\/span>\r\n  <span class=\"hljs-keyword\">return<\/span> &lt;p&gt;{data ? data.message : 'Loading...'}&lt;\/p&gt;;\r\n}\r\n\r\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-title.class\">DataFetcher<\/span>;<\/span><\/pre>\n<h3 id=\"2a9f\" class=\"lv lw om lx b ly ns ma mb mc nt me mf on nu mi mj oo nv mm mn op nw mq mr ms fh bj\"><strong class=\"lx fp\">Dependency Array<\/strong><\/h3>\n<p id=\"651d\" class=\"lv lw om lx b ly ns ma mb mc nt me mf on nu mi mj oo nv mm mn op nw mq mr ms fh bj\" data-selectable-paragraph=\"\">The second argument of\u00a0<code class=\"cw nx ny nz oa b\">useEffect<\/code>\u00a0is an array of dependencies. It determines when the effect will be re-run:<\/p>\n<p id=\"02ce\" class=\"lv lw om lx b ly ns ma mb mc nt me mf on nu mi mj oo nv mm mn op nw mq mr ms fh bj\" data-selectable-paragraph=\"\">1. The empty array means that the fetchData function will only be called on the initial render, and not on subsequent renders when the data state changes.<\/p>\n<p id=\"b20e\" class=\"lv lw om lx b ly ns ma mb mc nt me mf on nu mi mj oo nv mm mn op nw mq mr ms fh bj\" data-selectable-paragraph=\"\">2. If it contains dependencies, the effect will only run if any of the dependencies have changed since the last render.<\/p>\n<p id=\"1996\" class=\"pw-post-body-paragraph lv lw fo lx b ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms fh bj\" data-selectable-paragraph=\"\"><code class=\"cw nx ny nz oa b\">useLayoutEffect<\/code>\u00a0is a synchronous version of\u00a0<code class=\"cw nx ny nz oa b\">useEffect<\/code>\u00a0specifically designed for DOM mutations.<\/p>\n<pre class=\"nf ng nh ni nj ob oa oc bo od ba bj\"><span id=\"f8c8\" class=\"oe ky fo oa b bf of og l oh oi\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-title.class\">React<\/span>, { useState, useLayoutEffect } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react\"<\/span>;\r\n\r\n<span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title.function\">ExampleComponent<\/span>() {\r\n  <span class=\"hljs-keyword\">const<\/span> [width, setWidth] = <span class=\"hljs-title.function\">useState<\/span>(<span class=\"hljs-number\">0<\/span>);\r\n\r\n  <span class=\"hljs-comment\">\/\/ useLayoutEffect hook for measuring element width<\/span>\r\n  <span class=\"hljs-title.function\">useLayoutEffect<\/span>(<span class=\"hljs-function\">() =&gt;<\/span> {\r\n    <span class=\"hljs-comment\">\/\/ Get the width of the element with the ID 'myElement'<\/span>\r\n    <span class=\"hljs-keyword\">const<\/span> newWidth = <span class=\"hljs-variable.language\">document<\/span>.<span class=\"hljs-title.function\">getElementById<\/span>(<span class=\"hljs-string\">\"myElement\"<\/span>).<span class=\"hljs-property\">offsetWidth<\/span>;\r\n    <span class=\"hljs-title.function\">setWidth<\/span>(newWidth);\r\n  }, []); <span class=\"hljs-comment\">\/\/ Empty dependency array means it only runs on mount<\/span>\r\n\r\n  <span class=\"hljs-keyword\">return<\/span> (\r\n    &lt;div&gt;\r\n      &lt;div id=\"myElement\"&gt;Resizable Element&lt;\/div&gt;\r\n      &lt;p&gt;Width: {width}px&lt;\/p&gt;\r\n    &lt;\/div&gt;\r\n  );\r\n}\r\n\r\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-title.class\">ExampleComponent<\/span>;<\/span><\/pre>\n<h3 id=\"8d8f\" class=\"kx ky fo be kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu bj\">Additional Hooks<\/h3>\n<p id=\"5208\" class=\"pw-post-body-paragraph lv lw fo lx b ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms fh bj\" data-selectable-paragraph=\"\">The\u00a0<code class=\"cw nx ny nz oa b\">useContext<\/code>\u00a0hook enables access to the value a React context provides in a functional component, eliminating the need for prop drilling.<\/p>\n<pre class=\"nf ng nh ni nj ob oa oc bo od ba bj\"><span id=\"c84f\" class=\"oe ky fo oa b bf of og l oh oi\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-title.class\">React<\/span>, { useContext, createContext } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react\"<\/span>;\r\n\r\n<span class=\"hljs-comment\">\/\/ Step 1: Create a Context<\/span>\r\n<span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-title.class\">MyContext<\/span> = <span class=\"hljs-title.function\">createContext<\/span>();\r\n\r\n<span class=\"hljs-comment\">\/\/ Step 2: Create a Provider Component<\/span>\r\n<span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title.function\">MyProvider<\/span>(<span class=\"hljs-params\">{ children }<\/span>) {\r\n  <span class=\"hljs-keyword\">const<\/span> sharedValue = <span class=\"hljs-string\">\"Hello from Context!\"<\/span>;\r\n\r\n  <span class=\"hljs-comment\">\/\/ Provide the shared value to the children<\/span>\r\n  <span class=\"hljs-keyword\">return<\/span> &lt;MyContext.Provider value={sharedValue}&gt;{children}&lt;\/MyContext.Provider&gt;;\r\n}\r\n\r\n<span class=\"hljs-comment\">\/\/ Step 3: Consume the Context with useContext<\/span>\r\n<span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title.function\">ChildComponent<\/span>() {\r\n  <span class=\"hljs-comment\">\/\/ Consume the value provided by MyContext<\/span>\r\n  <span class=\"hljs-keyword\">const<\/span> contextValue = <span class=\"hljs-title.function\">useContext<\/span>(<span class=\"hljs-title.class\">MyContext<\/span>);\r\n\r\n  <span class=\"hljs-comment\">\/\/ Display the context value<\/span>\r\n  <span class=\"hljs-keyword\">return<\/span> &lt;div&gt;{contextValue}&lt;\/div&gt;;\r\n}\r\n\r\n<span class=\"hljs-comment\">\/\/ Step 4: Use the Provider to wrap the component tree<\/span>\r\n<span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title.function\">App<\/span>() {\r\n  <span class=\"hljs-keyword\">return<\/span> (\r\n    &lt;MyProvider&gt;\r\n      &lt;ChildComponent \/&gt;\r\n    &lt;\/MyProvider&gt;\r\n  );\r\n}\r\n\r\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-title.class\">App<\/span>;<\/span><\/pre>\n<ul>\n<li id=\"e7b5\" class=\"lv lw om lx b ly ns ma mb mc nt me mf on nu mi mj oo nv mm mn op nw mq mr ms fh bj\">For more information about useContext, visit the [<a class=\"af oq\" href=\"https:\/\/reactjs.org\/docs\" target=\"_blank\" rel=\"noopener ugc nofollow\">React documentation<\/a>].<\/li>\n<\/ul>\n<ul>\n<li id=\"347e\" class=\"lv lw om lx b ly ns ma mb mc nt me mf on nu mi mj oo nv mm mn op nw mq mr ms fh bj\">Additionally, you can explore this detailed article on simplifying context consumption with useContext.\u00a0<a class=\"af oq\" href=\"https:\/\/medium.com\/@mansiteharia\/simplifying-context-consumption-with-usecontext-in-react-9aa4876283e9\" rel=\"noopener\">Click here<\/a><\/li>\n<\/ul>\n<h3 id=\"e4e3\" class=\"kx ky fo be kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu bj\">Additional Hooks<\/h3>\n<p id=\"a899\" class=\"pw-post-body-paragraph lv lw fo lx b ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms fh bj\" data-selectable-paragraph=\"\"><code class=\"cw nx ny nz oa b\">useRef<\/code>\u00a0return a mutable ref object that persists between renders.<\/p>\n<pre class=\"nf ng nh ni nj ob oa oc bo od ba bj\"><span id=\"46ab\" class=\"oe ky fo oa b bf of og l oh oi\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-title.class\">React<\/span>, { useRef, useEffect } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\r\n\r\n<span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title.function\">TextInput<\/span>() {\r\n  <span class=\"hljs-comment\">\/\/ Create a ref to hold the input element<\/span>\r\n  <span class=\"hljs-keyword\">const<\/span> inputRef = <span class=\"hljs-title.function\">useRef<\/span>(<span class=\"hljs-literal\">null<\/span>);\r\n\r\n  <span class=\"hljs-comment\">\/\/ useEffect to focus on the input element after component mounts<\/span>\r\n  <span class=\"hljs-title.function\">useEffect<\/span>(<span class=\"hljs-function\">() =&gt;<\/span> {\r\n    <span class=\"hljs-comment\">\/\/ Focus on the input element<\/span>\r\n    inputRef.<span class=\"hljs-property\">current<\/span>.<span class=\"hljs-title.function\">focus<\/span>();\r\n  }, []);\r\n\r\n  <span class=\"hljs-comment\">\/\/ Return an input element with the ref assigned<\/span>\r\n  <span class=\"hljs-keyword\">return<\/span> &lt;input ref={inputRef} type=\"text\" \/&gt;;\r\n}\r\n\r\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-title.class\">TextInput<\/span>;<\/span><\/pre>\n<p id=\"0e9a\" class=\"pw-post-body-paragraph lv lw fo lx b ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms fh bj\" data-selectable-paragraph=\"\"><code class=\"cw nx ny nz oa b\">useMemo<\/code>\u00a0memoizes function results based on dependencies.<\/p>\n<pre class=\"nf ng nh ni nj ob oa oc bo od ba bj\"><span id=\"ad20\" class=\"oe ky fo oa b bf of og l oh oi\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-title.class\">React<\/span>, { useMemo } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\r\n\r\n<span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title.function\">ExpensiveCalculation<\/span>(<span class=\"hljs-params\">{ data }<\/span>) {\r\n  <span class=\"hljs-comment\">\/\/ Use useMemo to memoize the result of the calculation<\/span>\r\n  <span class=\"hljs-keyword\">const<\/span> result = <span class=\"hljs-title.function\">useMemo<\/span>(<span class=\"hljs-function\">() =&gt;<\/span> {\r\n    <span class=\"hljs-comment\">\/\/ Perform complex calculations using data<\/span>\r\n    <span class=\"hljs-comment\">\/\/ Return the result<\/span>\r\n    <span class=\"hljs-comment\">\/\/ Note: The actual calculations should be placed here<\/span>\r\n  }, [data]); <span class=\"hljs-comment\">\/\/ The dependency array ensures recalculation if 'data' changes<\/span>\r\n\r\n  <span class=\"hljs-keyword\">return<\/span> &lt;p&gt;{result}&lt;\/p&gt;;\r\n}<\/span><\/pre>\n<p id=\"9fc3\" class=\"pw-post-body-paragraph lv lw fo lx b ly lz ma mb mc md me mf mg mh mi mj mk ml mm mn mo mp mq mr ms fh bj\" data-selectable-paragraph=\"\"><code class=\"cw nx ny nz oa b\">useCallback<\/code> memorizes a callback function. The main difference between useCallback and useMemo is the type of value they return. useCallback returns a memorized callback function, while useMemo returns a memorized value.<\/p>\n<pre class=\"nf ng nh ni nj ob oa oc bo od ba bj\"><span id=\"822a\" class=\"oe ky fo oa b bf of og l oh oi\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-title.class\">React<\/span>, { useCallback } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\r\n\r\n<span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title.function\">ParentComponent<\/span>() {\r\n  <span class=\"hljs-comment\">\/\/ Define a memoized callback function using useCallback<\/span>\r\n  <span class=\"hljs-keyword\">const<\/span> handleClick = <span class=\"hljs-title.function\">useCallback<\/span>(<span class=\"hljs-function\">() =&gt;<\/span> {\r\n    <span class=\"hljs-comment\">\/\/ Handle click event<\/span>\r\n    <span class=\"hljs-comment\">\/\/ Add your click event handling logic here<\/span>\r\n  }, []); <span class=\"hljs-comment\">\/\/ Empty dependency array means the callback doesn't depend on any external variables<\/span>\r\n\r\n  <span class=\"hljs-comment\">\/\/ Render the ChildComponent and pass the memoized callback as a prop<\/span>\r\n  <span class=\"hljs-keyword\">return<\/span> &lt;ChildComponent onClick={handleClick} \/&gt;;\r\n}\r\n\r\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-title.class\">ParentComponent<\/span>;<\/span><\/pre>\n<p id=\"7ce5\" class=\"pw-post-body-paragraph lv lw fo lx b ly ns ma mb mc nt me mf mg nu mi mj mk nv mm mn mo nw mq mr ms fh bj\" data-selectable-paragraph=\"\">By utilizing these hooks, you can significantly enhance the functionality and readability of your React functional components, making them more efficient and easier to manage. Incorporating hooks into your React applications empowers you to build robust and dynamic user interfaces with less boilerplate code.<\/p>\n<\/div>\n<\/div>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>React Hooks were introduced in version 16.8 to revolutionize how state and other React features are handled in functional components, eliminating the need for class components. They provide a clean and intuitive way to manage state, side effects, and access key React functionalities. This article will delve into the fundamental hooks in React and their [&hellip;]<\/p>\n","protected":false},"author":1673,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[3038,1],"tags":[5512,5511,55,4857],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59102"}],"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\/1673"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=59102"}],"version-history":[{"count":4,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59102\/revisions"}],"predecessor-version":[{"id":59651,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59102\/revisions\/59651"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=59102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=59102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=59102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}