Real-world mechanisms, state management, edge cases, performance trade-offs, and practical coding.
Answer: The Context API is React's built-in system for passing data down the component tree without manually threading props through intermediate layers (avoiding "prop drilling").
How it works:
React.createContext(defaultValue): Creates a Context object.Provider: A component that wraps the component subtree and accepts a value prop.useContext(Context): A hook that lets components subscribe to context changes.When to use it: Use Context for global, low-frequency updates, such as user authentication state, UI themes (light/dark mode), active locales/languages, or shared multi-step wizard state.
The Performance Problem (Unnecessary Re-renders):
Whenever the value of a Context Provider changes, all descendant components that consume that Context will re-render, even if they only use a subset of the context value that didn't change. Furthermore, if the Provider passes an object literal (e.g., value={{ state, dispatch }}), a new object reference is created on every render, triggering updates for all consumers.
How to optimize and prevent these re-renders:
useMemo so its reference remains stable unless the state changes.const contextValue = useMemo(() => ({ state, dispatch }), [state]);
return <MyContext.Provider value={contextValue}>{children}</MyContext.Provider>;
React.memo, or use child components passed as children (which React won't re-render unless their parent changes props).Answer:
useMemo and useCallback are optimization hooks designed to prevent expensive recalculations and unnecessary child component re-renders by caching values and function references.
useMemo(fn, deps):useCallback(fn, deps):React.memo) to avoid breaking prop-equality checks.useCallback(fn, deps) is syntactically equivalent to useMemo(() => fn, deps).Performance Overhead & Overuse:
Developers often fall into the trap of wrapping every function in useCallback and every computation in useMemo. This can actually hurt performance due to:
Object.is) on every single render.When to use them:
useMemo when performing complex computations (e.g., filtering or sorting arrays with thousands of items).useCallback when passing a callback to a child component optimized with React.memo, or when the function itself is a dependency in another hook (e.g., inside a useEffect).Answer: The Virtual DOM diffing process determines how to update the real DOM when state changes. A generic tree comparison algorithm has a time complexity of $O(n^3)$. To achieve real-time performance, React uses a heuristic O(n) algorithm based on two main assumptions:
<div> to <span>, or from a <Counter> component to a <Profile> component), it doesn't try to diff them. Instead, it tears down the entire subtree, destroys its state, and mounts a brand new tree from scratch.key prop.
When comparing children of the same parent, React matches keys to map nodes from the old tree to the new tree. This allows it to efficiently detect when items are inserted, deleted, or reordered without rebuilding the entire list.Reconciliation details for same-type elements: If two React elements are of the same type, React keeps the DOM node, updates only the changed attributes or CSS classes, and then recursively diffs their children.
Answer:
A Custom Hook is a JavaScript function whose name starts with use and can call other React hooks. They are the primary mechanism in React for reusing stateful logic across multiple components.
Key characteristics:
The Rules of Hooks (which custom hooks must also follow):
Answer: React Portals provide a way to render a component's virtual DOM tree into a physical DOM node that exists outside the parent component's DOM hierarchy.
ReactDOM.createPortal(child, containerNode)
Common Use Cases:
overflow: hidden, position: relative, or custom z-index stacking contexts.Portals & Event Bubbling: Crucially, even though a portal component renders somewhere else in the physical DOM, it still behaves like a normal React child component in terms of React's event system. This means that:
You've completed the 5 free sample questions. Get unrestricted lifetime access to every question, model answer, implementation challenge, and all 27+ technologies for a single payment.
₹399 India / $9 International · One-time settlement · Zero subscription