📖 Scenario: You are building a simple React app that shows a counter. You want to reuse the counting logic in multiple places without repeating code.
🎯 Goal: Create a custom hook called useCounter that manages a counter value and a function to increase it. Then use this hook inside a functional component to display the count and a button to increase it.
📋 What You'll Learn
Create a custom hook named useCounter that returns a count state and an increment function.
Initialize the count state to 0 inside the hook.
Create a functional component named CounterDisplay that uses the useCounter hook.
Render the current count and a button labeled Increase inside CounterDisplay.
Clicking the button should increase the count by 1.
💡 Why This Matters
🌍 Real World
Custom hooks help you reuse logic like counters, form handling, or data fetching across many components without repeating code.
💼 Career
Understanding custom hooks is essential for React developers to write clean, maintainable, and reusable code in real projects.
Progress0 / 4 steps
1
Create the custom hook useCounter with initial count
Create a custom hook named useCounter that uses useState to create a state variable called count initialized to 0.
React
Hint
Use useState(0) inside useCounter to create count and setCount.
2
Add an increment function inside useCounter
Inside the useCounter hook, create a function called increment that increases count by 1 using setCount. Return increment along with count from the hook.
React
Hint
Define increment as a function that calls setCount(count + 1). Return it from the hook.
3
Create CounterDisplay component using useCounter
Create a functional component named CounterDisplay. Inside it, call the useCounter hook to get count and increment. Render a <div> with a <p> showing the text Count: {count} and a <button> with the label Increase that calls increment when clicked.
React
Hint
Use object destructuring to get count and increment from useCounter(). Render them inside JSX.
4
Export CounterDisplay as default export
Add a default export statement for the CounterDisplay component at the end of the file.
React
Hint
Use export default CounterDisplay; to export the component.
Practice
(1/5)
1. What is the main purpose of creating a custom hook in React?
easy
A. To create new HTML elements
B. To reuse stateful logic across multiple components
C. To style components with CSS
D. To replace React components entirely
Solution
Step 1: Understand what hooks do
Hooks let you use React features like state and effects inside functions.
Step 2: Identify the role of custom hooks
Custom hooks let you reuse this logic in many components without repeating code.
Final Answer:
To reuse stateful logic across multiple components -> Option B
Quick Check:
Custom hooks = reuse logic [OK]
Hint: Custom hooks start with 'use' and share logic [OK]
Common Mistakes:
Thinking hooks create UI elements
Confusing hooks with styling tools
Believing hooks replace components
2. Which of the following is the correct way to define a custom hook in React?
easy
A. function useCounter() { const [count, setCount] = useState(0); return [count, setCount]; }
B. function counterHook() { const [count, setCount] = useState(0); return [count, setCount]; }
C. function UseCounter() { const [count, setCount] = useState(0); return [count, setCount]; }
D. function counter() { const [count, setCount] = useState(0); return [count, setCount]; }
Solution
Step 1: Recall naming rules for custom hooks
Custom hooks must start with 'use' to follow React conventions and enable linting.
Step 2: Check function names
Only function useCounter() { const [count, setCount] = useState(0); return [count, setCount]; } starts with 'use' and uses camelCase correctly.
Final Answer:
function useCounter() { const [count, setCount] = useState(0); return [count, setCount]; } -> Option A
Quick Check:
Custom hook names start with 'use' [OK]
Hint: Custom hooks always start with 'use' prefix [OK]
Common Mistakes:
Not starting function name with 'use'
Using uppercase first letter in hook name
Naming hooks like regular functions
3. Given this custom hook code, what will be logged when ExampleComponent renders?
A. Directly modifying count variable instead of using setCount
B. Missing React import for useState
C. increment function should return count
D. Custom hook name should not start with 'use'
Solution
Step 1: Check how state is updated
State variables like count must be updated via setCount, not by direct assignment.
Step 2: Identify the error in increment
increment tries to assign count = count + 1, which does not update React state.
Final Answer:
Directly modifying count variable instead of using setCount -> Option A
Quick Check:
State updates need setCount [OK]
Hint: Always use setState to update state, never assign directly [OK]
Common Mistakes:
Trying to assign state variable directly
Forgetting to call setCount
Confusing variable assignment with state update
5. You want to create a custom hook useFetch that fetches data from a URL and returns loading state, data, and error. Which approach correctly reuses logic and handles updates safely?