Search code examples
reactjstailwind-csspostcsshtml-react-parser

How to convert Html content into plain text in Tailwind and postcss


I'm not able to parse html-content in react, which is actually tailwind and postcss specific issue because i have used same code on codesandbox which is working fine without tailwind CSS styling.

Note: I have tested this on my local-computer with and without tailwind, i have faced same issue

App.js

import React, { useState } from 'react';
import parse from 'html-react-parser';

const App = () => {
  const [text, setText] = useState('<p>asdfsadf</p><ul><li>asdfsdf</li><li>sdcas</li></ul>');
  return (
    <div className="m-10">{parse(text)}</div>
  );
};

export default App;

Expected Output enter image description here

Actual Output enter image description here

Whether there is another way to convert html into plain-text when using tailwind for styling in react


Solution

  • Change your code like this:

    import React, { useState } from 'react';
    
    const App = () => {
      const [text, setText] = useState(
        '<p>asdfsadf</p><ul><li>asdfsdf</li><li>sdcas</li></ul>'
      );
      return <div className="m-10" dangerouslySetInnerHTML={{ __html: text }} />;
    };
    
    export default App;
    

    Result https://react-v29quj.stackblitz.io/