question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React warning: Prop `className` did not match.

See original GitHub issue

Current behavior:

When using SSR and in dev mode, the classname generated on the server doesn’t match what’s on the client.

I think I’ve narrowed it down to an issue involving sourcemaps.

In https://github.com/emotion-js/emotion/blob/117c6e42687cdcb773153f7400c5a430f228aa73/packages/serialize/src/index.js#L387 it seems the code is attempting to extract the sourcemap string in the style:

  if (process.env.NODE_ENV !== 'production') {
    styles = styles.replace(sourceMapPattern, match => {
      sourceMap = match
      return ''
    })
  }

The particular elements that gives rise to this bug seem to have multiple sourcemaps. This is the style before the replace (I’ve abbreviated the base64 string itself):

color: #1a1a1a;
  font-weight: bold;
  margin: 0;
  padding: 0;
 color:;font-size:32px;line-height:1.15;margin-bottom:4px;/*# sourceMappingURL=data:application/json;charset=utf-8;base64,ey...19 */label:ColoredCardHeading;color:#BB2F89;hyphens:auto;/*# sourceMappingURL=data:application/json;charset=utf-8;base64,ey...19 */

And this is the style after replace:

color: #1a1a1a;
  font-weight: bold;
  margin: 0;
  padding: 0;
 color:;font-size:32px;line-height:1.15;margin-bottom:4px;label:ColoredCardHeading;color:#BB2F89;hyphens:auto;/*# sourceMappingURL=data:application/json;charset=utf-8;base64,ey...19

This particular component is created by extending another component in another file:

(file1.js)
import Component2 from "file2.js";
const Component1 = styled(Component2).`...`

(file2.js)
const Component3 = ...
const Component2 = styled(Component3)`...`
export default Component2

The base64 encoded strings in the styles above point to file1 and file2 respectively. So I suspect either there is a bug in sourcemap generation which creates two instances of sourcemap in the same style string, or a problem with the replace mechanism that extracts one and not the other.

The actual mismatch happens because the server generated sourcemap has "sources": ["file1.js"] in the base64 encoded json sourcemap, while on the client this is fully qualified: "sources": ["/Users/.../file1.js"]

To reproduce:

I’d be happy to create a repo to demo this, it will take me some time though due to SSR and the use of extractCritical.

Expected behavior:

Using the basic apis for styled should not create mismatched classname on client & server.

Environment information:

  • react version: 16.6.0
  • @emotion version: 10.0.27

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
shobhitsharmacommented, Aug 15, 2021

@Andarist It still persist for Firefox and Safari, in chrome works fine.

1reaction
Andaristcommented, Jan 20, 2020

@fsaheli funny thing - this most likely got fixed by accident on the next branch. The goal of the change was to give more accurate source maps to the browser for composed styles and to make this happen we had to add g flag to that sourceMapPattern: https://github.com/emotion-js/emotion/blob/d950d7d3a2d0a4c77d9037c0dbeae9d0b24e6577/packages/serialize/src/index.js#L335

I believe this accidentally fixed your problem. I encourage you to migrate to v11 (available on npm) anyway, even if you have already worked around your problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Warning: Prop `className` did not match. when using styled ...
That error is showing you the class that is being created by your styled-components library on the server and how it is different...
Read more >
Warning: Prop className did not match. #7322 - GitHub
Using css prop introduced with styled-components v4 causes Warning: Prop className did not match. . To Reproduce. Add any HTML element with css ......
Read more >
Fixing ClassName did not match error - DEV Community ‍ ‍
babelrc in the root directory and configure it. Here's the config file. Restart your server and refresh your server and you should be...
Read more >
Solving the Styled Components Warning in Next.JS with Babel
Solving the Styled Components Warning in Next.JS with Babel. Warning: Prop `className` did not match. Server: “fs-fdsf” Client: “sd-dfasj”.
Read more >
Prop `className` did not match - CodeSandbox
VS Code's tsserver was deleted by another application such as a misbehaving virus detection tool. Please reinstall VS Code. Manage Extension.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found