DEV Community

Cover image for Getting rid of Next.js & styled-components Warning: Prop `className` did not match
Tarek Campos
Tarek Campos

Posted on

1

Getting rid of Next.js & styled-components Warning: Prop `className` did not match

So, if you've been in touch with the Next.js + styled-components error: "Warning: Prop className did not match", at development stage, here's a how to solve that problem.

Error Image

This problem only occurs on development stage of Next.js enviroment basically because at the current version of Next.js (11.1.2), Next uses Babel under the hood.

And because of that, we need to inform the compiler that we're using the styled-component library, in a way to keep the stylized layout rendered at refresh time.

For our own lucky, there is an easy way to solve that. With that in mind, lets add the babel-styled-components plugin, as you can see below:

$ yarn add babel-plugin-styled-components -D
Enter fullscreen mode Exit fullscreen mode

or in case you're using npm:

$ npm install --save-dev babel-plugin-styled-components
Enter fullscreen mode Exit fullscreen mode

After that, at the root of our directory, lets create a .babelrc file, then insert the code below:

{
  "presets": ["next/babel"],
  "plugins": [["styled-components", { "ssr": true }]]
}
Enter fullscreen mode Exit fullscreen mode

And that's it, just restart the development build of Next in a way to babel see and apply the latest config, then it should be working 100% error-free.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay