vercel / next.js

The React Framework
https://nextjs.org
MIT License
122.2k stars 26.17k forks source link

Warning: Prop className did not match. #7322

Closed th0th closed 4 years ago

th0th commented 5 years ago

Examples bug report

Example name

with-styled-components

Describe the bug

Also posted here: #2538

Using css prop introduced with styled-components v4 causes Warning: Prop className did not match..

To Reproduce

Add any HTML element with css prop.

Expected behavior

Correctly styled rendering on the server side without any warnings.

Screenshots

57497472-7b657c00-72e0-11e9-84c5-e7e5fa0d351c

System information

Additional context

Here is an example I created to demonstrate the issue: https://codesandbox.io/embed/jlwvwyy0ow

Open this and refresh once the building is done: https://jlwvwyy0ow.sse.codesandbox.io/

kumarabhirup commented 5 years ago

Happens to me everytime. +1

Tomekmularczyk commented 5 years ago

The same happens with Next v.7. This error doesn't occur when the server starts, only after making changes in the code.

elliotbonneville commented 5 years ago

I'm seeing the same error. It doesn't appear to affect the live app, but it's very annoying to have it constantly appearing locally.

cabralada commented 5 years ago

I have a similar error, but with inline style.

Warning: Prop `style` did not match. Server: "height:50px;margin-left:4vw;cursor:pointer;width:auto;margin-top:0px" Client: "height:30px;margin-left:4vw;cursor:pointer;width:auto;margin-top:6px"

šŸ˜¢

th0th commented 5 years ago

I hope once enough people raise their voices here this issue will get attention šŸ™‚

SarKurd commented 5 years ago

I face this issue in my simple navbar active className, is there a workaround until official fix?

timneutkens commented 5 years ago

Use šŸ‘ on the initial issue instead of "+1" and "same problem".

If you do feel like posting +1 provide a clear and concise reproduction that is clearly reproducible. Otherwise the comment is not actionable.

This goes for any open-source project you're commenting on.

Thanks šŸ™

c0b41 commented 5 years ago

Seems like they fixed latest release this bug https://github.com/styled-components/styled-components/issues/2563

th0th commented 5 years ago

Nope, that's a totally different log message, it is the one with yellow background on the screenshot I posted, it disappeared after the update.

First one, warning about different classNames is still there.

Timer commented 5 years ago

Tagging this as up for grabs to investigate by a motivated contributor.

quantizor commented 5 years ago

Hey there, please update babel-plugin-styled-components to the latest version which should fix css prop mismatches: https://github.com/styled-components/babel-plugin-styled-components/releases/tag/v1.10.1

I've noticed an error message in development still, but no ill effects from it so can be safely ignored.

ThisIsEsh commented 4 years ago

Still same after fix posted above, and can't ignore it. Having some layout problems when this error occurs.

ShayDavidson commented 4 years ago

So do I, when this occurs I'm getting layout errors and my component go into a weird state where they do not render based on new props.

It happens for me in an inline style.

quantizor commented 4 years ago

Make sure your babel plugin version is up to date, lots of fixes in the last couple weeks. We also made changes to how we do dev styles in v5 that largely address this issue

ShayDavidson commented 4 years ago

The Prop 'style' does not match is not solved with the babel plugin upgrade.

quantizor commented 4 years ago

"style" is an inline style and nothing to do with styled-components, so that's a totally separate issue

jass-trix commented 4 years ago

what's the fix for this one? i always get this warning everytime i refresh. i always do refresh everytime change something, bad old habit šŸ˜… everytime i do refresh it shows warning and the styling all gone

masives commented 4 years ago

It appears to be an issue that backend generates styled component but client doesn't get it/doesn't use it and instead of using server side rendered style client generates it by itself.

edit: I just downloaded the example and couldn't reproduce the issue while using the same code as in your codepen sandbox. Tested in chrome and firefox and it behaves the same (no error nor warning).

quantizor commented 4 years ago

@jass-trix have you tried the s-c v5 beta? We made a lot of changes to eliminate this issue.

jass-trix commented 4 years ago

@probablyup how to use this version?

JohnGrisham commented 4 years ago

It appears to be an issue that backend generates styled component but client doesn't get it/doesn't use it and instead of using server side rendered style client generates it by itself.

edit: I just downloaded the example and couldn't reproduce the issue while using the same code as in your codepen sandbox. Tested in chrome and firefox and it behaves the same (no error nor warning).

I have this issue as well after upgrading to next 9.0.2 and styled components 4.3.2 but it does not occur every time. I have to go in and out of debug a few times to replicate at certain points. I have attempted the fixes suggested through similar issues such as https://github.com/styled-components/babel-plugin-styled-components/issues/78. Seems to only effect development and I'm experiencing layout breaks in one section of the app. Still not sure if it is related to styled components or next but it does seem to have to do with SSR classnames being different on client/server I'm using the recommended babel plugin to fix this. babel-plugin-styled-components

quantizor commented 4 years ago

We changed how this all works in v5, Iā€™d recommend trying it out.

On Tue, Jul 23, 2019 at 9:42 AM JohnGrisham notifications@github.com wrote:

It appears to be an issue that backend generates styled component but client doesn't get it/doesn't use it and instead of using server side rendered style client generates it by itself.

edit: I just downloaded the example and couldn't reproduce the issue while using the same code as in your codepen sandbox. Tested in chrome and firefox and it behaves the same (no error nor warning).

I have this issue as well after upgrading to next 9.0.2 and styled components 4.3.2 but it does not occur every time. I have to go in and out of debug a few times to replicate at certain points. I have attempted the fixes suggested through similar issues such as styled-components/babel-plugin-styled-components#78 https://github.com/styled-components/babel-plugin-styled-components/issues/78. Seems to only effect development and I'm experiencing layout breaks in one section of the app. Still not sure if it is related to styled components or next but it does seem to have to do with SSR classnames being different on client/server I'm using the recommended babel plugin to fix this. babel-plugin-styled-components

ā€” You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/zeit/next.js/issues/7322?email_source=notifications&email_token=AAELFVTNGZGAJJXB2ARVGVLQA4DCZA5CNFSM4HMQ4H2KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2TEYOA#issuecomment-514214968, or mute the thread https://github.com/notifications/unsubscribe-auth/AAELFVSKJJJ4YJSZTC4J4BLQA4DCZANCNFSM4HMQ4H2A .

JohnGrisham commented 4 years ago

I was in the process of trying the beta but I appreciate your quick response šŸ‘ @probablyup

JohnGrisham commented 4 years ago

@probablyup I can confirm that this doesn't seem to be an issue in v5.0.0-beta.8. I look forward to the full release.

haywirez commented 4 years ago

I'm still getting the issue with "^5.0.0-beta.8" and "babel-plugin-styled-components": "^1.10.6" šŸ˜ž

278kunal commented 4 years ago

Still facing the same issue with "babel-plugin-styled-components": "1.10.1" and "styled-components": "3.4.0".

I am using the following config in .babelrc

"plugins": [
        [
            "babel-plugin-styled-components",
            {
                "ssr": true,
                "displayName": true,
                "preprocess": false
            }
        ],
        [
            "transform-runtime",
            {
                "regenerator": true
            }
        ],
        "transform-object-assign",
        "transform-class-properties",
        "transform-es2015-modules-commonjs",
        "transform-react-remove-prop-types",
        "transform-imports"
    ],
    "env": {
        "development": {
            "plugins": [
                [
                    "babel-plugin-styled-components",
                    {
                        "ssr": true,
                        "displayName": true,
                        "preprocess": false
                    }
                ],
                "transform-ensure-ignore"
            ]
        },
        "test": {
            "plugins": [
                "dynamic-import-node"
            ]
        }
    }
JohnGrisham commented 4 years ago

@278kunal This may be unrelated but according to styled components when envs are specified styled components plugin has to be in each of the environments. https://github.com/styled-components/babel-plugin-styled-components/issues/78. In my case I had to update to the alpha of the latest release of styled components. I would try that as well as including your plugins in all environments.

VitalyKrenel commented 4 years ago

Hey guys! The issue is still open, but no relevant comments for a month - is there any update? I have the same problem:

Warning: Prop `className` did not match. Server: "sc-bwzfXH bBouZQ" Client: "sc-bdVaJa cVPsWU"

Technically it's fine because it does not affect production, but the message itself is slightly annoying.

dubKelly commented 4 years ago

https://github.com/styled-components/babel-plugin-styled-components/issues/78#issuecomment-361160935

nilportugues commented 4 years ago

@dubKelly thanks for sharing the solution. @timneutkens you way close this... or at least update the example with the working solution :)

JohnGrisham commented 4 years ago

@nilportugues While I can confirm that updating to the latest beta of styled components and using the plugin for styled-components as well as including the plugins for all environments this has only been a partial fix. I am still experiencing this warning after applying all of the solutions. This is more of an issue with styled components I believe since the warning is a result of styles being rendered server side and then rendered as another name in the client. The styled-components plugin is supposed to solve this issue and keep the hashing consistent. https://github.com/styled-components/babel-plugin-styled-components

xphenixo commented 4 years ago

Hi, i solved the problem by using the following set up and it is working just fine!

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()
    }
  }
}

https://github.com/zeit/next.js/tree/canary/examples/with-styled-components

JohnGrisham commented 4 years ago

Potentially solved by https://github.com/styled-components/styled-components/releases/tag/v4.4.0 I have not had time to test this yet.

timneutkens commented 4 years ago

Checked with @probablyup and it seems that that release should solve the issue indeed.

sangdth commented 4 years ago

I'm not using styled-components and still get this issue. Please. There are many people here already said this.

timneutkens commented 4 years ago

@sangdth please read this thread: https://twitter.com/timneutkens/status/1154351052973625346

nvuhung commented 4 years ago

I am applying all of solutions but it's not working, the issue still happen.

"next": "^9.0.7", "styled-components": "^4.4.0", "babel-plugin-styled-components": "^1.10.6",

AryanJ-NYC commented 4 years ago

@JohnGrisham were you able to test with styled-components@4.4.0? I just upgraded and am still getting the error.

JohnGrisham commented 4 years ago

@AryanJ-NYC I have not yet tried this version I will let you know when I have and what result I got.

nitindhar7 commented 4 years ago

Still happening with styled-components@4.4.1

alextsoi commented 4 years ago

https://github.com/zeit/next.js/issues/7423 Seems there is solution in this URL.

VienNguyen113 commented 4 years ago

I'm still got this error too.

atirudom commented 4 years ago

Here to confirm again as @alextsoi said, #7423 solved my issue. (see comment by Sergioamjr)

tsirolnik commented 4 years ago

I'm using MUI with Styled Components and I'm getting this error too.

girardiricardo commented 4 years ago

I'm using styled-components@4.4.1 and still got this error too.

bengrunfeld commented 4 years ago

Not sure if this will actually help anyone (and it may just be shit programming on my end, so apologies if that is the case), but my instance of this error actually came from my own code.

So, in the following code, we have a styled component. Inside the styled component, we check for the existence of the prop displayMobile. If it's there, we use styles, and if not, it returns empty. The error is generated by the second usage of the props retrieval function found inside the template string - ${({ theme }) => theme.colors.green}. As soon as I take that out, the above error goes away. Not sure if this helps anyone out, but it took me an embarrassingly large amount of hours to deduce that.

import styled from "styled-components";

export const ServiceBox = styled.div`
  ${({ displayMobile }) =>
    displayMobile
      ? `
      flex-direction: column;
      width: 15.2rem;
      height: 16.2rem;
      padding-top: 1.7rem;
      justify-content: space-between;
      border: 0.4rem solid ${({ theme }) => theme.colors.green};
    `
      : ""}
`;
TheDSCPL commented 4 years ago

Found the solution.

Either this (better in the production bundle size): https://github.com/zeit/next.js/issues/4068#issuecomment-382298624 or this (same as the above but without checking the environment): https://github.com/zeit/next.js/issues/4068#issuecomment-485014513

I think the boilerplate code in all the links referenced by the styled-components documentation on the use case with Next.js should be updated to include the styled-components babel plugin as well. Because the _document.js and _app.js files aren't the complete boilerplate!

TheDSCPL commented 4 years ago

Well, actually I believe I've found a bug in the hot reload part of Next.JS's dev server.

I got the same warning again so I put a console.log in the _default function inside babel-plugin-styled-components module's lib/index.js file to see when the plugin was being used.

It turns out that the initial build runs the plugin but subsequent rebuilds (whenever a file is edited) don't rerun babel. Is this due to some kind of incremental build algorithm, @timneutkens?

Anyway, I'm not sure of what is causing the problem but it seems to be a problem during the rebuild process.

timneutkens commented 4 years ago

We cache Babel output and only rebuild the parts that cache, if you add a plugin the plugin itself is executed on bootup and the visitors will be executed when you change files.

DaoHuyTuan commented 4 years ago

styled-components/babel-plugin-styled-components#78 (comment)

i confirm this fixed just follow that instruction