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 start the name of a custom hook with "use"?
React uses the "use" prefix to identify hooks. This helps React enforce rules of hooks and enables linting tools to check correct usage.
Click to reveal answer
intermediate
How do custom hooks help in React development?
Custom hooks let you share logic like fetching data, managing forms, or animations across components. This keeps code clean and avoids repetition.
Click to reveal answer
beginner
Can custom hooks use other hooks inside them?
Yes, custom hooks can call built-in hooks like useState, useEffect, or even other custom hooks to build complex reusable logic.
Click to reveal answer
beginner
What is a simple example of a custom hook?
A simple custom hook could be useCounter that uses useState to keep a count and returns the count and a function to increase it.
Click to reveal answer
What prefix must a custom hook's name start with in React?
Ause
Bget
Cfetch
Dhook
✗ Incorrect
Custom hooks must start with "use" so React can identify them as hooks and apply rules correctly.
Which of these can a custom hook NOT do?
ACall other hooks
BReturn JSX elements
CManage state
DReuse logic
✗ Incorrect
Custom hooks do not return JSX. They return values or functions to be used inside components that return JSX.
Why use custom hooks instead of copying code between components?
ATo keep code DRY (Don't Repeat Yourself)
BTo make components bigger
CTo avoid using state
DTo write more lines of code
✗ Incorrect
Custom hooks help keep code DRY by sharing logic instead of copying it.
Which React hook is commonly used inside custom hooks to manage state?
AuseRef
BuseEffect
CuseState
DuseContext
✗ Incorrect
useState is often used inside custom hooks to manage stateful values.
Can a custom hook call another custom hook?
ANo
BOnly if they don't use state
COnly if they are in the same file
DYes
✗ Incorrect
Custom hooks can call other custom hooks to compose complex logic.
Explain what a custom hook is and why it is useful in React.
Think about how you can share code between components without copying.
You got /5 concepts.
Describe how you would create a simple custom hook to manage a counter.
Start with useState and return what components need.
You got /4 concepts.
Practice
(1/5)
1. What is the main reason to create 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 the purpose of hooks
Hooks let you use React features like state and effects inside functions.
Step 2: Identify why custom hooks are used
Custom hooks let you reuse stateful logic easily across 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 always reuse logic, not UI or styles [OK]
Common Mistakes:
Thinking custom hooks create UI elements
Confusing hooks with styling tools
Believing hooks replace components
2. Which of the following is the correct way to name a custom hook in React?
easy
A. hookUseFetch
B. fetchDataHook
C. FetchData
D. useFetchData
Solution
Step 1: Recall naming rules for custom hooks
Custom hooks must start with the word "use" to follow React conventions.
Step 2: Check each option
Only "useFetchData" starts with "use" correctly.
Final Answer:
useFetchData -> Option D
Quick Check:
Custom hook names start with "use" [OK]
Hint: Custom hooks always start with 'use' prefix [OK]
A. It causes an infinite loop because setCount is called inside useEffect without dependencies
B. It returns a function instead of a value
C. It will never update count because useEffect has an empty dependency array
D. It uses useState incorrectly by initializing with 0
Solution
Step 1: Check useEffect dependencies
The effect has no dependency array.
Step 2: Understand setCount behavior
setCount(count + 1) runs on every render since there are no dependencies, causing an infinite loop of re-renders.
Final Answer:
It causes an infinite loop because setCount is called inside useEffect without dependencies -> Option A
Quick Check:
Missing deps in useEffect = infinite loop [OK]
Hint: useEffect without deps array runs every render [OK]
Common Mistakes:
Thinking the effect runs only once without deps array
Confusing return value type
Misunderstanding dependency array role
5. You want to create a custom hook useFetch that fetches data from a URL and returns { data, loading, error }. Which approach correctly handles loading state and errors inside the hook?
hard
A. Use useState for data, loading, error; use useEffect to fetch and update states with try/catch inside async function
B. Use only useState for data and ignore loading and error states
C. Fetch data directly in the component and pass it to the hook as a parameter
D. Use useEffect without useState and return data from fetch promise
Solution
Step 1: Identify state needs for fetch
Fetching data requires tracking data, loading status, and errors, so useState is needed for all three.
Step 2: Use useEffect with async fetch and error handling
Inside useEffect, define an async function with try/catch to fetch data and update states accordingly.
Final Answer:
Use useState for data, loading, error; use useEffect to fetch and update states with try/catch inside async function -> Option A
Quick Check:
Fetch with useState and useEffect + try/catch [OK]
Hint: Manage data, loading, error with useState and useEffect [OK]
Common Mistakes:
Ignoring loading or error states
Fetching outside useEffect causing infinite loops
Returning unresolved promises
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.