📖 Scenario: You want to build a simple React component that shows a number on the screen. This number can change when you click a button. To do this, you need to keep track of the number inside the component. This is called state.
🎯 Goal: Create a React functional component that uses state to store a number. Add a button that, when clicked, increases the number by 1. The number should update on the screen each time you click the button.
📋 What You'll Learn
Use React functional component
Use the useState hook to create a state variable called count
Initialize count to 0
Add a button with an onClick event that increases count by 1
Display the current value of count in a <div>
💡 Why This Matters
🌍 Real World
Tracking user interactions like clicks, form inputs, or toggles in a web app.
💼 Career
Understanding state is essential for building interactive React applications used in many web development jobs.
Progress0 / 4 steps
1
Set up the React component and initial state
Create a React functional component called Counter. Inside it, use the useState hook to create a state variable named count and a setter function named setCount. Initialize count to 0.
React
Hint
Remember to import useState from React. Use const [count, setCount] = useState(0); inside the component.
2
Add a button to increase the count
Inside the Counter component, return a <button> element. Add an onClick event to the button that calls setCount(count + 1) to increase the count by 1 when clicked.
React
Hint
Use onClick={() => setCount(count + 1)} inside the button tag.
3
Display the current count value
Modify the returned JSX to include a <div> that shows the current value of count above the button.
React
Hint
Use a <div> with {count} inside the returned JSX.
4
Complete the component export
Add export default Counter; at the end of the file to make the component usable in other parts of the app.
React
Hint
Write export default Counter; at the end of the file.
Practice
(1/5)
1. What is the main purpose of state in a React component?
easy
A. To create static content that never changes
B. To define the component's style and layout
C. To import external libraries into the component
D. To store data that can change and affect the component's display
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 D
Quick Check:
State = data that changes and updates UI [OK]
Hint: State holds changing data that updates the screen [OK]
Common Mistakes:
Confusing state with styling or layout
Thinking state is for static content
Mixing state with importing libraries
2. Which of the following is the correct way to declare a state variable called count with initial value 0 in a React functional component?
easy
A. const count = useState(0);
B. let count = 0;
C. const [count, setCount] = useState(0);
D. var count = useState(0);
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 C
Quick Check:
useState syntax = [value, setValue] [OK]
Hint: useState returns [value, setter] array, destructure it [OK]
Common Mistakes:
Using var or let without useState
Not destructuring the array returned by useState
Assigning useState directly to a single variable
3. Consider this React component code:
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?
medium
A. Count: 3
B. Count: 0
C. Count: 1
D. Count: undefined
Solution
Step 1: Understand initial state and button action
Initial count is 0. Each button click calls setCount(count + 1), increasing count by 1.
B. Directly modifying state variable instead of using setter
C. Using const instead of let for count
D. Setter function name is incorrect
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 B
Quick Check:
Use setter function to update state [OK]
Hint: Never assign state variable directly; use setter function [OK]
Common Mistakes:
Trying to assign value directly to state variable
Ignoring that useState returns a setter function
Confusing const with let for state variables
5. You want to create a React component that shows a list of items and lets the user add new items. Which approach correctly uses state to update the list when a new item is added?
hard
A. Use const [items, setItems] = useState([]); and update with setItems([...items, newItem])
B. Use let items = []; and push new items with items.push(newItem)
C. Use const items = []; and reassign with items = items.concat(newItem)
D. Use const [items, setItems] = useState([]); and update with items.push(newItem)
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
Use const [items, setItems] = useState([]); and update with setItems([...items, newItem]) uses spread syntax to create a new array and calls setter, which is correct. Options A and D mutate the array directly. Use const items = []; and reassign with items = items.concat(newItem) tries to reassign a const variable.
Final Answer:
Use const [items, setItems] = useState([]); and update with setItems([...items, newItem]) -> Option A
Quick Check:
Update state immutably with setter [OK]
Hint: Always create new array and use setter to update state [OK]
Common Mistakes:
Mutating state array directly with push
Reassigning const variables
Not using setter function to update state
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.