Component Patterns Interview Questions
With Answers & Code Examples

4 carefully curated Component Patterns interview questions with working code examples and real interview gotchas.

4questions
0beginner
2core
2advanced

What is the Compound Component pattern?

Question 1Core

Hint: Related components share implicit state via context — like <select> and <option> in HTML

Compound components work together, sharing state implicitly through context. The parent manages state; children access it without explicit prop passing.

const TabsContext = createContext(null);

function Tabs({ children, defaultTab }) {
  const [active, setActive] = useState(defaultTab);
  return (
    <TabsContext.Provider value={{ active, setActive }}>
      <div>{children}</div>
    </TabsContext.Provider>
  );
}

function TabList({ children }) {
  return <div role="tablist">{children}</div>;
}

function Tab({ id, children }) {
  const { active, setActive } = useContext(TabsContext);
  return (
    <button
      role="tab"
      aria-selected={active === id}
      onClick={() => setActive(id)}
    >
      {children}
    </button>
  );
}

function TabPanel({ id, children }) {
  const { active } = useContext(TabsContext);
  return active === id ? <div role="tabpanel">{children}</div> : null;
}

// Usage — clean, flexible, no prop drilling
<Tabs defaultTab="profile">
  <TabList>
    <Tab id="profile">Profile</Tab>
    <Tab id="settings">Settings</Tab>
  </TabList>
  <TabPanel id="profile"><ProfileView /></TabPanel>
  <TabPanel id="settings"><SettingsView /></TabPanel>
</Tabs>
💡 Good examples: Tabs, Accordion, Dropdown, Form with Field/Label/Error subcomponents. The key insight — the developer using the component controls the structure; the component controls the behavior.

What is the Render Props pattern and when would you still use it?

Question 2Core

Hint: Pass a function as a prop that returns JSX — the component calls it with its internal state

// Render props — component shares state by calling a function prop
function MouseTracker({ render }) {
  const [pos, setPos] = useState({ x: 0, y: 0 });
  return (
    <div onMouseMove={e => setPos({ x: e.clientX, y: e.clientY })}>
      {render(pos)} {/* caller decides what to render */}
    </div>
  );
}

// Usage
<MouseTracker render={({ x, y }) => (
  <p>Mouse at {x}, {y}</p>
)} />

// children as a function (same pattern)
<MouseTracker>
  {({ x, y }) => <Circle x={x} y={y} />}
</MouseTracker>

Render props vs custom hooks:

  • Custom hooks replaced most render prop use cases (simpler, less nesting)
  • Render props still shine when you need to inject JSX into a specific DOM location managed by the component
  • Libraries like React Table, Downshift, and Formik use render props for maximum flexibility
💡 If the render prop is just sharing state logic, refactor to a custom hook. If it's about rendering into a specific slot (like a virtualized list rendering each row), render props are still the right tool.

What is the Provider pattern and how do you build a scalable context architecture?

Question 3Advanced

Hint: Separate state logic from context provision — custom hook for consumers, provider component for setup

// ─── auth-context.tsx ───────────────────────────────────────────────────────
type AuthContextValue = {
  user: User | null;
  login: (credentials: Credentials) => Promise<void>;
  logout: () => void;
};

const AuthContext = createContext<AuthContextValue | null>(null);

// Provider encapsulates all auth logic
export function AuthProvider({ children }: { children: ReactNode }) {
  const [user, setUser] = useState<User | null>(null);

  const login = useCallback(async (creds) => {
    const user = await api.login(creds);
    setUser(user);
  }, []);

  const logout = useCallback(() => {
    api.logout();
    setUser(null);
  }, []);

  return (
    <AuthContext.Provider value={{ user, login, logout }}>
      {children}
    </AuthContext.Provider>
  );
}

// Custom hook — type-safe, error if used outside provider
export function useAuth() {
  const ctx = useContext(AuthContext);
  if (!ctx) throw new Error('useAuth must be used within AuthProvider');
  return ctx;
}

// ─── app layout ──────────────────────────────────────────────────────────────
<AuthProvider>
  <ThemeProvider>
    <QueryClientProvider client={queryClient}>
      <App />
    </QueryClientProvider>
  </ThemeProvider>
</AuthProvider>
💡 The null-check in useAuth() is critical for DX — it gives a clear error message instead of a cryptic "cannot read property of undefined" when someone forgets the provider.

What is the Container/Presentational pattern? Is it still relevant?

Question 4Advanced

Hint: Smart vs dumb components — data fetching separated from rendering; largely replaced by hooks but the principle remains

The pattern separates data concerns (fetching, state) from UI concerns (rendering, styling):

// ❌ Old way — explicit Container component
function UserListContainer() {
  const [users, setUsers] = useState([]);
  useEffect(() => { fetchUsers().then(setUsers); }, []);
  return <UserList users={users} />; // pass data down
}

function UserList({ users }) {
  return users.map(u => <UserCard key={u.id} user={u} />);
}

// ✅ Modern way — custom hook achieves the same separation
function useUsers() {
  return useQuery({ queryKey: ['users'], queryFn: fetchUsers });
}

// Presentational component stays the same
function UserList({ users }) {
  return users.map(u => <UserCard key={u.id} user={u} />);
}

// Consuming component — much simpler
function UsersPage() {
  const { data: users } = useUsers();
  return <UserList users={users ?? []} />;
}

Still relevant principles:

  • Keep components that render UI free of data-fetching — easier to test, reuse in Storybook
  • The separation now lives in custom hooks, not wrapper components
💡 The pattern's goal (separation of concerns) is still correct; the implementation changed. Hooks achieve it more elegantly without extra component nesting.

Other React Interview Topics

Ready to practice Component Patterns?

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