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 custom hooks start with the word "use"?
Starting with "use" lets React know the function follows hook rules. This helps React check for correct usage and enables linting tools to warn about mistakes.
Click to reveal answer
intermediate
Name two best practices when creating custom hooks.
1. Keep hooks focused on a single responsibility to make them easy to understand and reuse. 2. Use built-in hooks inside custom hooks to manage state or side effects cleanly.
Click to reveal answer
intermediate
How can you make a custom hook more reusable?
Accept parameters to customize behavior and return only the necessary data or functions. Avoid tying it to specific UI details so it can work in different components.
Click to reveal answer
intermediate
What should you avoid inside custom hooks?
Avoid calling hooks conditionally or inside loops. Also, avoid side effects that are not related to the hook’s purpose to keep behavior predictable.
Click to reveal answer
What prefix must a custom hook's name start with in React?
Afetch
Bget
Cuse
Dhandle
✗ Incorrect
Custom hooks must start with "use" so React can identify them as hooks and apply rules correctly.
Which of these is a good practice when writing custom hooks?
AKeeping the hook focused on one task
BReturning JSX from the hook
CDirectly manipulating the DOM inside the hook
DCalling hooks conditionally inside the custom hook
✗ Incorrect
Custom hooks should focus on one task to be clear and reusable. Calling hooks conditionally or manipulating DOM directly breaks rules.
How can you make a custom hook reusable across different components?
AHardcode component-specific logic inside the hook
BUse global variables inside the hook
CReturn only UI elements from the hook
DAccept parameters to customize behavior
✗ Incorrect
Accepting parameters lets the hook adapt to different needs, making it reusable.
What should a custom hook return?
AOnly JSX elements
BState values and functions to update them
CNothing, it just runs code
DDOM nodes
✗ Incorrect
Custom hooks usually return state and functions so components can use and control the logic.
Why is it important to follow hook rules inside custom hooks?
ATo avoid React warnings and bugs
BTo make the code longer
CTo prevent using useState
DTo avoid using parameters
✗ Incorrect
Following hook rules prevents bugs and lets React optimize rendering and state management.
Explain what a custom hook is and why you would create one in React.
Think about how to share logic without repeating code.
You got /4 concepts.
List and describe three best practices to follow when writing custom hooks.
Consider naming, clarity, reusability, and React rules.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is a best practice when naming a custom React hook?
easy
A. Start the hook name with use to follow React conventions
B. Name the hook with a capital letter but no prefix
C. Use any name as long as it describes the functionality
D. Prefix the hook name with hook instead of use
Solution
Step 1: Understand React hook naming conventions
React requires custom hooks to start with use so React can identify them as hooks.
Step 2: Evaluate the options
Only Start the hook name with use to follow React conventions correctly follows this convention by starting the name with use.
Final Answer:
Start the hook name with use to follow React conventions -> Option A
Quick Check:
Hook names start with 'use' = B [OK]
Hint: Always start custom hook names with 'use' [OK]
Common Mistakes:
Naming hooks without 'use' prefix
Using 'hook' prefix instead of 'use'
Starting hook names with lowercase letters without 'use'
2. Which of the following is the correct syntax to define a custom hook that returns a count state and an increment function?
easy
A. function useCount() { const [count, setCount] = useState(0); return { count increment: () => setCount(count + 1) }; }
B. function countHook() { const [count, setCount] = useState(0); return [count, () => setCount(count + 1)]; }
C. function useCount() { const [count, setCount] = useState(0); return [count, () => setCount(count + 1)]; }
D. function useCount() { const count = 0; function increment() { count += 1; } return { count, increment }; }
Solution
Step 1: Check the hook naming and return type
The hook must start with use and return state and updater properly. function useCount() { const [count, setCount] = useState(0); return [count, () => setCount(count + 1)]; } uses useCount and returns an array with count and increment function.
Step 2: Verify state management correctness
function useCount() { const [count, setCount] = useState(0); return [count, () => setCount(count + 1)]; } uses useState correctly and returns a function that updates state with setCount(count + 1). This is the correct pattern.
Final Answer:
function useCount() { const [count, setCount] = useState(0); return [count, () => setCount(count + 1)]; } -> Option C
Quick Check:
Custom hook syntax with useState and return array = C [OK]
Hint: Custom hooks return state and updater, named starting with 'use' [OK]
What will the component render after calling toggle() once?
medium
A.
ON
B.
false
C.
OFF
D. Error because toggle is called outside an event
Solution
Step 1: Analyze where toggle is called
toggle() is called directly in the component body, which executes during every render phase.
Step 2: Understand React render rules
State updates during render trigger re-renders. Since toggle() is unconditional, it flips state every render, causing an infinite loop. React prohibits this and produces errors/warnings (e.g., "Cannot update a component while rendering a different component" or too many re-renders).
Final Answer:
Error because toggle is called outside an event -> Option D
Quick Check:
State updaters only in events/effects, not render body [OK]
Hint: Never call state updaters during render; use events or useEffect [OK]
Common Mistakes:
Assuming async update shows initial state without considering re-render loop
Expecting 'ON' due to toggle flip
Confusing boolean with string output
Thinking render body calls are safe without infinite loop
A. It does not use React state, so count won't trigger re-renders
B. The hook name should not start with 'use'
C. The increment function should not modify count
D. It should return an array, not an object
Solution
Step 1: Check state management in the hook
The hook uses a local variable count that resets to 0 on every call. It does not use useState, so React won't track changes or re-render.
Step 2: Understand React state importance
Without useState, updating count does not cause component updates, making the hook ineffective.
Final Answer:
It does not use React state, so count won't trigger re-renders -> Option A
Quick Check:
Local variable won't update UI without useState [OK]
Hint: Always use useState for reactive state in hooks [OK]
Common Mistakes:
Using local variables instead of useState
Expecting UI to update without state
Returning object instead of array (not always wrong)
Misnaming hook without 'use' prefix
5. You want to create a custom hook useFetch that fetches data from a URL and returns loading state, data, and error. Which approach follows best practices?
hard
A. Fetch data directly in the hook body without useEffect, return data and error only
B. Use useEffect inside the hook to fetch data on mount, use useState for loading, data, and error, and return them as an object
C. Use useMemo to fetch data and return loading and data states
D. Fetch data outside the hook and pass it as a parameter to the hook
Solution
Step 1: Understand data fetching in hooks
Fetching data should happen inside useEffect to avoid running on every render. State variables track loading, data, and error.
Step 2: Evaluate options for best practice
Use useEffect inside the hook to fetch data on mount, use useState for loading, data, and error, and return them as an object correctly uses useEffect and useState to manage asynchronous fetch and returns all relevant states as an object for clarity.
Final Answer:
Use useEffect inside the hook to fetch data on mount, use useState for loading, data, and error, and return them as an object -> Option B
Quick Check:
Fetch in useEffect with state management = A [OK]
Hint: Fetch inside useEffect, manage state with useState [OK]
Common Mistakes:
Fetching data directly in hook body causing infinite loops
Not managing loading or error states
Using useMemo for side effects
Passing fetched data as parameter instead of fetching inside hook
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.