<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>useContext() &#8211; MaXEster Technologies  | Technical Blog</title>
	<atom:link href="https://www.maxester.com/blog/tag/usecontext/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.maxester.com/blog</link>
	<description>Tutorials, Examples and Implementation code for Developers Help</description>
	<lastBuildDate>Mon, 17 Feb 2025 09:17:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.22</generator>
	<item>
		<title>What is Hooks in React JS?</title>
		<link>https://www.maxester.com/blog/2023/12/16/what-is-hooks-in-react-js/</link>
		<comments>https://www.maxester.com/blog/2023/12/16/what-is-hooks-in-react-js/#respond</comments>
		<pubDate>Sat, 16 Dec 2023 10:23:45 +0000</pubDate>
		<dc:creator><![CDATA[Mayank Jha]]></dc:creator>
				<category><![CDATA[React Js]]></category>
		<category><![CDATA[React Js Hooks]]></category>
		<category><![CDATA[useContext()]]></category>
		<category><![CDATA[useEffect()]]></category>
		<category><![CDATA[useReducer()]]></category>
		<category><![CDATA[useRef()]]></category>
		<category><![CDATA[useState()]]></category>

		<guid isPermaLink="false">https://www.maxester.com/blog/?p=1170</guid>
		<description><![CDATA[<p>Hooks in react js are functions, which let you to use state inside the functional components. Hooks are used to associate the state to the functional component. Before the hooks were introduced, if we wanted to use the states in&#8230;</p>
<p><a href="https://www.maxester.com/blog/2023/12/16/what-is-hooks-in-react-js/" class="btn-more">Read More<span class="arrow-more">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="https://www.maxester.com/blog/2023/12/16/what-is-hooks-in-react-js/">What is Hooks in React JS?</a> appeared first on <a rel="nofollow" href="https://www.maxester.com/blog">MaXEster Technologies  | Technical Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[
<p>Hooks in react js are functions, which let you to use state inside the functional components. Hooks are used to associate the state to the functional component.</p>



<p>Before the hooks were introduced, if we wanted to use the states in our components, then we had to convert it to the class based component. As we all know functional components are a lot easier to understand and write. Therefore, hooks came to take there game one level up.</p>



<p>There are many hooks available in react js like useState(), useEffect(), useMemo(), useReducer(), useContext() etc. We can also create are own hooks.</p>



<p>There are some conditions which we have to follow, if we want to use hooks in our components.</p>



<ol><li>Hooks must be used inside the react components, not in any regular javascript function.</li><li>Hooks must not be used inside the local blocks, loops.</li><li>Hooks must appear at the top of the component functions.</li></ol>



<h3>Hooks in React JS</h3>



<h4>1. useState() :</h4>



<p>useState() hook is used to track the state of the component. A state contains the data of a component at any particular instance of a time.</p>



<p>To use the useState() hook, First we have to import it.</p>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2023/12/import.png" alt="" class="wp-image-1171" srcset="https://www.maxester.com/blog/wp-content/uploads/2023/12/import.png 322w, https://www.maxester.com/blog/wp-content/uploads/2023/12/import-300x28.png 300w" sizes="(max-width: 322px) 100vw, 322px" /></figure>



<p>The useState() hook takes initital state as a parameter and returns two values, i.e. current state and the function to update the state. We can&#8217;t update the state directly.</p>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2023/12/useState.png" alt="" class="wp-image-1172" srcset="https://www.maxester.com/blog/wp-content/uploads/2023/12/useState.png 400w, https://www.maxester.com/blog/wp-content/uploads/2023/12/useState-300x31.png 300w" sizes="(max-width: 400px) 100vw, 400px" /></figure>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2023/12/program.png" alt="" class="wp-image-1174" srcset="https://www.maxester.com/blog/wp-content/uploads/2023/12/program.png 857w, https://www.maxester.com/blog/wp-content/uploads/2023/12/program-300x216.png 300w, https://www.maxester.com/blog/wp-content/uploads/2023/12/program-768x553.png 768w" sizes="(max-width: 857px) 100vw, 857px" /></figure>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2023/12/web1-1.png" alt="" class="wp-image-1175" srcset="https://www.maxester.com/blog/wp-content/uploads/2023/12/web1-1.png 538w, https://www.maxester.com/blog/wp-content/uploads/2023/12/web1-1-300x91.png 300w" sizes="(max-width: 538px) 100vw, 538px" /></figure>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2023/12/web2-1.png" alt="" class="wp-image-1176" srcset="https://www.maxester.com/blog/wp-content/uploads/2023/12/web2-1.png 546w, https://www.maxester.com/blog/wp-content/uploads/2023/12/web2-1-300x88.png 300w" sizes="(max-width: 546px) 100vw, 546px" /></figure>



<h4>2. useEffect() :</h4>



<p>useEffect() is a hook used in react js to handle the side effects like fetching the data or updating the DOM, if a particular event occurs.</p>



<p>In order to use useEffect() hook, we first have to import it.</p>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2023/12/useEffect.png" alt="" class="wp-image-1177" srcset="https://www.maxester.com/blog/wp-content/uploads/2023/12/useEffect.png 330w, https://www.maxester.com/blog/wp-content/uploads/2023/12/useEffect-300x24.png 300w" sizes="(max-width: 330px) 100vw, 330px" /></figure>



<p>useEffect() hook takes two values, one is the function to be executed and another is the dependency array. The dependency array contains the value upon whose updation the useEffect() hook will re render. If you want that the useEffect() hook should render only once then you can pass a empty array.</p>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2023/12/program2.png" alt="" class="wp-image-1178" srcset="https://www.maxester.com/blog/wp-content/uploads/2023/12/program2.png 814w, https://www.maxester.com/blog/wp-content/uploads/2023/12/program2-300x182.png 300w, https://www.maxester.com/blog/wp-content/uploads/2023/12/program2-768x467.png 768w" sizes="(max-width: 814px) 100vw, 814px" /><figcaption>The useEffect() will execute on every button click</figcaption></figure>



<h3>3. useContext() :</h3>



<p>useContext() hook is used to manage the state of the whole application. It is used to pass the data in all the components without using the prop drilling. Whenever the components are nested deeply inside the component tree, at that time passing states using props become cumbersome. In that case the useContext() hook comes to rescue us.</p>



<p>We can create a context using the createContext() method.</p>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2023/12/context.png" alt="" class="wp-image-1179" srcset="https://www.maxester.com/blog/wp-content/uploads/2023/12/context.png 830w, https://www.maxester.com/blog/wp-content/uploads/2023/12/context-300x44.png 300w, https://www.maxester.com/blog/wp-content/uploads/2023/12/context-768x112.png 768w" sizes="(max-width: 830px) 100vw, 830px" /></figure>



<p>To use context inside the components, we have to wrap the components inside the method provided by the react, which is &lt;context_name.Provider&gt;&lt;/context_name.Provider&gt;.</p>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2023/12/index.js_.png" alt="" class="wp-image-1182" srcset="https://www.maxester.com/blog/wp-content/uploads/2023/12/index.js_.png 612w, https://www.maxester.com/blog/wp-content/uploads/2023/12/index.js_-300x95.png 300w" sizes="(max-width: 612px) 100vw, 612px" /></figure>



<p>Now for using the context values, we have to use the useContext() hook, which takes the context object which is returned by the createContext() method and it returns back the current value of that context.</p>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2023/12/useContext.png" alt="" class="wp-image-1180" srcset="https://www.maxester.com/blog/wp-content/uploads/2023/12/useContext.png 736w, https://www.maxester.com/blog/wp-content/uploads/2023/12/useContext-300x228.png 300w" sizes="(max-width: 736px) 100vw, 736px" /></figure>



<h4>4. useReducer() :</h4>



<p>useReducer() hook in react js is used to manage the state. Its purpose is similar to the useState() hook but it is preferred in complex applications where the state logic is complex and has different types of functions to perform the updation.</p>



<p>Syntax:</p>



<p>const [currentState, dispatchAction] = useReducer(reducerFunction, initialState,  init)</p>



<p>useReducer() takes 3 arguments reducerFunction which will be used to change the state, initial state of the application and a init function which is used to load the initial state lazily.</p>



<p></p>



<h3>5. useRef() :</h3>



<p>useRef() hook is used to persist a particular value between multiple renderings, For example , if we want to count how many times a particular component got rendered.</p>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2023/12/useRef.png" alt="" class="wp-image-1185" srcset="https://www.maxester.com/blog/wp-content/uploads/2023/12/useRef.png 756w, https://www.maxester.com/blog/wp-content/uploads/2023/12/useRef-300x185.png 300w" sizes="(max-width: 756px) 100vw, 756px" /></figure>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2023/12/render1.png" alt="" class="wp-image-1186"/></figure>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2023/12/render2.png" alt="" class="wp-image-1187"/></figure>
<p>The post <a rel="nofollow" href="https://www.maxester.com/blog/2023/12/16/what-is-hooks-in-react-js/">What is Hooks in React JS?</a> appeared first on <a rel="nofollow" href="https://www.maxester.com/blog">MaXEster Technologies  | Technical Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.maxester.com/blog/2023/12/16/what-is-hooks-in-react-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
