How to use State in Functional Components (useState Hook) | What is useState Hook in React & How to Manage State in Functional Components?

Image 196 views

ReactJS introduced Hooks to give functional components the ability to manage state and side effects β€” which was previously only possible in class components. The most commonly used hook is useState, which lets you create and update state variables directly inside your functional components. In simple words, state allows your React component to “remember” values between renders β€” such as form inputs, button clicks, counters, etc.

In this article, we’ll explore how to use the useState hook to build a simple counter app. You will learn how to define a state variable, update its value using the setter function, and reflect those changes in the UI. This is one of the most essential building blocks for any modern React application.

Let’s dive into the code and see how it works step-by-step.

πŸ‘‰ Download Free Ebook β†’

πŸ‘‰ Get Free Course β†’

Image

βœ… Example: Counter Using useState in React Functional Component

🧩 HTML + JavaScript (ReactJS Functional Component)

  1.     import React, { useState } FROM 'react';
  2.  
  3. FUNCTION CounterApp() {
  4.   // Step 1: DECLARE state USING useState hook
  5.   const [COUNT, setCount] = useState(0);
  6.  
  7.   // Step 2: CREATE handler TO UPDATE state
  8.   const increase = () => {
  9.     setCount(COUNT + 1);
  10.   };
  11.  
  12.   RETURN (
  13.     <div STYLE={{ padding: '20px', fontFamily: 'Arial' }}>
  14.       <h2>ReactJS Counter USING useState</h2>
  15.       <p>CURRENT COUNT: <strong>{COUNT}</strong></p>
  16.       <button onClick={increase} STYLE={{ padding: '10px 20px' }}>
  17.         Increase COUNT
  18.       </button>
  19.     </div>
  20.   );
  21. }
  22.  
  23. export DEFAULT CounterApp;

 

πŸ’‘ Key Concepts:

  • useState(0) initializes the count state with 0
  • setCount updates the state value
  • Re-render occurs whenever state is updated
  • You can have multiple useState() calls for different values

 

FAQ (Frequently Asked Questions)

How do you use the useState hook to manage state in a functional component?

import React, { useState } from react; Within your functional component, call useState with the initial state value as an argument. It returns an array containing two elements: The Current state value: Use this in your JSX to display the data dynamically.

Which state Hooks can be used to maintain state in functional components?

The React useState Hook allows us to track state in a function component. State generally refers to data or properties that need to be tracking in an application.

How do you initialize state in a functional component using Hooks?

You can achieve this by passing an argument called initialState to the useState hook within your custom hook. The second requirement is to expose a reset function handler to the consumer. You can achieve this by returning a reset function from your custom hook that resets the state to the initial state.

 
  
πŸ‘‰ Get Free Course β†’ Image

πŸ“Œ Salesforce Administrators

πŸ“Œ Salesforce Lightning Flow Builder

πŸ“Œ Salesforce Record Trigger Flow Builder

πŸ‘‰ Get Free Course β†’

Image

πŸ“Œ Aura Lightning Framework

πŸ“Œ Lightning Web Component (LWC)

πŸ“Œ Rest APIs Integration



Image

Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net