skip to content

In a React Server Components app, why have runtime CSS-in-JS libraries classically needed to run as client components, and is that still universally true?

level: seniorimportance: should knowfreq 7%

answer

  1. what a server component cannot hold
  2. theme from context
  3. a stylesheet kept in the browser
  4. inline style tags from the server
  5. theming moves to custom properties

basics

~20 s

The classic design reads the theme from context, uses hooks and keeps a stylesheet in the browser, none of which a server component has. Not universally now: styled-components, since 6.3.0, renders in server components, losing context-based theming there.

solid answer

~50 s

A classic runtime library is built on client-side React: a theme provider built on context, hooks around rendering and insertion, and a stylesheet it manages in the document. React's documentation lists `createContext`, most hooks and DOM insertion among the APIs that force a component to run on the client, and server components cannot hold state or use most hooks. So every styled component, and the module that defines it, had to sit behind the client boundary, which pulls styling code into the client bundle and stops data-fetching server components from rendering styled elements directly. That is no longer universal: styled-components 6.3.0 added server-component support, emitting inline `<style>` tags with no directive needed, and 6.4.0 reworked them into plain `<style data-styled>` tags deduplicated per render. The cost: its `ThemeProvider` is a no-op there, so theming moves to CSS custom properties.

go deeper

for a junior

Remember that server components have no state, context or effects, and that classic runtime styling libraries depend on all three.

for a middle

Explain which parts of a runtime library need the client: the context-based theme, the hooks, and the stylesheet it maintains in the document.

for a senior

Show that the rule is no longer universal, and name the costs of rendering styles in server components: no context theme, sibling style tags affecting selectors, and static-first styling.

for a principal

Decide whether shared components should stay client-side, adopt server-capable runtime styling, or move to extraction, weighing bundle size, theming architecture and migration effort.

## What a server component cannot do In React's **Server Components** model, a server component renders only on the server and is not kept in memory afterwards. React's documentation spells out the limits that matter here: - server components **cannot use most hooks** and cannot hold their own state; - third-party components that use `createContext`, React's hooks (apart from `use` and `useId`) or client APIs such as **DOM insertion** must run on the client. Anything relying on those has to live behind the client boundary. The directive's own mechanics are a separate topic; what matters here is why styling libraries ended up on the client side of it. ## Why the classic runtime design lands on the client A **runtime CSS-in-JS library** in its classic form depends on exactly those client capabilities: 1. **Theme from context.** Styles interpolate `props.theme`, supplied by a provider component built on React context. 2. **Hooks around rendering.** The library uses hooks to read that context, memoise work and insert rules at the right moment. 3. **A stylesheet in the document.** In the browser it creates and maintains `<style>` elements, inserting rules as new classes appear. A server component has no context, no state and no effects, so a styled component could not be one. On a concert-ticketing site moving to server components, every event card, seat legend and checkout panel written with such a library had to become a client component. The consequences: - the styling code and every module behind those components ship in the client bundle; - a server component that fetches event data cannot render a styled element itself; it passes data or children into a client wrapper; - the per-promoter theme provider sits at the top of a client subtree. Framework documentation still reflects this picture: Next.js 16.3.8's CSS-in-JS guide lists the libraries it supports, runtime ones included, as supported in client components, and notes Emotion as "currently working on support". ## What changed "Every runtime library must be a client component" is no longer true as a universal statement. styled-components is the clearest example in its changelog: | Release | Change in server components | |---|---| | 6.3.0 | Detects a server-component environment; components work there **without a client directive** and emit inline `<style>` tags; `ThemeProvider` and `StyleSheetManager` become no-ops | | 6.4.0 | Emits plain inline `<style data-styled>` tags, **deduplicated per render** through `React.cache`; `StyleSheetManager`'s `stylisPlugins` and `shouldForwardProp` now apply; adds `createTheme`, which expresses a theme as CSS custom properties usable in both server and client components | The price is visible in the same notes: - **No context-based theme.** Because `ThemeProvider` passes children through untouched in server components, 6.3.0 recommends build-time CSS variable theming instead. - **Selector side effects.** Inline `<style>` tags are real DOM children, so `:first-child`, `:nth-child()` and adjacent-sibling selectors can miscount; 6.4.0 added an opt-in plugin, `stylisPluginRSC`, that rewrites the child-index selectors, while `:first-of-type` and `:nth-of-type()` are unaffected. - **Dynamic interpolations.** 6.3.0's guidance is to prefer static styles and use data attributes and custom properties for variation. ## Zero-runtime libraries and server components A build-time extraction library sidesteps the question. Its output is a static stylesheet linked in the page and class names exported as plain strings; nothing about applying a class needs context, state or an effect at render time, so a server component can use those class names like any other string. ## What to audit before moving a styled component server-side - **Theme interpolations**: every `props.theme` read must become a custom property or a static value. - **Child-index selectors**: `:first-child`, `:nth-child()` and adjacent-sibling rules may need rewriting. - **Prop-driven interpolations**: prefer data attributes for variants and inline custom properties for continuous values. - **Interactive state**: anything driven by state or event handlers stays in a client component regardless of the styling library. ## Choosing for the ticketing site - **Keep the runtime library behind a client boundary** when theming through context is central and the client bundle cost is acceptable. - **Render styled components in server components** with a library that supports it, after moving the per-promoter theme into custom properties and auditing child-counting selectors. - **Move to extraction** when the goal is server components that carry no styling work at all.

  • How does a per-promoter theme survive in server components without context?
    Express the theme as CSS custom properties set on a wrapping element or the root, and let styles read `var(--promoter-accent)`. The value travels through the cascade rather than through React context. styled-components 6.4.0's `createTheme` follows this pattern, turning each theme leaf into a `var(--prefix-path, fallback)` reference.
  • Why can inline style tags from server components break a selector such as :first-child?
    Each emitted `<style>` element sits in the DOM next to the element it styles, so it counts as a sibling. A rule like `li:first-child` may then match nothing because a style element is the first child. Type-based selectors such as `:first-of-type` ignore those tags because they filter by element name.

  • Every runtime CSS-in-JS library must sit behind a client boundary, with no exceptions
  • A runtime library rendering in a server component still reads the theme from context
  • Server components cannot output class attributes, so styling must be client-side
  • Inline style tags emitted beside elements have no effect on other selectors
  • A zero-runtime library also needs a client boundary to apply its class names