0
0
Reactframework~5 mins

Reusing logic with hooks in React - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a custom hook in React?
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
Which React hook is commonly used inside custom hooks to manage side effects like data fetching?
AuseEffect
BuseState
CuseContext
DuseRef
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
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
Can a custom hook return multiple values?
ANo, only one value
BOnly strings
COnly numbers
DYes, usually as an object or array
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.