Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
How to create a Functional Component in React?
Next article icon

State in React Functional Component

Last Updated : 28 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

State in React functional components refers to a way of managing data that influences the rendering and behavior of components. Unlike class components, where state is managed using a this.state object, functional components use the React Hook useState to manage state efficiently.

  • State is Local to the Component: Each component has its own state, which is independent of other components.
  • Reactivity: When the state changes, React re-renders the component to reflect the updated state in the UI.
  • State is Immutable: You should never directly modify the state. Instead, you use functions to update it.
  • State is Managed with Hooks: In functional components, the useState hook is used to manage state.
JavaScript
import React, { useState } from 'react'; function App() {     const [count, setCount] = useState(1);     return (         <div>             <h1>Count: {count}</h1>             <button onClick={() =>              	setCount(count + 1)}>Increment</button>         </div>     ); } export default App; 

Output

counter-state-in-react-functional-component
State in React Functional Component

In this example

  • count contains the value of the current state.
  • setCount can be defined as the function which is used to update the state.
  • useState(1) initializes the value of the count state with 1. It can be any default value.
  • setCount(count + 1) is used to update the state, and React will automatically re-render the component to show the new count.

Understanding useState Hook

The useState Hook is used to add state to functional components. To use it first import it in the application. It returns two values:

  • State Variable: Holds the current state value.
  • State Updater Function: Updates the state and triggers a re-render.

Syntax

const [count, setCount] = useState(initialValue);
  • count: In the count, the current state is stored.
  • setCount: setCount is used to update the state.
  • initialValue: initialValue is the starting value or the default value of the state.

Why Use State in Functional Components?

Using state in functional components allows us to manage data and perform the other actions which are mentioned below:

  • Dynamic Data: We can save and change values which get updated when the user performs some actions like number in the counter or the user types in the field.
  • Automatic Updates: React automatically updates the user interface when there is change in the state of the component.
  • Simpler Code: In the functional component it is easier to write the code, without the help of the lifecycle methods which is used in the class components.

Common Use Cases of State in Functional Components

  • Form Handling: Managing form input values dynamically.
const [name, setName] = useState(""); const handleInputChange = (e) => setName(e.target.value);
  • Conditional Rendering: Display different UI elements based on state.
const [isLoggedIn, setIsLoggedIn] = useState(false);
  • API Data Management: Store and display fetched data.
const [data, setData] = useState([]);

Next Article
How to create a Functional Component in React?

S

souravsharma098
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • ReactJS-Component
  • ReactJS-State
  • MERN-QnA

Similar Reads

  • ReactJS Functional Components
    In ReactJS, functional components are a core part of building user interfaces. They are simple, lightweight, and powerful tools for rendering UI and handling logic. Functional components can accept props as input and return JSX that describes what the component should render. What are Reactjs Functi
    5 min read
  • Implementing State in React Components
    In React State is an object that holds some information which can be changed overtime. Whenever a State is updated it triggers re-rendering of the component. In React components State can be implemented by default in class components and in functional components we have to implement state using hook
    3 min read
  • What is a pure functional component in ReactJS ?
    What is a JavaScript function? A JavaScript function is a block of code designed to perform a particular task which is executed when it is called. How React functional components are similar to JavaScript functions? Conceptually, components are like JavaScript functions.  A functional component is a
    3 min read
  • How to create a Functional Component in React?
    To create a functional component in React, you define a JavaScript function that returns JSX. These components are stateless and rely on props for data input. Functional components are concise, easy to read, and can utilize React Hooks for managing state and side effects, making them a fundamental b
    2 min read
  • How to create Functional Components in React ?
    To create a functional component in React, you define a JavaScript function that returns JSX. These components are stateless and rely on props for data input. Functional components are concise, easy to read, and can utilize React Hooks for managing state and side effects, making them a fundamental b
    2 min read
  • How To Get Previous State In ReactJS Functional Component?
    To get the previous state in ReactJS Functional Component we can use the previous value of the state while using the set state method. State can only be directly accessed in the class component so we will be using the react useState hook to accomplish the task. PrerequisitesReact JSReact JS function
    2 min read
  • React Suite Portal Component
    React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. Portal component renders its children into a new subtree outside the current DOM hierarchy. We can use the following approach in ReactJS to use the React Suite P
    2 min read
  • React Native Smart Components
    In this article, we are going to learn about Smart components in React Native. The smart component is one of the categories of React Components so before diving into the smart components detail. A Component is one of the core building blocks of React and React-Native. The component is a block of cod
    3 min read
  • Role of Hooks in Functional Components.
    Hooks in React are like new tools that make building components easier. Before, we had to use classes, which were like following strict instructions. But with hooks, we can do the same thing more simply and flexibly directly inside functional components. It's like having a magic connector for buildi
    3 min read
  • How to use PropTypes in a Functional Component in React?
    PropTypes is a mechanism provided by React for type-checking props passed to components. It allows developers to specify the expected types of props (such as strings, numbers, arrays, objects, etc.) and whether they are required or optional. PropTypes helps catch bugs early by providing warnings in
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences