<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Prateek Lohani</title>
    <description>The latest articles on DEV Community by Prateek Lohani (@prateek_lohani).</description>
    <link>https://dev.to/prateek_lohani</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2805780%2Ffb740249-01c7-446e-9f48-b93347e5ead4.jpeg</url>
      <title>DEV Community: Prateek Lohani</title>
      <link>https://dev.to/prateek_lohani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prateek_lohani"/>
    <language>en</language>
    <item>
      <title>Why You should avoid dangerouslySetInnerHTML in React?</title>
      <dc:creator>Prateek Lohani</dc:creator>
      <pubDate>Sun, 02 Feb 2025 17:51:59 +0000</pubDate>
      <link>https://dev.to/prateek_lohani/why-you-should-avoid-dangerouslysetinnerhtml-in-react-4ok</link>
      <guid>https://dev.to/prateek_lohani/why-you-should-avoid-dangerouslysetinnerhtml-in-react-4ok</guid>
      <description>&lt;p&gt;The &lt;u&gt;dangerouslySetInnerHTML&lt;/u&gt; in React allows developers to directly set the innerHTML property of an element without any sanitization. Passing untrusted user input into the attribute is risky and can lead to serious security vulnerabilities like Cross-Site-Scripting (XSS).&lt;br&gt;
So, It is always the best choice to avoid passing untrusted user input to the dangerouslySetInnerHTML attribute.&lt;br&gt;
If passing user input is absolutely necessary, ensure that the input is sanitized.&lt;/p&gt;

&lt;p&gt;Usage -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect, useRef } from "react";

const sanitizeHTML = (html) =&amp;gt; {
    // Only run in browser environment
    if (typeof window === 'undefined') return html;

    try {
        const parser = new DOMParser();
        const doc = parser.parseFromString(html, "text/html");

        // Define all the allowed tags to use 
        const allowedTags = ["p"];

        const sanitizeNode = (node) =&amp;gt; {
            if (node.nodeType === Node.ELEMENT_NODE) {
                const element = node;

                // Process children first
                Array.from(element.childNodes).forEach((child) =&amp;gt; sanitizeNode(child));

                if (!allowedTags.includes(element.tagName.toLowerCase())) {
                    // Replace element with its children
                    while (element.firstChild) {
                        element.parentNode.insertBefore(element.firstChild, element);
                    }
                    element.remove();
                }
            }
        };

        sanitizeNode(doc.body);
        return doc.body.innerHTML;
    } catch (error) {
        console.error('Error sanitizing HTML:', error);
        return html; // Return original content if sanitization fails
    }
};

const SanitizeContent = ({ content }) =&amp;gt; {
    const ref = useRef(null);

    useEffect(() =&amp;gt; {
        if (ref.current) {
            const sanitized = sanitizeHTML(content);
            ref.current.innerHTML = sanitized;
        }
    }, [content]);

    return &amp;lt;div ref={ref} /&amp;gt;;  
};

const App = () =&amp;gt; {
    const item = '&amp;lt;p&amp;gt;This is the sample !&amp;lt;/p&amp;gt;'
    return (
        &amp;lt;&amp;gt;
            &amp;lt;SanitizeContent content={item} /&amp;gt;
        &amp;lt;/&amp;gt;
    );
};

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
