React useState vs var: State Management in React

🚀 var vs useState in React – A Common Confusion for Beginners Many new React developers ask this question: “When we can store values in var, why do we need useState?” Let’s break it down 👇 🔹 Using a normal variable (var, let, const) A normal variable can store values and you can change them anytime. Example: let count = 0; const increment = () => {  count = count + 1; } The value changes, React will NOT update the UI. Why? Because React doesn’t know that the value has changed. 🔹 Using UseState useState is designed to manage state inside React components. const [count, setCount] = useState(0); const increment = () => {  setCount(prev => prev + 1); } When setCount runs: 1️⃣ React knows the state changed 2️⃣ The component re-renders 3️⃣ The UI updates automatically Role in React If a value affects the UI, it should be stored in state (useState), not in a normal variable. Learning these small concepts deeply makes you a better developer. 💻 #ReactJS #JavaScript #FrontendDevelopment #WebDevelopment #ReactjsDevelopment #LearnReactjs #LearningInPublic #InterviewQ

  • graphical user interface

To view or add a comment, sign in

Explore content categories