A custom hook is a JavaScript function whose name starts with 'use' and that can call other hooks. It lets you reuse stateful logic between components without changing their structure.
Click to reveal answer
beginner
Why should you use custom hooks instead of duplicating code?
Custom hooks help keep code DRY (Don't Repeat Yourself). They make your code easier to read, maintain, and test by sharing logic in one place instead of copying it in many components.
Click to reveal answer
intermediate
How do you create a custom hook to fetch data from an API?
You create a function starting with 'use', use React hooks like useState and useEffect inside it to manage data and loading state, then return the data and status so components can use it.
Click to reveal answer
intermediate
Can custom hooks use other hooks inside them?
Yes! Custom hooks can call built-in hooks like useState, useEffect, or even other custom hooks. This lets you build complex reusable logic by combining simpler hooks.
Click to reveal answer
beginner
What rules must you follow when writing custom hooks?
You must start the function name with 'use'. Inside, call hooks only at the top level and not inside loops or conditions. This ensures React can track hook calls correctly.
Click to reveal answer
What prefix must a function have to be recognized as a custom hook in React?
Ahandle
Bget
Cuse
Dfetch
✗ Incorrect
React recognizes custom hooks by the 'use' prefix, which signals that the function uses React hooks internally.
Which React hook is commonly used inside custom hooks to manage side effects like data fetching?
AuseEffect
BuseState
CuseContext
DuseRef
✗ Incorrect
useEffect lets you perform side effects such as data fetching inside custom hooks.
Why should hooks be called only at the top level of a custom hook?
ATo avoid infinite loops
BSo React can track hook calls in the same order on every render
CTo keep code clean
DTo improve performance
✗ Incorrect
React relies on hooks being called in the same order each render to manage state correctly.
What is a benefit of using custom hooks?
AThey allow reusing logic without changing component structure
BThey replace components
CThey improve CSS styling
DThey make components class-based
✗ Incorrect
Custom hooks let you share logic between components without changing how components are built.
Can a custom hook return multiple values?
ANo, only one value
BOnly strings
COnly numbers
DYes, usually as an object or array
✗ Incorrect
Custom hooks often return multiple values bundled in an object or array for flexibility.
Explain how you would create and use a custom hook to share logic between two React components.
Think about how to wrap state and effects in a reusable function.
You got /4 concepts.
What are the rules you must follow when writing custom hooks in React and why are they important?
Consider React's hook call order and naming conventions.
You got /4 concepts.
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?