What if your app could remember and update itself without you lifting a finger?
What is state in React - Why It Matters
Imagine you have a webpage with a button that counts how many times you click it. Every time you click, you have to manually find the number on the page and update it yourself.
Manually updating the page is slow and easy to forget. If you miss updating the number, the page shows wrong info. It's hard to keep track of changes and update the right parts every time.
State in React keeps track of changing data automatically. When state changes, React updates the page for you, so you don't have to do it by hand.
button.onclick = () => { let count = parseInt(span.textContent); count++; span.textContent = count; }const [count, setCount] = React.useState(0); <button onClick={() => setCount(count + 1)}>{count}</button>
State lets your app remember and show changes instantly without extra work.
Think of a shopping cart that updates the number of items as you add or remove products, always showing the correct count.
State stores data that can change over time.
React updates the screen automatically when state changes.
This makes interactive apps easier and less error-prone.
Practice
state in a React component?Solution
Step 1: Understand what state represents
State holds information that can change over time and affects what the component shows.Step 2: Compare options with the definition
Only To store data that can change and affect the component's display correctly describes state as data that changes and affects display.Final Answer:
To store data that can change and affect the component's display -> Option DQuick Check:
State = data that changes and updates UI [OK]
- Confusing state with styling or layout
- Thinking state is for static content
- Mixing state with importing libraries
count with initial value 0 in a React functional component?Solution
Step 1: Recall the useState syntax
useState returns an array with current value and a setter function, usually destructured as [value, setValue].Step 2: Check each option's syntax
const [count, setCount] = useState(0); correctly uses array destructuring with useState(0). Options A, B, and C do not follow the correct pattern.Final Answer:
const [count, setCount] = useState(0); -> Option CQuick Check:
useState syntax = [value, setValue] [OK]
- Using var or let without useState
- Not destructuring the array returned by useState
- Assigning useState directly to a single variable
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
Count: {count}
setCount(count + 1)}>Increase
);
}What will be displayed after clicking the button three times?
Solution
Step 1: Understand initial state and button action
Initial count is 0. Each button click calls setCount(count + 1), increasing count by 1.Step 2: Calculate count after 3 clicks
After 3 clicks, count increments 3 times: 0 + 3 = 3.Final Answer:
Count: 3 -> Option AQuick Check:
3 clicks increase count from 0 to 3 [OK]
- Thinking count resets on each click
- Confusing initial value with updated value
- Ignoring that setCount updates state asynchronously
const [count, setCount] = useState(0);
function increment() {
count = count + 1;
}Solution
Step 1: Identify how state should be updated
React state must be updated using the setter function returned by useState, not by direct assignment.Step 2: Analyze the code's mistake
The code tries to assign directly to count, which is a constant and does not update React state.Final Answer:
Directly modifying state variable instead of using setter -> Option BQuick Check:
Use setter function to update state [OK]
- Trying to assign value directly to state variable
- Ignoring that useState returns a setter function
- Confusing const with let for state variables
Solution
Step 1: Understand state immutability in React
State should not be mutated directly; instead, create a new array with the new item added.Step 2: Check each option's update method
Useconst [items, setItems] = useState([]);and update withsetItems([...items, newItem])uses spread syntax to create a new array and calls setter, which is correct. Options A and D mutate the array directly. Useconst items = [];and reassign withitems = items.concat(newItem)tries to reassign a const variable.Final Answer:
Use const [items, setItems] = useState([]); and update with setItems([...items, newItem]) -> Option AQuick Check:
Update state immutably with setter [OK]
- Mutating state array directly with push
- Reassigning const variables
- Not using setter function to update state
Ready to prove it?
Pass the test and this pattern moves unknown
