React · Lesson 15 of 15 · 5 min
Advanced React Features & Modern Concepts
This is what separates senior candidates from mid-level. You need to discuss these trade-offs in a system design interview.
When to memoize (and when not to)
Memoization hooks (React.memo, useCallback, useMemo) prevent re-renders by caching values. You use them to solve performance problems, not to write "cleaner" code.
The root problem is referential equality. An object or function created inside a component's body is new on every render. Passing it as a prop to a child component will cause that child to re-render, even if the "value" is identical. useMemo gives you a stable object reference across renders; useCallback does the same for a function. React.memo wraps a component and does a shallow comparison of its props, preventing a re-render if the props are shallowly equal to the last render.
The trap: Premature optimization. Memoization is not free. It uses memory and adds a layer of complexity. Incorrect dependency arrays are a common source of bugs, creating stale closures or infinite loops. Never apply memoization hooks without first identifying a bottleneck with the React DevTools profiler.
In an interview, your goal is to show you understand the cost, not just the benefit. When asked if you'd optimize a component, start your answer with, "First, I'd profile it to confirm it's a bottleneck." This signals seniority. Then, you can discuss the appropriate tool for the job.
How to keep the UI responsive with concurrency
React 18's concurrent features let you mark certain state updates as non-urgent. This allows React to interrupt a slow, non-urgent render to handle something more important, like user input. The UI remains responsive instead of freezing.
useTransition is the primary tool. It gives you an isPending flag and a startTransition function. You wrap your non-urgent state update in startTransition. While that update is rendering, isPending is true, and the UI remains interactive.
A classic use case is a search input that filters a large list. The input value state update is urgent; you want to see the character you just typed. The state update that filters the list is non-urgent; it can happen a few milliseconds later. Wrapping the list filter update in startTransition prevents the input from lagging while the list re-renders.
useDeferredValue is similar but works on a value, not a state update function. It's useful when you don't control the code that triggers the update. It takes a value and returns a new copy of that value that will "lag behind" during an urgent render.
This is not the same as debouncing. Debouncing guarantees a function won't be called until after a certain period of inactivity. Transitions are integrated into React's scheduler; React decides when to work on the non-urgent update, and it can be interrupted.
How to prevent a single component from crashing your app
An uncaught JavaScript error in one component will unmount the entire React component tree. Error Boundaries are components that catch these errors in their children, log them, and display a fallback UI.
They must be class components that define either static getDerivedStateFromError() or componentDidCatch(). The first is used to update state to render the fallback UI. The second is for side effects, like logging the error to a service.
The catch: Error Boundaries do not catch all errors. They miss errors inside event handlers, asynchronous code (like setTimeout or requestAnimationFrame callbacks), server-side rendering, and errors thrown in the error boundary component itself. You still need try...catch for event handlers and promises.
Place them strategically. You can wrap top-level route components, or place them around specific widgets you know might be fragile, like a third-party data visualization component.
import React, { Component, ErrorInfo, ReactNode } from 'react';
interface Props {
children: ReactNode;
fallbackUI: ReactNode;
}
interface State {
hasError: boolean;
}
class ErrorBoundary extends Component<Props, State> {
public state: State = {
hasError: false,
};
public static getDerivedStateFromError(_: Error): State {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
// You can log the error to an external reporting service here
console.error("Uncaught error:", error, errorInfo);
}
public render() {
if (this.state.hasError) {
return this.props.fallbackUI;
}
return this.props.children;
}
}
export default ErrorBoundary;
How Server Components change everything
React Server Components (RSC) are a new architecture, not just an optimization. They let you write components that run exclusively on the server. Their code is never downloaded to the client, which means they have zero impact on your bundle size.
This is different from Server-Side Rendering (SSR). SSR renders your components to HTML on the server, but it still sends all the component JavaScript to the client for hydration. RSCs run on the server and send a special description of the UI to the client, which React then uses to update the DOM.
Server Components can directly access the backend (databases, file systems, internal APIs) without needing an API endpoint. They cannot use state (useState) or effects (useEffect) because they are not interactive.
Components that need interactivity are marked as Client Components with the 'use client' directive at the top of the file. Your app becomes a tree of Server Components (the default) with islands of interactive Client Components inside them.
Suspense is the mechanism that makes this work seamlessly. If a Server Component is fetching data, React can send a loading fallback immediately, then stream the final UI once the data is ready, without blocking the rest of the page.
In an interview, you must be able to articulate the difference between RSC and SSR. Focus on the zero-bundle-size aspect and the ability to directly access server resources as the key benefits.
Try it
Implement an ErrorBoundary component from the code above. Create a child component that throws an error on a button click. Wrap the child in your boundary and verify that clicking the button shows the fallback UI, but the rest of the application remains interactive.
Next: You've finished the course. Time to put it all together in a mock interview.
Lock it in — apply this lesson under interview conditions.