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 :: character to ascii in js 
Javascript :: javascript to convert rgb to hsl 
Javascript :: convert string to datetime javascript 
Javascript :: node js sublime text 
Javascript :: tostring javascript 
Javascript :: js insert string at position 
Javascript :: jquery datatables turn off sorting 
Javascript :: react native build apk 
Javascript :: protractor element.all for eahc 
Javascript :: javascript regex replace all 
Javascript :: file name without extension javascript 
Javascript :: vue htmlWebpackPlugin.options.title 
Javascript :: javascript iterate through object 
Javascript :: valid phone number regex with country code 
Javascript :: jquery change value 
Javascript :: random light color js 
Javascript :: how to play music in js 
Javascript :: references another schema in sequelize 
Javascript :: js remove from array by value 
Javascript :: Access data out of Axios .then vue.js 
Javascript :: jquery change href value 
Javascript :: H. Nazmul 
Javascript :: network response timed out expo 
Javascript :: limitar la cantidad de decimales en javascript 
Javascript :: discord.js get all members with role 
Javascript :: JS not executing 
Javascript :: react redux actions must be plain objects 
Javascript :: axios upload progress react 
Javascript :: joi enum validation 
Javascript :: redirect page using javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =