Advanced Patterns Interview Questions
With Answers & Code Examples

3 carefully curated Advanced Patterns interview questions with working code examples and real interview gotchas.

3questions
0beginner
0core
3advanced

What is Error Boundary and how do you implement one?

Question 1Advanced

Hint: Class component wrapping the tree — catches render errors and shows fallback UI instead of blank screen

Error Boundaries catch JavaScript errors during rendering, in lifecycle methods, and in constructors of child components. They must be class components (no hook equivalent yet).

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false, error: null };
  }

  // Render phase — update state to show fallback
  static getDerivedStateFromError(error) {
    return { hasError: true, error };
  }

  // Commit phase — log error to monitoring service
  componentDidCatch(error, info) {
    logErrorToSentry(error, info.componentStack);
  }

  render() {
    if (this.state.hasError) {
      return this.props.fallback ?? <h2>Something went wrong.</h2>;
    }
    return this.props.children;
  }
}

// Usage — wrap features, not the entire app
function App() {
  return (
    <ErrorBoundary fallback={<ErrorPage />}>
      <ErrorBoundary fallback={<WidgetError />}>
        <SomeWidget />
      </ErrorBoundary>
      <MainContent />
    </ErrorBoundary>
  );
}

// react-error-boundary library — hook-friendly wrapper
import { ErrorBoundary } from 'react-error-boundary';

<ErrorBoundary
  FallbackComponent={ErrorFallback}
  onError={logError}
  onReset={resetApp}
>
  <App />
</ErrorBoundary>
💡 Error boundaries do NOT catch errors in event handlers, async code (setTimeout, fetch), or SSR. Use try/catch for those. Place boundaries at feature level, not component level — granular boundaries give better UX.

What is the Suspense pattern and data fetching with it?

Question 2Advanced

Hint: Suspend rendering while async work completes — component throws a Promise; Suspense catches it and shows fallback

Suspense lets components "wait" for something before rendering. The component throws a Promise; React catches it and shows the fallback.

// React.lazy — built-in Suspense integration
const Chart = lazy(() => import('./Chart'));

// React Query with Suspense mode
function UserProfile({ id }) {
  // This will suspend (throw) if data isn't ready yet
  const { data: user } = useSuspenseQuery({
    queryKey: ['user', id],
    queryFn: () => fetchUser(id),
  });

  return <p>{user.name}</p>; // Only renders when data is ready
}

// Wrapping with Suspense
function Page() {
  return (
    <Suspense fallback={<Skeleton />}>
      <UserProfile id={1} />
    </Suspense>
  );
}

// Nested Suspense — granular loading states
function Dashboard() {
  return (
    <Suspense fallback={<PageSkeleton />}>
      <UserProfile />
      <Suspense fallback={<ChartSkeleton />}>
        <Chart />  {/* loads independently */}
      </Suspense>
    </Suspense>
  );
}
💡 Suspense eliminates the isLoading + early return pattern. The component is always in a "ready" state — Suspense handles the loading state above it. Combine with Error Boundaries for complete coverage.

What is prop spreading and when is it an anti-pattern?

Question 3Advanced

Hint: {...props} forwards all props — useful for wrapper components but can pass wrong props to DOM elements

// ✅ Legitimate use — forwarding unknown props to underlying element
function Button({ variant, className, ...rest }) {
  return (
    <button
      className={`btn btn-${variant} ${className}`}
      {...rest}  // forwards onClick, disabled, type, aria-*, etc.
    />
  );
}

// ❌ Anti-pattern 1 — unknown DOM attributes cause React warnings
function Box({ color, ...rest }) {
  return <div {...rest} />; // if 'color' is in rest, "Unknown prop 'color'" warning
}

// ✅ Fix — destructure all custom props, only spread standard ones
function Box({ color, size, children, ...htmlProps }) {
  return <div style={{ color }} {...htmlProps}>{children}</div>;
}

// ❌ Anti-pattern 2 — spreading all parent props down
function Parent(props) {
  return <Child {...props} />; // leaks parent-specific props to child
}

// ❌ Anti-pattern 3 — security risk with user-provided data
// If attackers control the props object, they could inject onClick etc.
const userConfig = JSON.parse(untrustedInput);
<Component {...userConfig} /> // dangerous!
💡 Always destructure custom props before spreading. "Rest" in a component should only contain valid HTML attributes for the element it spreads onto.

Other React Interview Topics

Ready to practice Advanced Patterns?

Get AI feedback on your answers, predict code output, and fix real bugs.