Skip to content

Instantly share code, notes, and snippets.

@indreklasn
Created January 26, 2024 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indreklasn/5ebf9dec4727d3eadb61e8ab3ba89b14 to your computer and use it in GitHub Desktop.
Save indreklasn/5ebf9dec4727d3eadb61e8ab3ba89b14 to your computer and use it in GitHub Desktop.
import React, { useState } from 'react';
import DOMPurify from 'dompurify';
const UserComments = () => {
const [comments, setComments] = useState([
// Assume these comments are fetched from a server
{ id: 1, content: '<script>alert("XSS Attack!")</script>Great article!' },
{ id: 2, content: 'Really enjoyed this post.' },
// More comments...
]);
const createMarkup = (htmlContent) => {
return { __html: DOMPurify.sanitize(htmlContent) };
};
return (
<div>
<h2>User Comments</h2>
<ul>
{comments.map((comment) => (
<li key={comment.id} dangerouslySetInnerHTML={createMarkup(comment.content)} />
))}
</ul>
</div>
);
};
export default UserComments;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment