Felt fine here, covered batching and the fact that setState is async in terms of when the re-render happens.
Start by defining useState as a React Hook that adds state to functional components, then explain the internal mechanics: how React tracks state via a linked list of hooks, batches updates, and triggers re-renders. Use a simple counter example to illustrate the process from setState call to DOM update, emphasizing the asynchronous and batched nature of updates.
Pro tip: Mention that state updates are batched in React 18+ even in promises, timeouts, and native event handlers due to automatic batching, and that using the functional update form (prev => prev + 1) avoids stale state issues when updates depend on previous values.
Explain that useState is a Hook that lets you add React state to function components, returning a state variable and a setter function. Mention that it's called at the top level of a component to preserve state between renders.
Explain that React maintains a linked list of hooks for each component instance, and useState initializes or retrieves the current state from the corresponding hook node. The setter function schedules an update and triggers a re-render.
Detail how React batches multiple state updates within event handlers (and in React 18+, in promises, timeouts, etc.) to avoid unnecessary re-renders. Updates are queued and processed asynchronously, often before the next paint.
Describe how after state updates are processed, React re-renders the component with the new state, compares the new virtual DOM with the previous one (reconciliation), and updates the real DOM efficiently.
Use a simple counter component to illustrate the entire flow: clicking a button calls setCount, React batches the update, re-renders the component with the new count, and updates the DOM.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Start by explaining the purpose of the dependency array in useEffect: it tells React when to re-run the effect by comparing dependencies between renders. Then, walk through the most common mistakes, such as missing dependencies, using objects/arrays as dependencies, and misunderstanding the empty array. Finally, tie it back to root cause analysis and trade-offs, emphasizing how these mistakes can lead to bugs and performance issues.
Pro tip: Mention that the dependency array is not a performance optimization tool but a correctness mechanism—React uses it to avoid stale closures. Also, highlight that using the ESLint plugin (exhaustive-deps) can catch many mistakes, but it's not a silver bullet; developers must understand the underlying principles.
Describe how useEffect accepts a second argument, an array of dependencies. React compares these dependencies between renders using Object.is and re-runs the effect only if any dependency has changed.
Discuss how omitting dependencies can cause stale closures, where the effect captures outdated values from the component scope. This often leads to bugs that are hard to trace.
Explain that including objects, arrays, or functions as dependencies can cause infinite loops because they are recreated on every render. Solutions include useMemo, useCallback, or moving them inside the effect.
Clarify that an empty array means the effect runs only once after the initial render, but it can still capture stale props/state. It's not equivalent to componentDidMount and should be used carefully.
Emphasize that understanding the dependency array is crucial for debugging. Trade-offs include using useCallback/useMemo to stabilize dependencies versus adding complexity, and relying on lint rules versus manual control.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Short answer, covered subscriptions and timers.
Start by clearly defining the cleanup function as the optional return value of useEffect, then explain its purpose: to clean up side effects before the component unmounts or before the effect re-runs. Focus on when it's actually needed—subscriptions, timers, event listeners, and async operations—and emphasize that it prevents memory leaks and stale behavior.
Pro tip: Mention that cleanup runs before every re-execution of the effect, not just on unmount, and that returning a cleanup function is a best practice even if you think it's unnecessary—it shows foresight and prevents subtle bugs.
Explain that useEffect can return a function that React calls to clean up the previous effect. This function runs before the component unmounts and before the effect runs again due to dependency changes.
Describe the problems without cleanup: memory leaks, stale subscriptions, duplicate event listeners, and race conditions. Emphasize that cleanup ensures resources are released and the effect doesn't interfere with subsequent runs.
List scenarios where cleanup is essential: subscriptions (e.g., WebSocket, Firebase), timers (setTimeout/setInterval), event listeners (window resize, scroll), and asynchronous requests with cancellation (AbortController).
Clarify that cleanup is unnecessary for effects that don't create persistent side effects, such as logging or simple state updates. However, it's still good practice to return a cleanup function if there's any doubt.
Mention that cleanup should be idempotent and not cause side effects. Discuss the trade-off between over-cleaning (unnecessary complexity) and under-cleaning (bugs), and how to test cleanup behavior.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
This is the question I wish I'd thought about more carefully before the interview.
Start by defining what useMemo does and the cost model behind it, then clearly distinguish scenarios where memoization saves expensive recomputation versus where it adds unnecessary overhead. Use concrete examples to illustrate both cases, and emphasize that the decision should be driven by profiling and measurement, not by default.
Pro tip: Mention that useMemo is not a semantic guarantee—React may discard memoized values—so never rely on it for correctness, only for performance. Also, highlight that in most cases, simpler alternatives like lifting state, splitting components, or using React.memo yield better results with less complexity.
Briefly state that useMemo caches the result of a function between renders, avoiding recomputation if dependencies haven't changed. But it also incurs overhead: storing the value, comparing dependencies, and potentially increasing memory usage.
Describe cases where the computation is genuinely expensive (e.g., complex calculations, large array transformations) or where referential equality is needed for downstream optimizations (e.g., passing a memoized object to a React.memo component).
Explain that for cheap computations, the overhead of dependency comparison and cache management can outweigh the savings. Also, overusing useMemo can lead to stale closures, memory leaks, and code complexity without measurable benefit.
Stress that the decision should be based on profiling (e.g., React DevTools Profiler) to confirm that the computation is a bottleneck. Premature optimization is a common pitfall.
Conclude that useMemo is a tool for specific, measured performance issues, not a default. Prefer simpler optimizations first, and only add useMemo when profiling shows a clear win.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Pretty clean answer, useCallback is just useMemo for functions.
Start by defining both hooks clearly, emphasizing that useMemo memoizes a computed value while useCallback memoizes a function reference. Then explain the decision criteria: use useMemo for expensive calculations or to preserve referential equality of objects/arrays, and use useCallback to stabilize function identities passed to optimized child components or used in dependency arrays. Finally, discuss trade-offs and when to avoid overusing them.
Pro tip: Mention that premature optimization with these hooks can hurt performance and readability; always profile first and only memoize when you have a measurable bottleneck or a clear referential equality requirement.
Explain that useMemo returns a memoized value, recomputing it only when dependencies change. It's used to avoid expensive recalculations on every render.
Explain that useCallback returns a memoized version of a callback function, changing only when dependencies change. It's used to prevent unnecessary re-renders of child components that rely on reference equality.
Highlight that useMemo is for values and useCallback is for functions, but useCallback(fn, deps) is essentially useMemo(() => fn, deps). Both help with referential equality and performance optimization.
Describe when to use each: use useMemo for expensive computations or to keep object/array references stable; use useCallback for event handlers or functions passed to memoized child components (React.memo) or included in dependency arrays.
Discuss that overusing these hooks can add complexity and memory overhead without benefit. Recommend profiling and only optimizing when necessary, and note that React may discard memoized values in future.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Talked about persisting values across renders without triggering a re-render.
Start by acknowledging the common use of useRef for DOM references, then pivot to its broader purpose as a mutable container that persists across renders without causing re-renders. Explain how this makes it useful for storing any value that should not trigger a render when updated, and give concrete examples like timers, previous values, and instance variables.
Pro tip: Emphasize that useRef is not just for DOM—it's a general-purpose 'box' for mutable data. Mention that unlike state, updating ref.current does not cause a re-render, which is crucial for performance-sensitive scenarios.
Briefly mention the typical use case of accessing DOM elements, showing you understand the basics.
Explain that useRef returns a mutable object whose .current property persists across renders and does not trigger re-renders when changed.
Provide examples such as storing timer IDs, previous state/props, instance variables, or any value that needs to be mutable without causing re-renders.
Highlight the difference: state updates cause re-renders, while ref updates do not, making refs ideal for non-visual data.
Discuss how using refs can optimize performance by avoiding unnecessary renders, but caution about overuse and potential pitfalls like stale closures.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Covered the basics but stumbled a little on the performance side.
Start by explaining the mechanics of useContext: how it creates a global state accessible to any component in the tree without prop drilling. Then, discuss the performance implications: context updates trigger re-renders of all consuming components, which can be costly if not optimized. Finally, mention strategies to mitigate performance issues, such as splitting contexts, memoizing values, and using selectors.
Pro tip: Emphasize that useContext is not a state management solution by itself; it's a dependency injection mechanism. Combine it with useReducer or external stores for complex state, and always consider the re-render cost in large applications like those at Boeing.
Describe how useContext allows components to subscribe to a context object, avoiding prop drilling. Mention that it's often used with createContext and a Provider at the top of the tree.
Explain that when the context value changes, all components that consume that context will re-render, regardless of whether they use the changed part of the value. This can lead to performance bottlenecks.
Highlight that frequent updates or large context values can cause unnecessary re-renders, impacting performance. Mention that this is especially critical in large-scale applications with many consumers.
Suggest strategies like splitting contexts to isolate updates, memoizing context values with useMemo, and using libraries like use-context-selector to enable selective re-rendering.
Connect the discussion to system design trade-offs, such as when to use context versus other state management solutions (e.g., Redux, Zustand) based on performance needs and application complexity.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Rules of hooks came up here: only call hooks at the top level, only call them from React functions.
Start by defining what a custom hook is and why you'd build one, then walk through a concrete example (e.g., useFetch or useForm) that shows extraction of reusable logic. Finally, explicitly state the rules of hooks and how you enforce them, tying back to code quality and maintainability.
Pro tip: Mention that custom hooks are about sharing logic, not state, and that you can use tools like ESLint's react-hooks plugin to catch rule violations early. This shows you care about correctness and team scalability.
Explain that a custom hook is a JavaScript function whose name starts with 'use' and that calls other hooks, allowing you to extract and reuse stateful logic across components.
Describe a simple custom hook like useFetch that manages loading, error, and data states, and show how it's used in a component.
List the two core rules: only call hooks at the top level (not inside loops, conditions, or nested functions) and only call hooks from React function components or other custom hooks.
Mention that ESLint's react-hooks plugin enforces these rules, and highlight benefits like reusability, testability, and separation of concerns.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
State change, prop change, context change, parent re-render.
Start by clearly explaining the common triggers of re-renders in React, then transition into a systematic diagnostic process. Emphasize a methodical approach to identify the root cause, using tools like React DevTools and profiling, and discuss potential fixes.
Pro tip: Mention that unexpected re-renders often stem from parent component re-renders or context changes, and highlight the importance of using React.memo and useCallback to optimize performance.
List the primary causes: state changes, prop changes, context changes, and parent re-renders. Briefly explain each.
Mention React DevTools Profiler, console.log statements, and the why-did-you-render library to track re-renders.
Start by reproducing the issue, then use profiling to identify which components re-render and why. Check for unnecessary state updates or prop drilling.
Suggest optimizations like React.memo, useMemo, useCallback, and moving state closer to where it's needed. Discuss trade-offs.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.