🔨

Warning: Prop className did not match.

問題

NextJSでstyled componentsを使用すると、 Warning: Prop className did not match. というエラーメッセージとともにスタイルが壊れます。
 

問題を解決する

 

解決方法 1

next.config.js ファイルに以下のコードを追加し、サーバーを再起動します。
module.exports = { compiler: { styledComponents: true, }, }
 

解決方法 2

プロジェクトのデフォルトフォルダに.babelrc ファイルを生成し、以下のコードを追加します。
{ "presets": ["next/babel"], "plugins": [["styled-components", { "ssr": true }]] }
 
.babelrcプラグインが重複している場合は、以下のように入れます。
{ "presets": ["next/babel"], "plugins": ["inline-react-svg", ["styled-components", {"ssr": true}]] }