Skip to content
tezvyn:
Menu

Architecting a themeable React component library

Source: interviewMediumHow cards are made

Summary

theming React components via Context.

Key points

a ThemeProvider supplying tokens through Context, components reading them, and CSS variables for performance.

Watch out for

prop-drilling the theme or putting fast-changing state in Context.

What's really being asked

This probes React architecture: how to distribute theme data without prop drilling and where Context helps versus hurts performance. A senior answer pairs Context with CSS variables.

The full answer

Expose a ThemeProvider component that consumers wrap their app in, passing a theme object of tokens. Internally it places that object into a React Context, and components read it through a useTheme hook. This avoids drilling theme props through every layer. For performance and SSR friendliness, have the provider also write the tokens as CSS custom properties on a wrapper element, so most styling reads from CSS rather than re-rendering React on theme changes. Memoize the context value so its identity is stable, and keep the theme relatively static. Default to a built-in theme when no provider is present so the library works out of the box.

The mistakes people make

Prop drilling the theme through every component, which is unmaintainable. Putting frequently changing state in the same Context, causing every consumer to re-render. Forcing consumers to import internal context objects directly instead of a clean hook.

What usually comes next

How do you avoid re-rendering all consumers when the theme changes? Why combine Context with CSS variables? How do you support nested providers for sub-sections with different themes?

A concrete example

You export ThemeProvider and a useTheme hook. The provider does const value = useMemo(() => theme, [theme]) and renders a div whose inline style sets --color-primary and friends from the theme, then wraps children in context. A Button uses var(--color-primary) in its CSS and only calls useTheme for logic, so swapping themes updates CSS variables instantly while React re-renders stay minimal.

Interview question

Why pair React Context with CSS custom properties in a themeable library rather than relying on Context alone?

  • a.CSS variables replace the need for a ThemeProvider entirely
  • b.CSS variables apply theme changes via styling without re-rendering every Context consumerCorrect
  • c.Context values are inaccessible during server-side rendering
  • d.Context cannot store objects, only strings
Why?

Emitting CSS variables lets style updates happen in CSS, avoiding cascade re-renders that pure Context-driven styling would trigger; Context still distributes the theme for logic. The other statements are false.

Just read this? Test yourself on what you have been reading.

Read the original → react.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on design-systems — each one lists the topics its interview covers.

See open roles