question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

problem with dangerouslySetInnerHTML

See original GitHub issue

Information:

  • 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:closed
  • Created 2 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
mAAdhaTTahcommented, Aug 18, 2021

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.

1reaction
RunDevelopmentcommented, Aug 18, 2021

I got it using useState and useEffect

That’s an even better solution.

but I’m not sure it’s performatico.::

React should be smart enough to cache things here. Should be fine.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found