简体   繁体   English

我使用 Next.js 收到危险的SetInnerHTML 警告和空内容,这是怎么回事?

[英]What is happening such I receive dangerouslySetInnerHTML warning and empty content using Next.js?

First-off, I do know I should not nest <p> tags.首先,我知道我不应该嵌套<p>标签。

So I created a Next.js application and I have a component that sets rich text html as follows:所以我创建了一个 Next.js 应用程序,并且我有一个设置富文本 html 的组件,如下所示:

    <Typography
      dangerouslySetInnerHTML={{
        __html: richText
      }}
    />

The react component Typography is just a p tag in the end so this could simply be written as: React 组件Typography最后只是一个p标签,所以可以简单地写成:

    <p
      dangerouslySetInnerHTML={{
        __html: richText
      }}
    />

So when the browser loads the page, my content does not show up and I am left with the console warning:所以当浏览器加载页面时,我的内容没有显示出来,我留下了控制台警告:

Warning: Prop 'dangerouslySetInnerHTML' did not match. Server: "" Client: "<p>...</p>"

After a lengthy debugging session, during cleaning up, using the <span> instead of <p> was the solution经过长时间的调试 session,在清理过程中,使用<span>而不是<p>是解决方案

    <span
      dangerouslySetInnerHTML={{
        __html: richText
      }}
    />

Nested p tags was a mistake, regardless, what is happening that makes Next.js not render this particular content resulting in the empty string Server: "" ?嵌套 p 标签是一个错误,无论如何,发生了什么导致 Next.js 不呈现此特定内容导致空字符串Server: "" Is Next.js just simply very sensitive to errors and warnings? Next.js 只是对错误和警告非常敏感吗?

I've had the same problem because in the richtext I was getting <p> tags too, so when I switched my wrapping tag from <p> to <div> that error disappeared.我遇到了同样的问题,因为在richtext中我也得到了<p>标签,所以当我将包装标签从<p>切换到<div>时,该错误消失了。

I had the same issue, I solved this using react useState and useEffect hooks.我有同样的问题,我使用 react useStateuseEffect钩子解决了这个问题。 so it's not rendering in the server.所以它不会在服务器中呈现。

import { useState, useEffect } from 'react';

export default function ParentComponent({ content }) {
   const [render, setRender] = useState(false);
   
   useEffect(() => {
      setRender(true);
   }, []);

   return <Typography dangerouslySetInnerHTML={{ __html: render && content }} />
}

Next.js will de-nest invalid html on the server so Next.js 将在服务器上嵌套无效的 html 所以

<p> 
  <p>
     hello world
  </p>
</p>

becomes变成

<p></p>
<p> hello world </p>

so the HTML structure no longer matches the client rendered html hence rehydration fails所以 HTML 结构不再匹配客户端呈现的 html 因此补液失败

It depends on where richText is coming from.这取决于richText的来源。 Is it possible that on the server-side render, richText = "" , and then you make some API request on the front-end that sets the value of richText?有没有可能在服务器端渲染, richText = "" ,然后你在前端发出一些 API 请求来设置richText 的值?

Material-UI have a prop called component. Material-UI 有一个称为组件的道具。 use component as div使用组件作为div

<Typography {...other Typography Props} component="div" dangerouslySetInnerHTML={{
    __html: richText
  }}/>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM