Next.js styled-components 报错: Warning: Prop `className` did not match. Server:...

阅读 138

2022-03-12

Next.js + StyledComponents

浏览器console报错信息:

Warning: Prop `className` did not match. Server: "sc-gsDKAQ WHHak" Client: "sc-bdvvtL fKlryF"

大概就是 (hydrate) 水合注水失败了, 虽然是个 warning , 但界面却是不正常的. 这个警告必须解决.

解决方案

Next.js 12.1

next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
    reactStrictMode: true,
    compiler: {
        styledComponents: true,
    },
};

module.exports = nextConfig;

官方文档截图
这个是 v12.1 新增的.
估计以后还会有新的功能或者Css in js 的框架被集成进来.
在这里插入图片描述

v12.1以下的旧版本使用插件解决.

babel插件

精彩评论(0)

0 0 举报