📖 Scenario: You are building a simple React counter app. The app has a button that increases the count by 1 each time it is clicked. You want to update the count state safely using a callback function to avoid issues when multiple clicks happen quickly.
🎯 Goal: Create a React functional component called Counter that uses useState to hold a count starting at 0. Add a button that, when clicked, updates the count state by adding 1 using a callback function inside setCount.
📋 What You'll Learn
Create a React functional component named Counter
Use useState to create a state variable count initialized to 0
Add a button with the text Increment
Use a callback function inside setCount to update count by adding 1 when the button is clicked
Display the current count value in a <p> tag
💡 Why This Matters
🌍 Real World
Updating state safely with callback functions is important in React apps to avoid bugs when multiple updates happen quickly, such as in counters, forms, or live data.
💼 Career
React developers often use callback functions in state setters to ensure reliable updates, especially in interactive UI components.
Progress0 / 4 steps
1
Set up the initial state
Create a React functional component called Counter. Inside it, use useState to create a state variable named count initialized to 0.
React
Hint
Remember to import useState from React and call it inside your component to create count and setCount.
2
Add the increment button
Inside the Counter component, add a <button> element with the text Increment. This button will later update the count when clicked.
React
Hint
Wrap the button inside a return statement and JSX parentheses.
3
Add the callback function to update state
Add an onClick handler to the Increment button. Use setCount with a callback function that receives the previous count and returns the previous count plus 1.
React
Hint
Use an arrow function inside setCount to safely update the count based on the previous value.
4
Display the current count
Add a <p> tag inside the return that shows the text Count: followed by the current count value. Place it above the Increment button.
React
Hint
Use a React fragment <></> to wrap multiple elements inside return.
Practice
(1/5)
1. Why should you use a callback function when updating state in React with useState?
easy
A. Because it safely uses the previous state value to calculate the new state.
B. Because it makes the component render twice automatically.
C. Because it prevents the component from updating.
D. Because it allows you to update multiple states at once.
Solution
Step 1: Understand state updates in React
State updates can be asynchronous, so relying on the current state value directly can cause bugs.
Step 2: Use callback function to access previous state
The callback function receives the previous state safely and returns the new state, ensuring correct updates.
Final Answer:
Because it safely uses the previous state value to calculate the new state. -> Option A
Quick Check:
Callback uses previous state = safe update [OK]
Hint: Use callback to get previous state safely for updates [OK]
Common Mistakes:
Updating state directly without callback causes stale values
Thinking callback triggers extra renders
Believing callback merges multiple states automatically
2. Which of the following is the correct syntax to update state using a callback function with useState?
easy
A. setCount(count + 1);
B. setCount(prevCount => prevCount + 1);
C. setCount(() => count + 1);
D. setCount(prevCount + 1);
Solution
Step 1: Identify correct callback syntax
The callback function receives previous state as an argument and returns the new state.
Step 2: Check each option
setCount(prevCount => prevCount + 1); uses prevCount => prevCount + 1, which is the correct callback form.
Final Answer:
setCount(prevCount => prevCount + 1); -> Option B
Quick Check:
Callback syntax = setState(prev => new) [OK]
Hint: Callback syntax always uses previous state parameter [OK]
Common Mistakes:
Passing new state directly without function
Using arrow function without parameter
Trying to add number outside callback
3. What will be the output after clicking the button twice in this React component?