<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: rashmiw333</title>
    <description>The latest articles on DEV Community by rashmiw333 (@rashmiw333).</description>
    <link>https://dev.to/rashmiw333</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4132622%2Ff7083c31-84e8-4099-9643-5e81afd365b1.png</url>
      <title>DEV Community: rashmiw333</title>
      <link>https://dev.to/rashmiw333</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rashmiw333"/>
    <language>en</language>
    <item>
      <title>Understanding Custom Hooks in React with a Simple Counter Example</title>
      <dc:creator>rashmiw333</dc:creator>
      <pubDate>Sat, 19 Sep 2026 10:00:21 +0000</pubDate>
      <link>https://dev.to/rashmiw333/understanding-custom-hooks-in-react-with-a-simple-counter-example-3fcf</link>
      <guid>https://dev.to/rashmiw333/understanding-custom-hooks-in-react-with-a-simple-counter-example-3fcf</guid>
      <description>&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;To understand the concept better, I created a small counter application&lt;br&gt;
in CodeSandbox using two custom hooks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useCounter&lt;/li&gt;
&lt;li&gt;useLogger&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This simple example helped me understand how custom hooks can separate&lt;br&gt;
logic from the UI and make React components easier to manage.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is a Custom Hook?
&lt;/h2&gt;

&lt;p&gt;A custom hook is a JavaScript function that allows us to reuse logic&lt;br&gt;
that uses React hooks.&lt;/p&gt;

&lt;p&gt;Custom hooks usually start with the word &lt;code&gt;use&lt;/code&gt;, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useCounter&lt;/li&gt;
&lt;li&gt;useLogger&lt;/li&gt;
&lt;li&gt;useFetch&lt;/li&gt;
&lt;li&gt;useLocalStorage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of putting all the logic inside a component, we can move&lt;br&gt;
reusable logic into a custom hook.&lt;/p&gt;
&lt;h2&gt;
  
  
  1.Creating the useCounter Hook:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxexrx3s84dueymqu7h84.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxexrx3s84dueymqu7h84.png" alt=" " width="800" height="634"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useCounter&lt;/code&gt; hook manages the counter state.&lt;/p&gt;

&lt;p&gt;It provides three functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;incrementCounter()&lt;/code&gt; increases the counter.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;decrementCounter()&lt;/code&gt; decreases the counter.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;reset()&lt;/code&gt; sets the counter back to zero.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The hook returns the counter value and these functions so that a&lt;br&gt;
component can use them.&lt;/p&gt;
&lt;h2&gt;
  
  
  2.Creating the useLogger Hook:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F32p7c5gomg1d9tofxtx1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F32p7c5gomg1d9tofxtx1.png" alt=" " width="798" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Whenever the component renders, it logs the current counter value&lt;br&gt;
to the browser console.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
current value of counter 0&lt;br&gt;
current value of counter 1&lt;br&gt;
current value of counter 2&lt;/p&gt;
&lt;h2&gt;
  
  
  3.Using the Hooks in App.jsx:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fug16rh4a0v4s1e7yweev.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fug16rh4a0v4s1e7yweev.png" alt=" " width="800" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Inside &lt;code&gt;App&lt;/code&gt;, called &lt;code&gt;useCounter()&lt;/code&gt; and get back the counter value&lt;br&gt;
and the three functions.&lt;br&gt;
then passed the counter value to &lt;code&gt;useLogger()&lt;/code&gt;:&lt;br&gt;
useLogger(counter);&lt;/p&gt;
&lt;h2&gt;
  
  
  4.What happens when I click Increment?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fz6omn9udhc5kycscfsf8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fz6omn9udhc5kycscfsf8.png" alt=" " width="800" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flj7kgdr5fczhnjwsrl3x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flj7kgdr5fczhnjwsrl3x.png" alt=" " width="612" height="150"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Initial state:
counter = 0
       ↓
Click Increment
       ↓
incrementCounter()
       ↓
setCounter(counter + 1)
       ↓
counter = 1
       ↓
useLogger(1)
       ↓
Console → current value of counter 1

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I click the Increment button,&lt;code&gt;incrementCounter()&lt;/code&gt; updates the&lt;br&gt;
state.&lt;br&gt;
React re-renders the component with the new counter value.&lt;br&gt;
The logger then receives the updated value.&lt;/p&gt;

&lt;h2&gt;
  
  
  5.Why use a Custom Hook?
&lt;/h2&gt;

&lt;p&gt;This small example helped to understand why custom hooks are useful.&lt;/p&gt;

&lt;p&gt;Instead of keeping the counter state and functions directly inside&lt;br&gt;
the component, moved the logic into &lt;code&gt;useCounter&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, instead of writing API-fetching logic in multiple&lt;br&gt;
components, we can create a reusable &lt;code&gt;useFetch&lt;/code&gt; hook.&lt;/p&gt;

&lt;h2&gt;
  
  
  6.Learnings:
&lt;/h2&gt;

&lt;p&gt;While building this small example,learned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How custom hooks can extract reusable logic from a component.&lt;/li&gt;
&lt;li&gt;How &lt;code&gt;useState&lt;/code&gt; can be used inside a custom hook.&lt;/li&gt;
&lt;li&gt;How a custom hook can return both state and functions.&lt;/li&gt;
&lt;li&gt;How one custom hook can be used alongside another hook.&lt;/li&gt;
&lt;li&gt;How separating logic from UI can make components easier to understand.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This was a small example, but it helped me understand the concept&lt;br&gt;
before using custom hooks in larger React applications.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
