Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views2 pages

React Hooks Interview CheatSheet

React Hooks are functions that enable functional components to utilize state and lifecycle features without class components, enhancing code reusability and readability. Key hooks include useState, useEffect, and useContext, while additional hooks like useReducer and useRef cater to more complex scenarios. Adhering to the Rules of Hooks is crucial to avoid bugs and maintain state order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

React Hooks Interview CheatSheet

React Hooks are functions that enable functional components to utilize state and lifecycle features without class components, enhancing code reusability and readability. Key hooks include useState, useEffect, and useContext, while additional hooks like useReducer and useRef cater to more complex scenarios. Adhering to the Rules of Hooks is crucial to avoid bugs and maintain state order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

React Hooks Interview Cheat Sheet

Hooks are special functions introduced in React . That allow functional components to
use state and lifecycle features without writing class components. They simplify code,
make logic reusable, and improve readability.

Rules of Hooks
1. Call hooks only at the top level (not inside loops, conditions, or nested functions).

2. Call hooks only from React functional components or custom hooks.

Types of Hooks

Basic Hooks
• useState – For managing component state.

• useEffect – For handling side effects (API calls, subscriptions, timers).

• useContext – For accessing Context API and avoiding prop drilling.

Additional Hooks
• useReducer – Alternative to useState for complex state management.

• useRef – For accessing DOM elements or persisting mutable values.

• useMemo – Memoizes values to optimize performance.

• useCallback – Memoizes functions to prevent unnecessary re-renders.

• useLayoutEffect – Similar to useEffect, but runs synchronously before paint.

• useImperativeHandle – Customizes the instance value exposed when using refs.

Custom Hooks
• Developer-defined reusable hooks that start with 'use'.

• Example: useFetch, useAuth, useLocalStorage.

Common Interview Questions

Q1. What are Hooks in React?


Hooks are special functions that let functional components use state and lifecycle features.
Q2. Why were Hooks introduced?
To avoid class components, make code reusable with custom hooks, and improve
readability.

Q3. Difference between useEffect and useLayoutEffect?


• useEffect runs after render (good for async tasks like API calls).
• useLayoutEffect runs before the browser paints (good for DOM measurements).

Q4. Difference between useMemo and useCallback?


• useMemo memoizes values (expensive calculations).
• useCallback memoizes functions (to prevent unnecessary re-creations).

Q5. When would you use useReducer instead of useState?


Use useReducer when state logic is complex and involves multiple transitions (e.g., forms,
carts).

Q6. What are Custom Hooks?


Custom hooks allow you to extract and reuse stateful logic across components.

Q7. What happens if you break the Rules of Hooks?


React will lose track of state order, leading to bugs and errors. Hooks must follow the rules
strictly.

Quick Summary (One-Liners)


• useState → Adds state to functional components.

• useEffect → Handles side effects (API, subscriptions).

• useContext → Consumes context (no prop drilling).

• useReducer → Complex state management.

• useRef → Access DOM elements & persist values.

• useMemo → Optimize expensive calculations.

• useCallback → Optimize function references.

• useLayoutEffect → Runs before paint for layout fixes.

• Custom Hooks → Reusable logic across components.

You might also like