Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

styled components error in nextjs

//update in new version of next.js
// at next.config.js add styledComponents:true in compiler
 @type {import('next').NextConfig}
const nextConfig = {
  /* config options here */
  reactStrictMode:true,
  compiler:{
    styledComponents:true,
  }
}

export default nextConfig
Comment

styled of styled component not working in nextjs

//create .babelrc 
{
  "presets": ["next/babel"],
  "plugins": [["styled-components", { "ssr": true }]]
}

yarn add -D babel-plugin-styled-components

// pages/_documents.js
import Document from 'next/document'
import { ServerStyleSheet } from 'styled-components'

export default class MyDocument extends Document {
  static async getInitialProps(ctx) {
    const sheet = new ServerStyleSheet()
    const originalRenderPage = ctx.renderPage

    try {
      ctx.renderPage = () =>
        originalRenderPage({
          enhanceApp: (App) => (props) =>
            sheet.collectStyles(<App {...props} />),
        })

      const initialProps = await Document.getInitialProps(ctx)
      return {
        ...initialProps,
        styles: (
          <>
            {initialProps.styles}
            {sheet.getStyleElement()}
          </>
        ),
      }
    } finally {
      sheet.seal()
    }
  }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: import fa icons react 
Javascript :: how to execute javascript after c# function execute 
Javascript :: convert dict to json python 
Javascript :: has decimal javascript 
Javascript :: copy an array without pointer in angular 
Javascript :: jquery prop checked 
Javascript :: open page in new tab using jquery 
Javascript :: javascript get unique values from array 
Javascript :: javascript max length with elipsis 
Javascript :: open submenu jquery 
Javascript :: laravel jquery ajax post csrf 
Javascript :: timer in java script 
Javascript :: js password validation regex 
Javascript :: scrolltop top to bottom body get count 
Javascript :: put two buttons in a row in react native 
Javascript :: javascript clear sembols 
Javascript :: javascript array to comma separated list 
Javascript :: import paper material ui 
Javascript :: for key value in object javascript 
Javascript :: javascript one time event listener 
Javascript :: express hello world 
Javascript :: regex valid jwt 
Javascript :: check if an id exists javascript 
Javascript :: string to date angular 
Javascript :: js get selection start from contenteditable 
Javascript :: js html table extract data 
Javascript :: jquery change picture source 
Javascript :: react router dom npm 
Javascript :: datatable after render event 
Javascript :: js style remove property 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =