📖 Scenario: You are building a simple React counter component that updates its displayed number when a button is clicked.This simulates a real-world situation where a user interacts with a webpage and the content changes dynamically.
🎯 Goal: Create a React functional component that shows a number starting at 0 and a button labeled 'Increase'. When the button is clicked, the number increases by 1 and the component updates to show the new number.
📋 What You'll Learn
Use React functional components
Use the useState hook to manage the counter state
Add a button that updates the state when clicked
Display the current counter value in a <div>
💡 Why This Matters
🌍 Real World
Counters are common in apps for likes, votes, quantities, or steps. Understanding how to update components on user actions is essential.
💼 Career
React developers frequently use state and event handlers to create interactive UI components that update dynamically.
Progress0 / 4 steps
1
Set up the initial counter state
Create a React functional component called Counter. Inside it, use the useState hook to create a state variable called count initialized to 0.
React
Hint
Use const [count, setCount] = useState(0); inside the Counter function.
2
Add a button to increase the counter
Inside the Counter component, add a <button> element with the text 'Increase'. Add an onClick handler that calls setCount(count + 1) to increase the count by 1.
React
Hint
Return a button element with an onClick that calls setCount(count + 1).
3
Display the current count
Modify the Counter component to display the current count inside a <div> above the button.
React
Hint
Use a <div> to show the count value above the button.
4
Complete the component with export
Add export default Counter; at the end of the file to make the Counter 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 happens during the updating phase in a React component?
easy
A. The component is removed from the screen
B. The component is created for the first time
C. The component's styles are applied
D. The component re-renders when state or props change
A. Directly modifying state variable instead of using setter
B. Missing dependency array in useEffect
C. Using setCount with a number instead of an object
D. Not returning JSX from the component
Solution
Step 1: Identify state mutation
The code directly changes count variable, which is not allowed in React state management.
Step 2: Correct way to update state
Use setCount(count + 1) without modifying count directly.
Final Answer:
Directly modifying state variable instead of using setter -> Option A
Quick Check:
Never mutate state variable directly; use setter [OK]
Hint: Never assign state variable directly; always use setter [OK]
Common Mistakes:
Trying to change state variable directly
Confusing state variable with setter function
Ignoring React's immutable state rule
5. You want to update a React component's state only when a prop value changes, avoiding unnecessary re-renders. Which approach correctly uses the updating phase to achieve this?
function MyComponent({ value }) {
const [state, setState] = useState(value);
// What should go here?
}
hard
A. useEffect(() => { setState(value); }, [value]);
B. useEffect(() => { setState(value); }, []);
C. setState(value); // directly in component body
D. useEffect(() => { setState(value); });
Solution
Step 1: Use useEffect to react to prop changes
To update state only when value prop changes, use useEffect with [value] dependency.
Step 2: Avoid unnecessary updates
Dependency array ensures effect runs only when value changes, preventing infinite loops or extra renders.
Final Answer:
useEffect(() => { setState(value); }, [value]); -> Option A
Quick Check:
useEffect with [value] updates state on prop change only [OK]
Hint: Use useEffect with dependency array for prop-based updates [OK]
Common Mistakes:
Leaving dependency array empty causing no updates
Calling setState directly in component body causing infinite loops
Omitting dependency array causing effect on every render
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.