{"id":729,"date":"2024-02-02T09:31:27","date_gmt":"2024-02-02T09:31:27","guid":{"rendered":"https:\/\/www.cmsgalaxy.com\/blog\/?p=729"},"modified":"2024-02-05T11:06:45","modified_gmt":"2024-02-05T11:06:45","slug":"hooks-in-react-js","status":"publish","type":"post","link":"https:\/\/www.cmsgalaxy.com\/blog\/hooks-in-react-js\/","title":{"rendered":"Hooks in React.js"},"content":{"rendered":"\n<p>In React.js, hooks are functions that enable functional components to use state and other React features without writing a class. They were introduced in React 16.8 as a way to use stateful logic in functional components, thereby simplifying component logic and promoting reusability.<\/p>\n\n\n\n<p>Here are some of the most commonly used React hooks:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>useState:<\/strong> <code>useState<\/code> hook allows functional components to manage state. It returns a stateful value and a function to update it. This hook replaces the need for <code>this.state<\/code> and <code>this.setState<\/code> in class components. Example:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   import React, { useState } from 'react';\n\n   function Counter() {\n       const &#91;count, setCount] = useState(0);\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>useEffect:<\/strong> <code>useEffect<\/code> hook is used to perform side effects in functional components. It runs after every render and allows you to perform operations like data fetching, subscriptions, or manually changing the DOM. 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           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=\"3\">\n<li><strong>useContext:<\/strong> <code>useContext<\/code> hook is used to access the value of a context object created by <code>React.createContext<\/code>. It allows you to consume values from context without nesting. Example:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   import React, { useContext } from 'react';\n   import MyContext from '.\/MyContext';\n\n   function MyComponent() {\n       const value = useContext(MyContext);\n       return &lt;p&gt;{value}&lt;\/p&gt;;\n   }<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li><strong>useReducer:<\/strong> <code>useReducer<\/code> hook is an alternative to <code>useState<\/code> for managing complex state logic. It is used when state transitions depend on the previous state. Example:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   import React, { useReducer } from 'react';\n\n   const initialState = { count: 0 };\n\n   function reducer(state, action) {\n       switch (action.type) {\n           case 'increment':\n               return { count: state.count + 1 };\n           case 'decrement':\n               return { count: state.count - 1 };\n           default:\n               throw new Error();\n       }\n   }\n\n   function Counter() {\n       const &#91;state, dispatch] = useReducer(reducer, initialState);\n\n       return (\n           &lt;div&gt;\n               &lt;p&gt;Count: {state.count}&lt;\/p&gt;\n               &lt;button onClick={() =&gt; dispatch({ type: 'increment' })}&gt;Increment&lt;\/button&gt;\n               &lt;button onClick={() =&gt; dispatch({ type: 'decrement' })}&gt;Decrement&lt;\/button&gt;\n           &lt;\/div&gt;\n       );\n   }<\/code><\/pre>\n\n\n\n<p>React hooks enable functional components to have state and lifecycle features previously only available in class components, making them more powerful and flexible. They encourage a more functional style of programming in React and facilitate the creation of cleaner and more modular code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In React.js, hooks are functions that enable functional components to use state and other React features without writing a class.<\/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-729","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/729","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=729"}],"version-history":[{"count":1,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/729\/revisions"}],"predecessor-version":[{"id":730,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/729\/revisions\/730"}],"wp:attachment":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/media?parent=729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/categories?post=729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/tags?post=729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}