Skip to main content

Command Palette

Search for a command to run...

Rendering html response from API

Updated
3 min read
A

Frontend Developer with 3 years of experience building scalable, SEO-optimized web applications using React.js, Next.js, and modern JavaScript ecosystems. Skilled in performance optimization, advanced state management (Redux, React Query), and driving frontend architecture decisions within Agile development teams.

Apply styles on <p> and <li> tag

const apiResponse = {
    description:
      '<p>fgsdgsdgsdgsd</p><ol style="list-style-type: lower-greek;"><li>hgdj</li><li>fdjkhsdkjgh</li><li>fgvf</li></ol>',
  };

  const renderHTML = (htmlString) => {
    return { __html: htmlString };
  };

return (
<>
        <div dangerouslySetInnerHTML={renderHTML(apiResponse.description)} />
        {/* Apply custom styles for <p> and <li> here */}
        <style jsx>{`
          p {
            /* Your custom styles for <p> */
            color: blue;
            font-size: 16px;
          }

          li {
            /* Your custom styles for <li> */
            font-weight: bold;
            color: green;
          }
        `}</style>
      </div>
</>
)

এখানে ট্যাগ গুলোর উপর স্টাইল এপ্লাই করা হয়েছে, কিন্তু আমাদের renderHTML এর p এন্ড li ট্যাগ স্টাইল পাওয়ার কথা কিন্তু কম্পোনেন্ট এ সব p,li ট্যাগ এই স্টাইল পাচ্ছে। এই প্রব্লেমটা নিচে সল্ভ করে দেওয়া হলো।

Here the style is applied to the tags, but our renderHTML is supposed to get the p and li tag style but all the p, li tags in the component are getting this style. This problem is solved below.

Apply custom styles for <p> and <li> inside the "styled-content" class

const apiResponse = {
    description:
      '<p>fgsdgsdgsdgsd</p><ol style="list-style-type: lower-greek;"><li>hgdj</li><li>fdjkhsdkjgh</li><li>fgvf</li></ol>',
  };

  const renderHTML = (htmlString) => {
    return { __html: htmlString };
  };

return (
<>
       <div>
        <div
          className="styled-content"
          dangerouslySetInnerHTML={renderHTML(apiResponse.description)}
        />

        {/* Apply custom styles for <p> and <li> inside the "styled-content" class */}
        <style jsx>{`
          .styled-content p {
            /* Your custom styles for <p> inside .styled-content */
            color: blue;
            font-size: 16px;
          }

          .styled-content li {
            /* Your custom styles for <li> inside .styled-content */
            font-weight: bold;
            color: green;
          }
        `}</style>

        <div
          dangerouslySetInnerHTML={{ __html: offerData?.data[0]?.description }}
        />

        <p>Lorem ipsum dolor sit.</p>
      </div>
</>
)

Apply selectively render only specific tag

you can use the dangerouslySetInnerHTML prop to render HTML content, but if you want to selectively render only specific tags, you can use a more controlled approach by parsing the HTML and then filtering out the elements you don't want to render.

Here's an example of how you can achieve this in your component:


const MyComponent = () => {
  const apiResponse = {
    description: "<p>fgsdgsdgsdgsd</p><ol style=\"list-style-type: lower-greek;\"><li>hgdj</li><li>fdjkhsdkjgh</li><li>fgvf</li></ol>",
  };

  const renderHTML = (htmlString) => {
    // Parse the HTML string to a DOM object
    const parser = new DOMParser();
    const doc = parser.parseFromString(htmlString, 'text/html');

    // Filter out paragraphs and keep only the ordered list
    const olElement = doc.querySelector('ol');

    // Check if olElement exists before rendering
    return olElement ? { __html: olElement.outerHTML } : null;
  };
// ====================== shortcut =============================================
const renderHTML = (htmlString) => ({
    __html:
      (
        new DOMParser()
          .parseFromString(htmlString, 'text/html')
          .querySelector('ol') || {}
      ).outerHTML || null,
  });
// ===============================================================================

  return (
    <div>
      <div className="styled-content" dangerouslySetInnerHTML={renderHTML(apiResponse.description)} />

      {/* Apply custom styles for <li> inside the "styled-content" class */}
      <style jsx>{`
        .styled-content li {
          /* Your custom styles for <li> inside .styled-content */
          font-weight: bold;
          color: green;
        }
      `}</style>

      {/* Other content */}
      <div dangerouslySetInnerHTML={{ __html: offerData?.data[0]?.description }} />
      <p>Lorem ipsum dolor sit.</p>
    </div>
  );
};

export default MyComponent;

More from this blog

🚀 Docker Bind Mount ও Anonymous Volume ব্যবহার করে Local Codebase এর সাথে Container যুক্ত করা —

➡️ যখন আমরা bind mount ব্যবহার করি, তখন আমরা host machine-এর নির্দিষ্ট কোনো ফোল্ডার ঠিক করে দিই, যেটা কন্টেইনারের কোনো ফোল্ডারের সঙ্গে সরাসরি যুক্ত হয়ে যায়। এতে কন্টেইনারে পরিবর্তন করলে লোকাল ফাইলেও সেটা সাথে সাথে দেখা যায় — একেবারে রিয়েল টাইমে। 🧠...

Jul 3, 20253 min read

🚢 ডকার ভলিউম (Docker Volumes)

ডকার ভলিউম (Docker Volumes) : ডকার ভলিউম এমন একটি পদ্ধতি, যার মাধ্যমে কনটেইনারের ভিতরের ডেটা হোস্ট মেশিনে সংরক্ষণ করা যায়।যা ডকার কন্টেইনারের মধ্যে ডেটা সংরক্ষণ এবং শেয়ার করার জন্য ব্যবহৃত হয়। এটি ডেটাকে পারসিস্ট (স্থায়ীভাবে রাখা) করতে সাহায্য করে...

Jun 26, 20252 min read

How to Use MongoDB Transactions & Rollbacks

Transaction কী? Transaction এর মাধ্যমে একাধিক ডাটাবেইস অপারেশন একসাথে execute হয়, এবং সবগুলো সফল হলে তবেই ডাটাবেজে স্থায়ীভাবে save হয়। যদি কোনো একটি অপারেশন fail করে, তাহলে সবকিছু rollback হয়ে যায় — অর্থাৎ, কোনো ডেটা চেঞ্জই হয় না। উদাহরণ: ধরো তুমি এ...

May 27, 20251 min read

🐳 Docker Registry ও Repository: এক নজরে সহজ ব্যাখ্যা

🗂️ Docker Registry Docker Registry হল একটি centralized location যেখানে Docker কন্টেইনার ইমেজগুলি সংরক্ষণ এবং শেয়ার করা হয়। এটি পাবলিক বা প্রাইভেট হতে পারে। 👉 Docker Hub একটি পাবলিক রেজিস্ট্রি যা যে কেউ ব্যবহার করতে পারে এবং এটি ডিফল্ট রেজিস্ট্রি। ...

Apr 15, 20252 min read

🐳 Docker Image ও Container বোঝার জন্য সহজ ব্যাখ্যা (Class এর সাথে তুলনা)

Docker Image হচ্ছে Docker container রান করার জন্য একটি ব্লুপ্রিন্ট। Docker Image হচ্ছে একটি executable ফাইল, যার মধ্যে আমাদের অ্যাপ্লিকেশন রান করার জন্য প্রয়োজনীয় সব instruction, configuration এবং dependency আগে থেকেই সংরক্ষিত থাকে। অর্থাৎ, আমরা ...

Apr 14, 20251 min read

Untitled Publication

13 posts