react路由匹配_如何修复`dangerouslySetInnerHTML`与React中的错误不匹配

本文解决了一个关于在React中使用dangerouslySetInnerHTML时遇到的错误匹配问题,通过调整内部HTML结构,成功避免了将p标签嵌套在另一个p标签中的错误,确保了HTML的正确显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

react路由匹配

I was trying to print the HTML contained in a prop, using dangerouslySetInnerHTML, while I got this error in the browser console:

当我在浏览器控制台中收到此错误时,我尝试使用dangerouslySetInnerHTML设置InnerHTML来打印道具中包含HTML:

Warning: Prop `dangerouslySetInnerHTML` did not match.

This was a Next.js project, but the solution applies to any React code.

这是一个Next.js项目,但是该解决方案适用于任何React代码。

The string I was trying to print appeared for a while, and then disappeared. Quite strange!

我尝试打印的字符串出现了一段时间,然后消失了。 很奇怪!

It was even stranger when I tried to print a fixed HTML string, like this:

当我尝试打印固定HTML字符串时,甚至更陌生:

<p
  dangerouslySetInnerHTML={{
    __html: '<p>test</p>'
  }}></p>

The error message is cryptic but after a while, I realized I could not set a p tag inside another p tag.

该错误消息是神秘的,但一段时间后,我意识到我不能设置一个p另一个标记内p标签。

Switching to:

切换至:

<div
  dangerouslySetInnerHTML={{
    __html: '<p>test</p>'
  }}></div>

worked like a charm.

像魅力一样运作。

翻译自: https://flaviocopes.com/react-fix-dangerouslysetinnerhtml-did-not-match/

react路由匹配

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值