Skip to content

Is styled components feasible for next app router? #50473

Answered by devjiwonchoi
aomini asked this question in App Router
Discussion options

You must be logged in to vote

In App Router, use styled-components API to create a global registry component.

Example:

// [lib/registry.tsx]

'use client';
 
import React, { useState } from 'react';
import { useServerInsertedHTML } from 'next/navigation';
import { ServerStyleSheet, StyleSheetManager } from 'styled-components';
 
export default function StyledComponentsRegistry({
  children,
}: {
  children: React.ReactNode;
}) {
  // Only create stylesheet once with lazy initial state
  // x-ref: https://reactjs.org/docs/hooks-reference.html#lazy-initial-state
  const [styledComponentsStyleSheet] = useState(() => new ServerStyleSheet());
 
  useServerInsertedHTML(() => {
    const styles = styledComponentsStyleSheet.g…

Replies: 3 comments 5 replies

Comment options

You must be logged in to vote
3 replies
@aomini
Comment options

@AndersDJohnson
Comment options

@aomini
Comment options

Comment options

You must be logged in to vote
2 replies
@ceddybi
Comment options

@ceddybi
Comment options

Answer selected by aomini
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants