State in React Using useState Hook

 Introduction

In React, state is used to store data that can change overtime.
When this data changes, React automatically updates the UI.

In older React versions, state was mostly used in class components.
But in modern React, we use Hooks, and the most important hook for state is called "useState".

What is State in React?

State is:
  •     Data that belongs to a component
  •     Data that can change
  •     Data that controls what is shown on the screen
Example of state:
  1. Counter value
  2. Input text
  3. Login status
  4. Theme (dark / Light)
When state changes, React re-renders the component automatically.

What is the useState Hook ?

useState is a React Hook that allows you to:

  • Create state variables
  • Update state values
  • Re-render the UI when state changes

Basic Syntax




stateValue - current value of the state
setStateValue - function used to update the state
initialValue - starting value of the state

Importing useState

Before using useState, you must import it from React

Simple Counter Example 



Explanation step by step

1. Creating State




count - current value of the counter
setCount - function to update the counter
0 - initial value

So at the starrt 
  count is zero.

2. Displaying State in JSX



3. Updating State


When the button is clicked
  • setCount updates the value
  • React re-renders the component
  • The new value appears on the screen
Important 
Never update state like this
 

Always use the setter function 

Why State is Important?

State allows React apps to:
  • Be interactive
  • Respond to user actions
  • Update data without refreshing the page
Without State:
  • React apps would be static
  • Buttons and forms wouldn't work properly

Multiple State Variables

You can use useState more than once in a component


Each state variable works independently.

State Update Re renders UI

One of the most powerful features of  React

When state changes, React automatically updates the UI.

You don't need to 
  • Reload the page
  • Manually update HTML

React handles everything for you.

Comments

Popular posts from this blog

Styling React Applications in 2026

React Best Practices in 2026

Fetching Data in React Using APIs