简体   繁体   English

`className` 不匹配。 服务器:

[英]`className` did not match. Server:

I am using NextJS getServerSideProps to fetch data and the page is rendering correctly with the data.我正在使用 NextJS getServerSideProps 来获取数据,并且页面使用数据正确呈现。 So this works fine except when rendering a dynamic classname property on a html element I am getting the following error:所以这工作正常,除非在 html 元素上呈现动态类名属性时,我收到以下错误:

Prop `className` did not match. Server: "" Client: "dark"

when rendering the following component:渲染以下组件时:

function Header(props: { darkMode: boolean }) {
  const [darkMode, setDarkMode] = useState(props.darkMode);
  
  return (
      <div className={(darkMode ? "dark" : "")}></div>   
  );
 }
 export default Header;

When debugging I can see that darkMode is true but the div element is still rendered without the className, assuming it's due to the error descriped above.调试时,我可以看到 darkMode 为 true,但 div 元素仍然在没有 className 的情况下呈现,假设它是由于上面描述的错误造成的。

I am actually using MaterialUI although this element is not an materialUI element.我实际上使用的是 MaterialUI,虽然这个元素不是一个 materialUI 元素。

I have specified.bablerc like this:我已经像这样指定了.bablerc:

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

Any idea why I am getting this error?知道为什么我会收到此错误吗?

It seem like the issue is the same as the one mentioned here .似乎问题与此处提到的问题相同。 If that's the case, I have posted the answer there如果是这样的话,我已经在那里发布了答案

The className mismatch is related to how React syncs up the DOM node after a page is server-side rendered. className不匹配与 React 在服务器端渲染页面后如何同步 DOM 节点有关。 Since React will only patch/sync the text context for the node and className is an attribute.因为 React 只会修补/同步节点的文本上下文,而className是一个属性。 A warning is triggered.触发警告。

Reference参考

https://github.com/facebook/react/issues/11128#issuecomment-334882514 https://github.com/facebook/react/issues/11128#issuecomment-334882514

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 nextjs:警告:道具 `className` 不匹配。 服务器:“x” 客户端:“y” - nextjs: Warning: Prop `className` did not match. Server: "x" Client: "y" Next.Js 带有样式组件的 React 应用程序。 警告:道具 `className` 不匹配。 服务器:“x” 客户端:“y” - Next.Js React app with styled components. Warning: Prop `className` did not match. Server: "x" Client: "y" 警告 Prop `href` 不匹配。 使用反应服务器端渲染 - Warning Prop `href` did not match. using react server-side-rendering 如何修复 `data-rbd-draggable-context-id` 不匹配。 服务器:“1”客户端:“0”,带有 react-beautiful-dnd 和 next.js - How to fix `data-rbd-draggable-context-id` did not match. Server: "1" Client: "0"' with react-beautiful-dnd and next.js 道具&#39;aria-activedescendant`不匹配。 反应-选择 - Prop `aria-activedescendant` did not match. react-select 文本内容不匹配。 React 16 中的警告 - Text content did not match. Warning in React 16 服务器和客户端中的类名水化不匹配,(警告:Prop `className` 与服务器和客户端不匹配) - Class name hydration mismatch in server and client, (Warning: Prop `className` did not match Server and Client) 仅在本地开发中的“类名不匹配” - "className did not match" in local dev only 根据屏幕尺寸设置视频源会触发“警告:道具`src`不匹配”。 - Setting video source depending on screen size triggers "Warning: Prop `src` did not match." 使用 UIkit 和 NextJs 时如何修复“警告:道具类名不匹配” - How to fix “Warning: Prop className did not match” when using UIkit and NextJs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM