problem with dangerouslySetInnerHTML
See original GitHub issueInformation:
- Prism version: [1.24.1]
- Plugins: [a list of plugins you are using or ‘none’]
- Environment: [React.js and next.js]
Description I installed primsjs via yarn, and add it to my blog and it’s working, the problem is that the message below appears:
Warning: Prop dangerouslySetInnerHTML
did not match. Server: post…
Example
import PostStyle from "./post-styles.module.css";
import styles from "./style.module.css";
import CardImage from "../../Molecules/CardImage";
import Prism from "prismjs";
import { useEffect } from "react";
type Props = {
content: string;
thumbnail: string;
title: string;
};
const PostBody = ({ content, thumbnail, title }: Props) => {
useEffect(() => {
Prism.highlightAll()
}, []);
return (
<>
<CardImage src={thumbnail} alt={title} />
<article className={styles.section__article}>
<div
className={`${PostStyle["post"]}`}
dangerouslySetInnerHTML={{ __html: content }}
/>
</article>
</>
);
};
export default PostBody;
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Safe alternative to dangerouslySetInnerHTML - Stack Overflow
If XSS is your primary concern, you can use DOMPurify to sanitize your HTML before inserting it in the DOM via dangerouslySetInnerHTML ....
Read more >The Implications of DangerouslySetInnerHTML in React
The React framework is known to have adopted a browser-independent DOM system, which means it does not interact with the DOM directly.
Read more >Preventing XSS in React (Part 2): dangerouslySetInnerHTML
Dynamically rendering benign HTML code in React requires the use of dangerouslySetInnerHTML . That is not a naming mistake. This property is ...
Read more >What Is DangerouslySetInnerHTML? - Better Programming
dangerouslySetInnerHTML is an attribute under DOM elements in React. According to the official documentation, dangerouslySetInnerHTML is React's ...
Read more >DangerouslySetInnerHTML in React JS Explained
dangerouslySetInnerHTML is an attribute under DOM elements in React. According to the official documentation, dangerouslySetInnerHTML is React's replacement ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Your initial solution might be fine if you disable automatic highlighting with
Prism.manual = true
. The problem is the highlight in your initial useEffect isn’t actually the first highlight, and it’s happening before React runs, hence the mismatch.That’s an even better solution.
React should be smart enough to cache things here. Should be fine.