State in React Functional Component
Last Updated :
28 Jan, 2025
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
State in React Functional ComponentIn 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([]);
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 Functio
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