Understanding State in React: The Backbone of Dynamic UIs

When you build applications with React, state is one of the most essential concepts to master. It’s what gives React components their dynamic and interactive behavior. Without state, your React app is just static HTML with JavaScript syntax.

If we have to elaborate more then state lets your app remember things like user input, button clicks, or whether something is visible or hidden — and when the state changes, the UI updates automatically.

In a nutshell, state is a way to store and manage changing information in a component.

State is a JavaScript object that stores dynamic data in a component.

How to use state in functional component

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <h1>{count}</h1>
      <button onClick={() => setCount(count + 1)}>Increase</button>
    </div>
  );
}

Below is the breakdown of above code:

  • useState is the hook that lets you add state to the component.

  • count holds the initial / current value.

  • setCount is the setter function which is used to set or update the value.

For more clarity you can refer this structure

const [stateValue, setStateValue] = useState(initialValue);

👍 Updating state

Correct way ✅

setCount((prevCount) => prevCount + 1)

This ensures you’re using the most recent value of the state. Also remember, when your new state depends on the previous state, you have to update the value in above way.

Wrong way ❌

setCount(count + 1)

This works but can lead to bugs 🐞 in cases where state updates depend on previous state values.

🖥️ Best practices for managing state

  • Keep state minimal: Only store what’s necessary.

  • Use local state first: Don’t jump to global state too early.

  • Don’t mutate state directly: Always use setState or useState updater.

  • Separate concerns: Keep UI logic and state logic manageable and modular.

😶‍🌫️ Conclusion

State is what gives life to your React components. Whether you're toggling a button, fetching an API, or building a dynamic dashboard — state is at the core of it.

Mastering state management means:

  • More predictable UIs

  • Better performance

  • Easier scalability

So the next time someone says "React is just JavaScript", you’ll know — the real power lies in managing state effectively.

0
Subscribe to my newsletter

Read articles from Raj Kiran Chaudhary directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Raj Kiran  Chaudhary
Raj Kiran Chaudhary

Frontend developer who weaves creativity and code, book lover, avid traveller and a tech enthusiast